wsgi.py of myproject cannot read settings.py

爷,独闯天下 提交于 2020-01-06 19:56:17

问题


When I open the brower with apache, it occurs an error,

ImportError: No module named myproject.settings

My OS is centOS7. And following codes are what I did.

$ 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()

I didn't change settings.py at all, so the file is default. If someone gives me any pieces of advice, I am grateful. Thank you.

This is my Apache conf file.

$ 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:


I don't think Apache concatenates several WSGIPythonPath definitions, the last one will overwrite previous definition. Try putting all paths in one line instead:

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


来源:https://stackoverflow.com/questions/34947428/wsgi-py-of-myproject-cannot-read-settings-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!