简体   繁体   English

带有mod_wsgi的Apache上的Django:设置上的ImportError

[英]Django on Apache with mod_wsgi: ImportError on settings

I am serving a Django site on Apache2 server with mod_wsgi. 我正在使用mod_wsgi在Apache2服务器上提供Django站点。 Here is what my mysite/mysite directory looks like: 这是mysite / mysite目录的样子:

(mysite_venv)➜  mysite  tree mysite -I "*.pyc"
    mysite
    ├── __init__.py
    ├── db.sqlite3
    ├── settings
    │   ├── __init__.py
    │   ├── base.py
    │   └── local.py
    ├── urls.py
    └── wsgi.py

When I run this project using python manage.py runmodwsgi I get an Internal Server Error with this message in the logs: 当我使用python manage.py runmodwsgi运行该项目时,我在日志中收到内部服务器错误和以下消息:

[wsgi:error] [pid 8900] ImportError:         No module named mysite.settings.local
[mpm_prefork:notice] [pid 8898] AH00170: caught SIGWINCH, shutting down gracefully

Here is what my manage.py looks like: 这是我的manage.py的样子:

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.local")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

But when I change the directory mysite/mysite like this: 但是当我像这样更改目录mysite / mysite时:

(mysite_venv)➜  mysite  tree mysite -I "*.pyc"
mysite
├── __init__.py
├── base.py
├── db.sqlite3
├── local.py
├── urls.py
└── wsgi.py

And change the code os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.local") to os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.local") the server runs OK! 并将代码os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.local")更改为os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.local")服务器运行正常! How can I fix this issue? 如何解决此问题?

PS: When I use python manage.py runserver to run this project, everthing is OK too. PS:当我使用python manage.py runserver运行该项目时,一切都还可以。

Here is what my sys.path looks like: 这是我的sys.path的样子:

/Users/gagaxiaolong/PycharmProjects/mysite
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python27.zip
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/plat-darwin
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/plat-mac
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/plat-mac/lib-scriptpackages
/Users/gagaxiaolong/.virtualenvs/mysite_venv/Extras/lib/python
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/lib-tk
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/lib-old
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/site-packages
/Users/gagaxiaolong/.virtualenvs/mysite_venv/lib/python2.7/site-packages/IPython/extensions

There is a similar question asked 9 months ago : How to debug basic issues configuring django to be served with apache and mod-wsgi? 9个月前有一个类似的问题: 如何调试配置Django与apache和mod-wsgi一起使用的基本问题? , I tried the ways mentioned in the answer but does not work. ,我尝试了答案中提到的方法,但不起作用。

You need to check sys.path in your manage.py file (use pdb for this). 您需要在manage.py文件中检查sys.path (为此使用pdb)。 I think it is because you have a directory structure like this 'parent_dir/mysite/mysite/settings/' and your sys.path has an entry 'parent_dir'. 我认为这是因为您具有这样的目录结构“ parent_dir / mysite / mysite / settings /”,并且您的sys.path具有条目“ parent_dir”。 So system is trying to find settings module in parent_dir/mysite . 因此系统正在尝试在parent_dir/mysite找到settings模块。 Change your manage.py to debug 更改您的manage.py以进行调试

import os
import sys

if __name__ == "__main__":
    import ipdb; ipdb.set_trace()
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.local")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM