首页 > 解决方案 > 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.

标签: pythonpipgoogle-colaboratory

解决方案


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/


推荐阅读