Django& WSGI - 导入错误无法找到模块django

时间:2015-01-24 03:27:26

标签: python django apache mod-wsgi

我正在为django项目配置的流浪盒中设置本地开发环境。因此,virtualenv在这一点上真的没有太多用处,所以我只需要在modules.txt中安装所有模块

Django==1.7.3
Pillow==2.7.0
django-angular==0.7.10
djangorestframework
markdown
django-filter
mysqlclient==1.3.4

我的问题是无论我尝试过什么,我都会一直得到

Traceback (most recent call last):
File "/home/vagrant/photoapp.com/photoapp/wsgi.py", line 17, in <module>
from django.core.wsgi import get_wsgi_application
   ImportError: No module named 'django'

应用程序文件夹结构是:

photoapp.com/
├── app
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
├── photoapp
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── requirements.txt
├── templates
└── Vagrantfile

使用标准的wsgi.py:

import os
import sys

sys.path.append("/home/vagrant/photoapp.com")
sys.path.append("/home/vagrant/photoapp.com/photoapp")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "photoapp.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

和虚拟主机文件:

<VirtualHost *:80>
    ServerName      dev.photoapp.com
    ServerAlias     photoapp.com

    DocumentRoot    "/home/vagrant/photoapp.com"

    alias /photos/  "/home/vagrant/photos/"
    alias /static/  "/home/vagrant/photoapp.com/static/"

    <Directory /home/vagrant/photoapp.com/static>
            Require all granted
    </Directory>

    <Directory /home/vagrant/photos>
            Require all granted
    </Directory>

    <Directory /home/vagrant/photoapp.com/photoapp>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

    ErrorLog        /home/vagrant/logs/error.log
    CustomLog       /home/vagrant/logs/access.log combined

    WSGIScriptAlias / /home/vagrant/photoapp.com/photoapp/wsgi.py


</VirtualHost>

但每次我通过dev.photoapp.com访问该网站时(主机文件已被修改),我都会收到错误500和上面的回溯消息。

请注意

  1. import django在python shell中运行
  2. `python manage.py runserver使用url 127.0.0.1:8000
  3. 加载正常
  4. sys.path打印出来:

    [&#39;&#39 ;,    &#39;在/ usr /仓&#39 ;,    &#39; /usr/local/lib/python2.7/dist-packages/django' ;,    &#39; /usr/lib/python2.7' ;,    &#39; /usr/lib/python2.7/plat-x86_64-linux-gnu' ;,    &#39; /usr/lib/python2.7/lib-tk' ;,    &#39; /usr/lib/python2.7/lib-old' ;,    &#39; /usr/lib/python2.7/lib-dynload' ;,    &#39; /usr/local/lib/python2.7/dist-packages' ;,    &#39; /usr/lib/python2.7/dist-packages' ;,    &#39; /usr/lib/python2.7/dist-packages/IPython/extensions']

  5. 所以我可以看到django包可以在路径上访问。但它没有解决Apache访问问题。

    有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试添加:

WSGIPythonPath /home/vagrant/photoapp.com

位于virtualhost配置文件的顶部。