[英]Python manage.py ImportError: No module named django
I can't seem to get my manage.py script to recognize the django module. 我似乎无法让manage.py脚本识别django模块。 pip install req/dev.txt installs all of the modules I would expect it to into my my python virtual environment (stored at .venv). pip install req / dev.txt会将我希望将其安装的所有模块安装到我的python虚拟环境(存储在.venv中)中。
I think I've narrowed it down to a problem with either my $PYTHON_PATH or with my pip requirements files. 我认为我已将其范围缩小到$ PYTHON_PATH或pip要求文件的问题。 I've looped through sys.path, and I see that one of the values points to a folder in .venv that I can confirm contains the django module. 我遍历了sys.path,发现其中一个值指向.venv中的一个文件夹,我可以确认该文件夹包含django模块。
I'm unfortunately at a loss. 不幸的是我很茫然。 I've searched through a dozen related questions on Stack Overflow and have yet to find a solution that works. 我已经搜索了有关Stack Overflow的十几个相关问题,但尚未找到有效的解决方案。 Does anyone have any clues to point me in the right direction? 有没有人能向我指出正确的方向?
Error message: 错误信息:
$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/apps/config.py", line 119, in create
import_module(entry)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named django
Contents of reqs/dev.txt reqs / dev.txt的内容
-r common.txt
django-debug-toolbar==1.3.2
Contents of reqs/common.txt reqs / common.txt的内容
django==1.8
Fabric==1.10.2
ShopifyAPI>=2.1.5
Sphinx==1.3.1
celery>=3.1.20
django-compressor>=1.5
django-toolbelt>=0.0.1
jdcal>=1.0
kombu>=3.0.35
openpyxl==2.2.5
python-dateutil>=2.4.2
psycopg2>=2.5
requests==2.7.0
whitenoise==2.0
shippo==1.4.0
Contents of manage.py: manage.py的内容:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.dev")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Problem solved after a good night's rest. 一夜安息后问题解决了。
The issue was that my INSTALLED_APPS had a reference to kombu.transport.django
, which seems to be a completely unnecessary dependency we added for celery. 问题是我的INSTALLED_APPS引用了kombu.transport.django
,这似乎是我们为芹菜添加的完全不必要的依赖项。 More information on that here: Celery, kombu and django - import error 有关此的更多信息: Celery,kombu和django-导入错误
How I discovered the solution, in case the debugging technique helps anyone else: 我如何找到解决方案,以防调试技术对其他人有所帮助:
I started by diving into the stack trace. 我首先研究了堆栈跟踪。 I (temporarily) modified the files in .venv/
to add print statements for the input variables until I found my first clue in populate()
in .venv/lib/python2.7/site-packages/django/apps/registry.py
我(临时)修改了.venv/
的文件.venv/
输入变量添加打印语句,直到在.venv/lib/python2.7/site-packages/django/apps/registry.py
中的populate()
中找到我的第一个线索为止
Printing through each entry in populate()
allowed me to see that the package that was breaking was kombu.transport.django
and NOT the django
module proper. 通过populate()
每个条目的打印,我可以看到被破坏的软件包是kombu.transport.django
而不是django
模块。 Deleting that entry from my INSTALLED_APPS solved the problem. 从我的INSTALLED_APPS中删除该条目即可解决此问题。
To cleanup I reverted the files in .venv/
by removing my print statements and I was off and running! 为了清理,我通过删除我的打印语句恢复了.venv/
的文件,然后我就可以运行了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.