我正在尝试将我写的程序包导入Google Colaboratory笔记本中。我将包裹内容上传到云端硬盘,然后运行:
[ ] from google.colab import drive
[ ] drive.mount('content/gdrive/')
[ ] ! pip install --user /content/gdrive/My\ Drive/my-package
Processing ./gdrive/My Drive/my-package
Building wheels for collected packages: my-package
Building wheel for my-package (setup.py) ... done
Created wheel for my-package:
Stored in directory:
Successfully built my-package
Installing collected packages: my-package
Successfully installed my-package-1.0.0.dev1
pip list
显示软件包已成功安装。但是,该软件包的导入失败,并带有ModuleNotFoundError
。
我已成功在本地计算机上pip安装并导入了我的软件包。我还使用pip install --user
通过同一Colab笔记本成功安装并导入了另一个Python软件包。如here所述,我还尝试过重新启动内核。
这可能与与this相关但未回答的问题有关。
答案 0 :(得分:2)
可编辑的安装(或setuptools development-mode)将模块路径附加到easy-install.pth
文件中。 site
模块在启动python时处理这些文件,并将路径附加到sys.path
。这就是为什么它仅在重新启动运行时之后才起作用。
通过导入site
模块并(重新)运行site.main()
,可以避免重新启动colab笔记本。
%pip install -e pkg
import site
site.main()
import pkg
但是,在下面的示例中,这具有从sys.path
删除当前目录并将其替换为其绝对路径的作用。
%pip install -e "git+https://github.com/jd/tenacity#egg=tenacity"
print("\nTrying to import tenacity")
try:
import tenacity
except ImportError as exc:
print("ImportError")
print(exc)
print()
import sys, site
print("\n##### sys.path original #####")
for p in sys.path:
print(f"'{p}'")
print()
site.main()
print("\n##### sys.path after site.main() #####")
for p in sys.path:
print(f"'{p}'")
print()
import tenacity
print(f"\nImported tenacity from {tenacity.__file__}")
打印
Obtaining tenacity from git+https://github.com/jd/tenacity#egg=tenacity
Cloning https://github.com/jd/tenacity to ./src/tenacity
Running command git clone -q https://github.com/jd/tenacity /content/src/tenacity
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.6/dist-packages (from tenacity) (1.15.0)
Installing collected packages: tenacity
Running setup.py develop for tenacity
Successfully installed tenacity
Trying to import tenacity
ImportError
No module named 'tenacity'
##### sys.path original #####
''
'/env/python'
'/usr/lib/python36.zip'
'/usr/lib/python3.6'
'/usr/lib/python3.6/lib-dynload'
'/usr/local/lib/python3.6/dist-packages'
'/usr/lib/python3/dist-packages'
'/usr/local/lib/python3.6/dist-packages/IPython/extensions'
'/root/.ipython'
##### sys.path after site.main() #####
'/content'
'/env/python'
'/usr/lib/python36.zip'
'/usr/lib/python3.6'
'/usr/lib/python3.6/lib-dynload'
'/usr/local/lib/python3.6/dist-packages'
'/usr/lib/python3/dist-packages'
'/usr/local/lib/python3.6/dist-packages/IPython/extensions'
'/root/.ipython'
'/content/src/tenacity'
Imported tenacity from /content/src/tenacity/tenacity/__init__.py
colab笔记本示例:https://colab.research.google.com/drive/1S5EU-MirhaTWz1JJVdos3GcojmOSLC1C?usp=sharing
通过博客帖子https://yidatao.github.io/2016-05-10/Python-easyinstall-generates-pth-file/
偶然发现了这一点答案 1 :(得分:0)
您链接到的有关重新启动运行时的github页面有点含糊,因此我只想澄清一下:
您需要运行!pip install
单元格。 然后“重新启动运行系统”。 然后运行您的import
语句单元格。
我可能建议您在执行这些步骤之前“重置所有运行时”,只是为了确保您的状态良好。
-如果上述步骤 是您已经执行的操作: 您使用的是Python 2还是3笔记本? (不是100%肯定为什么会那么重要,但是更多信息会很好)
您是从Google驱动器链接pip install
到本地计算机吗? (如果没有,请尝试查看是否可行并报告)
答案 2 :(得分:0)
由于@jojo的建议,我在本地计算机上卸载并重新安装了该软件包,从而能够诊断问题。在本地计算机和Colab上,仅当在-e
命令(pip
)中包含pip install --user -e /content/gdrive/My\ Drive/my-package
(可编辑)标志并重新启动软件包时,才能够成功安装和导入软件包。安装后运行)。我不知道为什么包含-e
标志会有所作为;如果您有任何见解,请发表评论!