Reputation: 93
When I import a custom Python package and module in my Jupyter notebook on Google Colab, the Python interpreter reports an error message indicating "ModuleNotFoundError: No module named 'utilities'."
I would like to be able to develop a Jupyter notebook that uses functions from different Python classes/modules from different Python packages that I have developed and tested.
I have simplified my Jupyter notebook that makes a function call to a sole Python module/class in a single Python package stored in the same directory on Google Drive.
The source code for the Jupyter notebook is:
import importlib.util
from google.colab import drive
drive.mount('/content/drive')
import sys
sys.path.append('/content/drive/My\ Drive/Colab\ Notebooks/utilities')
# Module to test if I can import a Python package and module.
from utilities.simple_module import simple
class Try_to_Import_Package:
number_times_executed = 0
# Accessor and Mutator method.
@staticmethod
def get_number_times_executed():
Try_to_Import_Package.number_times_executed = Try_to_Import_Package.number_times_executed + 1
print(" Try_to_Import_Package 'Hello World' function called:",Try_to_Import_Package.number_times_executed,"times.")
if __name__ == "__main__":
for x in range(10):
simple.get_number_times_executed()
Try_to_Import_Package.get_number_times_executed()
In the directory for my Google Drive hosting code for Google Colab Jupyter notebooks (My Drive -> Colab Notebooks), I have a folder named "utilities" with a Python script named "simple_module.py".
The source code for "simple_module.py" is provided as follows:
class simple:
number_times_executed = 0
# Accessor and Mutator method.
@staticmethod
def get_number_times_executed():
simple.number_times_executed = simple.number_times_executed + 1
print(" simple 'Hello World' function has been called:",simple.number_times_executed,"times.")
In the "Colab Notebooks" directory in my Google Drive, I also have a file named: "init.py".
Its contents are:
from .utilities import *
What do I need to do to be able to use modules/classes from Python packages that I created and thoroughly tested.
P/S: The question and solution in How to import custom modules in google colab? does not cover importing Python modules in embedded in Python packages.
I can import Python modules in the directory for my Google Drive hosting code for Google Colab Jupyter notebooks (My Drive -> Colab Notebooks).
However, I have problems including Python modules/classes stored in Python packages (subdirectories of the folder/directory including the Python script with the main function for the Python program.)
Upvotes: 0
Views: 1931
Reputation: 93
As of Feb,2020 you can simply link the google drive to your colab notebook.
Now, navigate to the folder where your .py
module is located.
Right click on the directory and click on copy path.
Go to your colab notebook and type:
import sys
sys.path.append('your/folder/path/in/google/drive')
After this you should be able to load the modules in your colab using:
from module_name import *
Hope this helps!
Upvotes: 3