[英]ImportError: No module named django.core.wsgi for uwsgi
I'm using uwsgi for my Django(version =1.4) project, but there's an error if I run 我正在使用uwsgi作为我的Django(版本= 1.4)项目,但是如果我运行则会出错
uwsgi --ini django.ini
from django.core.wsgi import get_wsgi_application ImportError: No module named django.core.wsgi
but I could import django.core.wsgi as follows: 但我可以导入django.core.wsgi如下:
>>> import django.core.wsgi
the django.ini file: django.ini文件:
[uwsgi] chdir=/path/to/my/app module=app.wsgi:application master=True vacuum=True max-requests=5000 socket=127.0.0.1:9000
wsgi.py wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
The error ImportError: No module named django.core.wsgi
generally arises when uwsgi tries reading the wsgi.py file, and comes to the line: 错误ImportError: No module named django.core.wsgi
当uwsgi尝试读取wsgi.py文件时,通常会出现ImportError: No module named django.core.wsgi
,并且出现在该行中:
from django.core.wsgi import get_wsgi_application
It can't find this these modules because Django is not installed, or if it is installed, it is not in PYTHONPATH. 它找不到这些模块,因为没有安装Django,或者如果安装了它,它不在PYTHONPATH中。
If your project is in a virtualenv and Django is only installed in this virtualenv, somehow the path to the Django modules are not in the PYTHONPATH , and thus Python can't find it. 如果您的项目是virtualenv并且Django仅安装在这个virtualenv中,那么Django模块的路径不在PYTHONPATH中 ,因此Python无法找到它。
If you are curious, you can insert the following code into the wsgi.py
file, and see the PYTHONPATH: 如果您很好奇,可以将以下代码插入到wsgi.py
文件中,并查看PYTHONPATH:
import os
print '===== sys.path / PYTHONPATH ====='
for k in sorted(os.environ.keys()):
v = os.environ[k]
print ('%-30s %s' % (k,v[:70]))
If you run a local version of uwsgi, installed in the virtualenv, then the path will be set correct, but if you run a global version of uwsgi it will normally not have the PYTHONPATH set correctly. 如果你运行virtualenv中安装的uwsgi的本地版本,那么路径将被设置正确,但如果你运行uwsgi的全局版本,它通常不会正确设置PYTHONPATH。
You can tell uWSGI the path to the virtualenv, and it will figure out the correct PYTHONPATH. 你可以告诉uWSGI virtualenv的路径,它会找出正确的PYTHONPATH。 Just use the --virtualenv
command line argument, eg: 只需使用--virtualenv
命令行参数,例如:
uwsgi --http :8001 --module wsgi --virtualenv /home/jdoe/myvirtualenv
(The following arguments does exactly the same as --virtualenv: --venv, --home, -H) (以下参数与--virtualenv完全相同: - venv, - home,-H)
Strangely enough, if you don't use the --virtualenv argument, the environment variable $VIRTUAL_ENV
will be set correctly. 奇怪的是,如果不使用--virtualenv参数,将正确设置环境变量$VIRTUAL_ENV
。 Test this by inserting into wsgi.py: 通过插入wsgi.py进行测试:
print os.environ['VIRTUAL_ENV']
This will print: 这将打印:
/home/jdoe/myvirtualenv
But the PYTHONPATH is not set correctly, and does not include anything from the virtualenv. 但是PYTHONPATH设置不正确,并且不包含virtualenv中的任何内容。
I can't explain why this is. 我无法解释为什么会这样。
If you installed gunicorn on both sudo apt-get install gunicorn
and (venv) pip install gunicorn
, use sudo apt-get remove gunicorn
and restart your virtual environment. 如果你在sudo apt-get install gunicorn
和(venv) pip install gunicorn
,请使用sudo apt-get remove gunicorn
并重新启动你的虚拟环境。 This way, it worked for me. 这样,它对我有用。
If you use virtualenv try to add home
to django.ini: 如果您使用virtualenv尝试将home
添加到django.ini:
home=/path/to/venv/
To test it through web browser: 要通过Web浏览器进行测试:
uwsgi --ini django.ini --protocol=http
In my case, I installed the Django application and everything else for Python3, but the uwsgi was using Python2. 在我的例子中,我为Python3安装了Django应用程序和其他所有内容,但是uwsgi使用的是Python2。 Just check the log while running uwsgi whether it is using Python2 or Python3, and reinstall uwsgi if it is not consistent. 只需在运行uwsgi时检查日志,无论是使用Python2还是Python3,如果不一致,请重新安装uwsgi。 Look for the line similar to below line in uwsgi startup log. 在uwsgi启动日志中查找类似于以下行的行。
Python version: 3.4.3 (default, Oct 14 2015, 20:31:36) [GCC 4.8.4]
VS Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
Python version: 3.4.3 (default, Oct 14 2015, 20:31:36) [GCC 4.8.4]
VS Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
Since you accepted the answer which mentions virtualenv
, it seems that you use it. 既然你接受了提到virtualenv
的答案,那么你似乎就是在使用它。 In this case make sure that django
is installed in your virtualenv
directory (say venv
). 在这种情况下,请确保在virtualenv
目录中安装了django
(例如venv
)。
You can separately install it from pip
under virtualenv
or manually create a symbolic link (if you are on Unix-like system) to venv
's site-packages 你可以从virtualenv
下的pip
单独安装它,或者手动创建一个符号链接(如果你是类Unix系统)到venv
的站点包
ln -s /usr/path_to_django venv/lib/python2.7/site-packages/django
I received this error because I created the virtual environment in a shared folder of virtualbox which didn't allow symbolic links. 我收到此错误是因为我在virtualbox的共享文件夹中创建了虚拟环境,该文件夹不允许使用符号链接。 I recieved some errors but everything seemed to work so I continued until I got this error. 我收到了一些错误,但一切似乎都有效,所以我继续,直到我收到此错误。 The problem was solved when I recreated my virtual environment, made sure there were no errors and pointed uwsgi to the folder. 当我重新创建我的虚拟环境时,问题得到了解决,确保没有错误并将uwsgi指向该文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.