python - ModuleNotFoundError after successful pip install in Google Colaboratory
问题描述
I'm trying to import a package I wrote into a Google Colaboratory notebook. I uploaded the package contents to my Drive, then ran:
[ ] 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
shows the package has been successfully installed. However, imports of the package fail with a ModuleNotFoundError
.
I've successfully pip installed and imported my-package on my local machine. I've also successfully installed and imported another Python package through the same Colab notebook using pip install --user
. As mentioned here, I've also tried restarting the kernel.
This may be related to this related but unanswered question.
解决方案
The editable install (or setuptools development-mode) appends the module path to an easy-install.pth
file. The site
module processes these files when python is started and appends the paths to sys.path
. That's why it works only after restarting the runtime.
One can avoid restarting the colab notebook by importing the site
module and (re)running site.main()
.
%pip install -e pkg
import site
site.main()
import pkg
However in the example below this has the effect of removing the current directory from sys.path
and replacing it with its absolute 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__}")
Prints
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
Example colab notebook: https://colab.research.google.com/drive/1S5EU-MirhaTWz1JJVdos3GcojmOSLC1C?usp=sharing
Stumbled onto this via the blog post: https://yidatao.github.io/2016-05-10/Python-easyinstall-generates-pth-file/
推荐阅读
- authentication - 如何通过 keycloak 向 github 企业中的每个用户添加主电子邮件?
- spring-boot - javax.persistence.criteria.CriteriaBuilder#or 是如何工作的?
- python - 如何制作一个只包含 None 值的矩阵?
- python-3.x - 使用 python pandas 比较每小时数据
- javascript - 如何检查字符串是否与数据库中的任何字符串匹配
- python - 如何按小时汇总数据?
- angular - Angular Universal、元标记、复制链接、粘贴到社交 AZURE MSAL AUTHENTICATION crypto_nonexistent:加密对象或函数不可用
- multithreading - 从 Rust 中的多个线程写入内存映射文件
- python - 在列表 x 中插入 n --> 将列表 x 附加到列表 y --> 在列表 x 中删除 n
- python - 如何使用 python 将文本附加到文件中?