繁体   English   中英

Django 1.9 Apache错误导入django.core.handlers.wsgi ImportError:没有名为“ django”的模块

[英]Django 1.9 Apache error import django.core.handlers.wsgi ImportError: No module named 'django'

我尝试将Django项目部署到Apache。 但是我得到一个错误500response。 在日志中,我获得了有关Django缺失的信息。 我正在使用virtualenv运行此项目。 这是我第一次尝试部署Django项目。 从我的经验中,我知道我可能缺少一些简单的东西。 我一直在这个网站上寻找解决方案,但它们是针对Django和python以前的版本的。 他们不为我工作。

这是我的Apache test.conf

WSGIScriptAlias / /home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py 
WSGIDaemonProcess localhost python-path=/home/mariusz/Dokumenty/Projekty/zalien:/home/mariusz/Dokumenty/Projekt/envy/lib/python3.5/site-packages

<Directory /home/mariusz/Dokumenty/Projekty/zalien/zalien>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

这是我的wsgi.py

import os
import sys
import site
from django.core.wsgi import get_wsgi_application
application = django.core.handlers.wsgi.WSGIHandler()

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/mariusz/Dokumenty/Projekty/envy/local/lib/python3.5/site-packages')


# Add the app's directory to the PYTHONPATH
sys.path.append('/home/mariusz/Dokumenty/Projekty/zalien')
sys.path.append('/home/mariusz/Dokumenty/Projekty/zalien/zalien')

os.environ['DJANGO_SETTINGS_MODULE'] = 'zalien.settings'

# Activate your virtual env
activate_env=os.path.expanduser("/home/mariusz/Dokumenty/Projekty/envy/bin/activate_this.py")
exec(activate_env, dict(__file__=activate_env))

错误记录

[Mon May 16 09:44:28.368084 2016] [wsgi:error] [pid 7418:tid 139640747427584] mod_wsgi (pid=7418): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Mon May 16 09:44:28.368132 2016] [wsgi:error] [pid 7418:tid 139640747427584] mod_wsgi (pid=7418): Call to 'site.addsitedir()' failed for '/home/mariusz/Dokumenty/Projekty/zalien/'.
[Mon May 16 09:44:28.369458 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] mod_wsgi (pid=7418): Target WSGI script '/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py' cannot be loaded as Python module.
[Mon May 16 09:44:28.369493 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] mod_wsgi (pid=7418): Exception occurred processing WSGI script '/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py'.
[Mon May 16 09:44:28.369724 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] Traceback (most recent call last):
[Mon May 16 09:44:28.369752 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494]   File "/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py", line 23, in <module>
[Mon May 16 09:44:28.369758 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494]     from django.core.wsgi import get_wsgi_application
[Mon May 16 09:44:28.369781 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] ImportError: No module named 'django'

权限

-rw-rw-r-- 1 mariusz www-data   295 May  9 14:50 app.json
-rw-rw-r-- 1 mariusz www-data 51200 May 13 09:53 db.sqlite3
drwxrwxr-x 4 mariusz www-data  4096 May 13 10:06 games
-rwxrwxr-x 1 mariusz www-data   249 May  9 14:50 manage.py
drwxrwxr-x 4 mariusz www-data  4096 May 13 10:06 portal
-rw-rw-r-- 1 mariusz www-data    39 May  9 14:50 Procfile
-rw-rw-r-- 1 mariusz www-data    45 May  9 14:50 Procfile.windows
-rw-rw-r-- 1 mariusz www-data  1368 May  9 14:50 README.md
-rw-rw-r-- 1 mariusz www-data   298 May  9 14:50 requirements.txt
-rw-rw-r-- 1 mariusz www-data    13 May  9 14:50 runtime.txt
drwxrwxr-x 5 mariusz www-data  4096 May  9 14:50 static
drwxrwxr-x 4 mariusz www-data  4096 May  9 14:50 templates
drwxrwxr-x 4 mariusz www-data  4096 May 13 10:06 userprofile
drwxrwxr-x 3 mariusz www-data  4096 May 16 11:50 zalien

我有一些使用django的apache服务器,安装它们时很痛苦。 因此,使用静态文件的最低工作配置为:

WSGIPythonPath /home/mariusz/Dokumenty/Projekty/zalien

<VirtualHost *:80>
    WSGIScriptAlias / /home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py

    <Directory /home/mariusz/Dokumenty/Projekty/zalien/zalien>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    # Django static files, here you must specify your own path's
    Alias /static/ /var/www/djangointra/static/
    <Directory "/var/www/djangointra/static">
        Require all granted
    </Directory>
</VirtualHost>

另外,您不应在django项目中修改wsgi.py文件。 Apache配置足以启动您的Web服务器。

编辑 :wsgi.py

"""
WSGI config for djago_api project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

永远不会太晚。 也许这会帮助别人。 我有同样的错误,它通过2次更改得以解决。 以下是工作配置。 我有Django 1.10,Python 3.5和Apache 2.4

wsgi.pi

import os, sys, site

from django.core.wsgi import get_wsgi_application


site.addsitedir("/home/mariusz/Dokumenty/Projekt/envy/lib/python3.5/site-packages")

sys.path.append("/home/mariusz/Dokumenty/Projekty/zalien")
sys.path.append("/home/mariusz/Dokumenty/Projekty/zalien/zalien")

activate_this = "/home/mariusz/Dokumenty/Projekty/envy/bin/activate_this.py"
with open(activate_this) as f:
    code = compile(f.read(), activate_this, "exec")
    exec(code, dict(__file__=activate_this))

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

application = get_wsgi_application()

阿帕奇test.conf

<VirtualHost *:80>
       WSGIDaemonProcess zalien processes=2 threads=12 python-home=/home/mariusz/Dokumenty/Projekt/envy/ python-path=/home/mariusz/Dokumenty/Projekty/zalien:/home/mariusz/Dokumenty/Projekt/envy//lib/python3.5/site-packages
       WSGIProcessGroup zalien
       WSGIScriptAlias / /home/mariusz/Dokumenty/Projekty/zalien/wsgi.py process-group=zalien
       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined

       Alias /static/ /var/www/html/static/
         <Directory /var/www/html/static/>
             Require all granted
         </Directory>

         <Directory /home/mariusz/Dokumenty/Projekty/zalien>
            <Files wsgi.py>
              Require all granted
            </Files>
         </Directory>
</VirtualHost>

暂无
暂无

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

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