使用mod_wsgi的Apache上的Django:设置上的ImportError

时间:2015-08-14 01:57:43

标签: python django apache settings mod-wsgi

我正在使用mod_wsgi在Apache2服务器上提供Django站点。这是我的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

当我使用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

以下是我的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)

但是当我像这样更改目录mysite / mysite时:

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

将代码os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.local")更改为  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.local") 服务器运行正常! 我该如何解决这个问题?

PS:当我使用python manage.py runserver来运行这个项目时,也可以使用它。

这是我的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

9个月前有一个类似的问题:How to debug basic issues configuring django to be served with apache and mod-wsgi?,我尝试了答案中提到的方法,但没有用。

1 个答案:

答案 0 :(得分:0)

您需要检查manage.py文件中的sys.path(使用pdb)。我认为这是因为你有一个像'parent_dir / mysite / mysite / settings /'这样的目录结构,你的sys.path有一个条目'parent_dir'。因此系统正试图在settings中找到parent_dir/mysite模块。将manage.py更改为debug

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)