Python 导入错误:没有名为 django.core.wsgi 的模块用于 uwsgi

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14194859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:43:52  来源:igfitidea点击:

ImportError: No module named django.core.wsgi for uwsgi

pythondjangouwsgi

提问by user1687717

I'm using uwsgi for my Django(version =1.4) project, but there's an error if I run

我在我的 Django(version =1.4) 项目中使用 uwsgi,但是如果我运行会出现错误

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()

采纳答案by sneawo

If you use virtualenv try to add hometo django.ini:

如果您使用 virtualenv 尝试添加home到 django.ini:

home=/path/to/venv/

To test it through web browser:

要通过 Web 浏览器进行测试:

uwsgi --ini django.ini --protocol=http

回答by Mads Skjern

The error ImportError: No module named django.core.wsgigenerally arises when uwsgi tries reading the wsgi.py file, and comes to the line:

ImportError: No module named django.core.wsgi当 uwsgi 尝试读取 wsgi.py 文件时,通常会出现该错误,并出现以下行:

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.pyfile, 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. Just use the --virtualenvcommand line argument, eg:

你可以告诉 uWSGI 虚拟环境的路径,它会找出正确的 PYTHONPATH。只需使用--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)

Surprisingly, setting $VIRTUAL_ENV has no effect on PYTHONPATH

令人惊讶的是,设置 $VIRTUAL_ENV 对 PYTHONPATH 没有影响

Strangely enough, if you don't use the --virtualenv argument, the environment variable $VIRTUAL_ENVwill be set correctly. Test this by inserting into wsgi.py:

奇怪的是,如果您不使用 --virtualenv 参数,环境变量$VIRTUAL_ENV将被正确设置。通过插入 wsgi.py 来测试:

print os.environ['VIRTUAL_ENV']

This will print:

这将打印:

/home/jdoe/myvirtualenv

But the PYTHONPATH is notset correctly, and does not include anything from the virtualenv.

但是 PYTHONPATH设置正确,并且不包含 virtualenv 中的任何内容。

I can't explain why this is.

我无法解释这是为什么。

回答by Yaroslav Nikitenko

Since you accepted the answer which mentions virtualenv, it seems that you use it. In this case make sure that djangois installed in your virtualenvdirectory (say venv).

由于您接受了提到的答案virtualenv,因此您似乎在使用它。在这种情况下,请确保django已安装在您的virtualenv目录中(例如venv)。

You can separately install it from pipunder virtualenvor manually create a symbolic link (if you are on Unix-like system) to venv's site-packages

您可以从pip下单独安装它virtualenv或手动创建一个符号链接(如果您在类 Unix 系统上)到venv's site-packages

ln -s /usr/path_to_django venv/lib/python2.7/site-packages/django

回答by Abhishek

In my case, I installed the Django application and everything else for Python3, but the uwsgi was using Python2. Just check the log while running uwsgi whether it is using Python2 or Python3, and reinstall uwsgi if it is not consistent. Look for the line similar to below line in uwsgi startup log.

就我而言,我为 Python3 安装了 Django 应用程序和其他所有内容,但 uwsgi 使用的是 Python2。运行uwsgi时查看日志是使用Python2还是Python3,不一致就重新安装uwsgi。在 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]

回答by Nick Kanavati

I received this error because I created the virtual environment in a shared folder of virtualbox which didn't allow symbolic links. 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.

我收到此错误是因为我在不允许符号链接的 virtualbox 共享文件夹中创建了虚拟环境。我收到了一些错误,但似乎一切正常,所以我继续操作,直到出现此错误。当我重新创建我的虚拟环境,确保没有错误并将 uwsgi 指向该文件夹时,问题就解决了。

回答by G?ktu? A???

If you installed gunicorn on both sudo apt-get install gunicornand (venv) pip install gunicorn, use sudo apt-get remove gunicornand restart your virtual environment. This way, it worked for me.

如果您在sudo apt-get install gunicorn和上都安装了 gunicorn (venv) pip install gunicorn,请使用sudo apt-get remove gunicorn并重新启动您的虚拟环境。这样,它对我有用。