How to import .py file in jupyter notebook from AWS S3 - python-3.x

I am working on a jupyter notebook in AWS, I have two files: main.ipynb and utils.py, what I would like to do is to import utils in my jupyter notebook file.
Unfortunately I have tried the following solutions and none of them are working:
import sys
sys.path.append("/home/jovyan/dir1")
%load utils.py
And to directly import after changing the directory
import utils
my file "utils.py":
def hello():
print("hello")
Problem solved:
the solution is to add those :
s3 = boto3.resource('s3')
s3.meta.client.download_file(os.environ['S3_BUCKET'], "dir1/utils.py", "utils.py")
import utils

Export python path to the path from where you are trying to import utils.
Run this below command from you file path
export $PYTHONPATH=.

Related

Run test in separate file with unittest.main

I create a notebook which looks like the following:
import unittest
class TestHelpers(unittest.TestCase):
def test_trivial2(self):
self.assertEqual(True,True)
unittest.main(argv=[''],exit=False)
When running the notebook, everything works fine. Now, I try to move the test out to a separate *.py file:
Notebook code:
import unittest
unittest.main(argv=[''],exit=False)
New Python file (in same repo):
import unittest
class TestHelpers(unittest.TestCase):
def test_trivial2(self):
self.assertEqual(True,True)
When running the notebook I always get
Ran 0 test
I tried to add the path as argv such as
unittest.main(argv=['/Workspace/Repos/XXX/repo'],exit=False)
but the *.py file is not discorved
What do I have to hand over to argv so that the py file is identified
Thank you Daniil for the link:
import unittest
unittest.main(module="simple_test",argv=[''],exit=False)
makes the job!

Importing Colab modules into Colab

I am new to using Colab, I want to create modules that are imported into other notebooks. I have tried a number of methods and the best I end up with is this error message:
ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import converters
ModuleNotFoundError: No module named 'converters'
Can you import a *.ipynb as a module into Colab?
I have mounted the gdrive and set the root dir - see code below.
from google.colab import drive
drive.mount('/content/gdrive/', force_remount=True)
root_dir = "content/gdrive/My Drive/Colab Notebooks/projects/utilities/"
base_dir = root_dir + "projects/"
I have tried other methods as well, I have also tried sharing the notebook. help. thanks
Try :
# (1) Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/drive')
# (2) Insert the directory using sys
import sys
sys.path.insert(0,’/content/gdrive/My Drive/Colab Notebooks/projects/utilities/’)
# Import your module or file
import my_module
OR
from google.colab import drive
drive.mount('/content/drive')
sys.path.append('/content/gdrive/My Drive/Colab Notebooks/projects/utilities/')
!cp -r "/content/drive/My Drive/Colab Notebooks/projects/utilities/my_module.ipynb" '/content/'

Where to import dependency while working with multiple files

Let's say there are two files - mask.py and main.py.
mask.py has some function that I'm importing into main.py.
So if that function in mask.py which I'm importing has dependency like "os", where should I import os - in mask.py or main.py.
Let us consider a scenario as you have stated by using two files mask.py and main.py.
mask.py
import os
def some_function():
os.environ['a_url'] = "something.com" # using dependency as you mentioned
main.py
from mask import some_function
# do something with the function
Now, coming to your query, if you use import os in main.py but not in mask.py, you will get NameError in mask.py saying:
NameError: name 'os' is not defined
This is because you need to import any dependency in the same file where it is used. Also, if both of your file uses this dependency, you need to import it in both the files.
Hope this clarifies your query.

How do I import modules that are used inside a Python 3 file?

I have the following code in myfile.py
def show_path():
print(os.getcwd())
In Jupyter notebook, I have the following (which runs fine):
import os
from myfile.py import show_path
However, when I run the following:
show_path()
I get 'name 'os' is not defined' error. But when I simply type:
os.getcwd()
I get the path, which I understand. But I don't understand why running show_path() doesn't do the same thing? Is it necessary to have import os inside my myfile.py file? If so, why?
You have to import os in your module, its import list is different then that of your notebook. Since you did not import it in your module, it wa unavailable when your module was parsed.

JupyterLab - import module

I use JupyterLab, jupyter notebook and try to import the script. The script is located in the notebook's directory.
Unfortunately import is impossible.
Error:
ImportError: No module named 'irt_01_generate_sample_data'
On the other hand:
When I run terminal and import the script, everything works fine.
What should I do to import the script ?
One thing to check is whether you're running the same Python (executable) and have the same paths (where it is looking for imports) in both the terminal and Jupyter Notebook:
import sys
sys.executable
sys.path

Resources