myproject的wsgi.py无法读取settings.py

时间:2016-01-22 13:05:10

标签: django apache python-3.x centos mod-wsgi

当我用apache打开浏览器时,会出现错误,

ImportError: No module named myproject.settings

我的操作系统是centOS7。 以下代码就是我所做的。

$ cd
$ git clone git://github.com/yyuu/pyenv.git ./.pyenv
$ git clone git://github.com/yyuu/pyenv-virtualenv.git ./.pyenv/plugins/pyenv-virtualenv
$ cat << 'EOF' >> ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
EOF
$ exec $SHELL -l
$ pyenv install --list
$ pyenv install 3.5.1
$ pyenv versions
$ pyenv rehash
$ python --version  # 2.7.5
$ pyenv local 3.5.1
$ python --version  # 3.5.1
$ sudo yum -y install mod_wsgi
$ pip install uwsgi  # 2.0.12
$ pip install django  # 1.9.1

$ mkdir dj
$ cd dj
$ django-admin.py startproject myproject
$ cd myproject
$ python manage.py startapp myapp
$ python manage.py runserver  # it works
$ cd myproject
$ python wsgi.py
Traceback (most recent call last):
  File "wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup()
  File "/home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/jap/.pyenv/versions/3.5.1/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'myproject'

$ ls -l dj/myproject/
-rw-rw-r-- 1 jap jap    0  1月 22 21:09 __init__.py
-rw-r--r-- 1 jap jap 3072  1月 22 20:55 db.sqlite3
-rwxrwxr-x 1 jap jap  252  1月 22 20:55 manage.py
drwxrwxr-x 3 jap jap  116  1月 22 20:55 myapp
drwxrwxr-x 3 jap jap  114  1月 22 21:19 myproject

$ ls -l dj/myproject/myproject/
-rw-rw-r-- 1 jap jap    0  1月 22 20:55 __init__.py
drwxrwxr-x 2 jap jap  118  1月 22 20:55 __pycache__
-rw-rw-r-- 1 jap jap 3175  1月 22 20:55 settings.py
-rw-rw-r-- 1 jap jap  765  1月 22 20:55 urls.py
-rw-rw-r-- 1 jap jap  395  1月 22 20:55 wsgi.py

$ cat dj/myproject/myproject/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()

我根本没有更改settings.py,因此该文件是默认的。 如果有人给我任何建议,我很感激。 谢谢。

这是我的Apache conf文件。

$ cd /home
$ setfacl -m u:apache:--x jap
# setenforce 0
$ echo "Include /etc/httpd/conf/django.conf" >> /etc/httpd/conf/httpd.conf
$ cat /etc/httpd/conf/django.conf

    LoadModule wsgi_module modules/mod_wsgi.so
    WSGIScriptAlias /wsgi /home/jap/dj/myproject/myproject/wsgi.py
    WSGIPythonPath /home/jap/dj/myproject
    WSGIPythonPath /home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages

    <Directory /home/jap/dj/myproject/myproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

1 个答案:

答案 0 :(得分:2)

我不认为Apache连接了几个WSGIPythonPath定义,最后一个定义将覆盖之前的定义。尝试将所有路径放在一行中:

WSGIPythonPath /home/jap/dj/myproject:/home/jap/.pyenv/versions/3.5.1/lib/python3.5/site-packages