使用mod_wsgi在Apache上运行Django时的ImportError

时间:2015-04-21 02:46:41

标签: python django apache vagrant mod-wsgi

我已经按照Django教程设置了一个与mod_wsgi一起运行的项目,但在尝试访问我的网站时我遇到了一个ImportError。我通过Vagrant运行Apache服务器。

这是Apache日志的实际错误:

mod_wsgi (pid=4268): Target WSGI script '/vagrant/myapp/myapp/myapp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=4268): Exception occurred processing WSGI script '/vagrant/myapp/myapp/myapp/wsgi.py'.
Traceback (most recent call last):
   File "/vagrant/myapp/myapp/myapp/wsgi.py", 
     application = get_wsgi_application()
   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
     django.setup()
   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
     apps.populate(settings.INSTALLED_APPS)
   File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
     app_config.import_models(all_models)
   File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 197, in import_models
     self.models_module = import_module(models_module_name)
   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
   File "/vagrant/myapp/myapp/myapp/comment/models.py", line 3, in <module>
     from myapp.post.models import Post
   File "/vagrant/myapp/myapp/myapp/post/models.py", line 4, in <module>
     from myapp.item.models import Item
   File "/vagrant/myapp/myapp/myapp/item/models.py", line 5, in <module>
     from myapp.third_party.otherapp.service import SomeService
   File "/vagrant/myapp/myapp/myapp/third_party/otherapp/service.py", line 1, in <module>
     from myapp.third_party.otherapp.api import SomeApi
ImportError: No module named api

这是我的wsgi.py:

import os, sys

sys.path.insert(0, '/vagrant/myapp/myapp/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

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

这是我在站点中的虚拟主机的.conf文件:

<VirtualHost *:80>
  ServerName 192.168.33.3
  ServerAlias vagrant.localhost
  DocumentRoot /vagrant

  WSGIScriptAlias / /vagrant/myapp/myapp/myapp/wsgi.py

  WSGIDaemonProcess myapp python-path=/vagrant/myapp/myapp
  WSGIProcessGroup myapp

  <Directory /vagrant>
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all

    <Files /vagrant/myapp/myapp/myapp/wsgi.py>
      Order allow,deny
      Allow from all
    </Files>
  </Directory>
</VirtualHost>

似乎我的python路径不正确,但我尝试将我的app目录添加到wsgi.py中无济于事。导入指定的模块在Django shell中工作正常。还有什么我不想做的工作吗?

编辑:这是目录结构 -

vagrant
-- Vagrantfile
-- myapp
----- myapp
-------- manage.py
-------- myapp
----------- urls.py
----------- settings.py
----------- wsgi.py
----------- post
----------- item
----------- third_party
----------- ...

0 个答案:

没有答案