首页 > 解决方案 > ModuleNotFoundError while module was installed using pip

问题描述

This is a very weird situation. I've been using django and venv for a while and during my last pull of code, when trying to run python manage.py collectstatic, I ran into a ModuleNotFoundError. However, the module is installed and if I try to reinstall it, pip tells me I already have it.

The strange thing is I'm seeing importlib using my system python path ("/usr/lib/python3.6...") which I think it should be my virtualenv path...

If I run which python I get the correct venv python path...

This is the error I get:

File "/home/ubuntu/venv/lib/python3.6/site-packages/django/apps/config.py",
   line 90, in create
       module = import_module(entry)   
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in
   import_module
       return _bootstrap._gcd_import(name[level:], package, level)   
File "<frozen importlib._bootstrap>", line 994, in _gcd_import   
File "<frozen importlib._bootstrap>", line 971, in _find_and_load   
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
   ModuleNotFoundError: No module named 'django-mailbox'

标签: pythondjangopipvirtualenv

解决方案


您可能使用pip install django. pip这将安装与您的路径中找到的可执行文件相对应的 Python 安装模块。如果您安装了多个 Python 版本,则该pip命令可能指向您的 Python 3.5 安装(例如),而该python命令指向您的 Python 3.6 安装。

确保为正确的 Python 版本安装包,而不是:

pip install <package>

利用:

python -m pip install <package>

或者,如果您想使用特定的 Python 版本:

python3.6 -m pip install <package>

(在后一种情况下,您应该使用 运行您的应用程序python3.6 your_application.py


推荐阅读