简体   繁体   English

如何在 Google Colab 的 Jupyter 笔记本中导入自定义 Python package 和模块?

[英]How can I import a custom Python package and module in a Jupyter notebook on Google Colab?

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'."当我在 Google Colab 上的 Jupyter 笔记本中导入自定义 Python package 和模块时,Python 解释器报告了一条名为“未找到错误”的错误消息。

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.我希望能够开发一个 Jupyter 笔记本,它使用来自我开发和测试的不同 Python 包的不同 Python 类/模块的功能。

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. 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: Jupyter notebook 的源代码是:

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".在我的 Google Colab Jupyter 笔记本(我的驱动器 -> Colab 笔记本)的 Google Drive 托管代码目录中,我有一个名为“utilities”的文件夹,其中包含一个名为“simple_module.py”的 Python 脚本。

The source code for "simple_module.py" is provided as follows: “simple_module.py”的源代码如下:

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".在我的 Google Drive 的“Colab Notebooks”目录中,我还有一个名为:“ 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.我需要做什么才能使用我创建并经过全面测试的 Python 包中的模块/类。

P/S: The question and solution in How to import custom modules in google colab? P/S: How to import custom modules in google colab中的问题和解决方案? does not cover importing Python modules in embedded in Python packages.不包括导入 Python 包中嵌入的 Python 模块。

I can import Python modules in the directory for my Google Drive hosting code for Google Colab Jupyter notebooks (My Drive -> Colab Notebooks).我可以将 Python 模块导入 Google Colab Jupyter 笔记本(我的驱动器 -> Colab 笔记本)的 Google Drive 托管代码目录中。

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.) 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.)

As of Feb,2020 you can simply link the google drive to your colab notebook.从 2020 年 2 月起,您只需将 google 驱动器链接到您的 colab 笔记本即可。

  1. Go to left Pane. Go 到左窗格。
  2. Select files. Select 文件。
  3. Click on Mount drive.单击安装驱动器。

Now, navigate to the folder where your .py module is located.现在,导航到.py模块所在的文件夹。

Right click on the directory and click on copy path.右键单击目录,然后单击复制路径。

Go to your colab notebook and type: Go 到您的 colab 笔记本并键入:

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:在此之后,您应该能够使用以下命令在您的 colab 中加载模块:

from module_name import *

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 google colab(python,Jupyter notebook)中安装预定义的包(类)? - How to install a predefined package(class) in google colab(python,Jupyter notebook)? 如何在Anaconda(Jupyter笔记本)中导入python自定义类 - How can I import python custom class in Anaconda (Jupyter notebook) 如何使用google colab运行导入自定义python模块(两个文件在存储库文件夹中的级别相同)的现有jupyter笔记本? - How to run existing jupyter notebook that imports custom python module(both files are same level in repository folder) using google colab? 如何将 pyscipopt 导入 Google Colab(在 Jupyter Notebook 文件中)? - How to import pyscipopt to Google Colab (in the Jupyter Notebook file)? Python:在 Google Colab 中导入自定义模块 - Python: Import custom module in Google Colab 如何在 Jupyter Notebook 中将 python 文件作为模块导入? - How to import python file as module in Jupyter notebook? 如何将Python软件包导入Google Colab? - How to import a Python package into Google Colab? 如何在 Google Colab 上实现这个 Jupyter Notebook? - How to implement this Jupyter notebook on Google Colab? 如何在Jupyter笔记本中导入CPLEX? - How can I import CPLEX in Jupyter notebook? jupyter notebook 的 Python3 环境下无法导入已安装的包 - Can't import the installed package in Python3 environment of jupyter notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM