Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'

南楼画角 提交于 2019-12-18 12:29:24

问题


I'm trying to serve a little django project with the following Apache configuration :

Apache virtualhost configuration :

<VirtualHost *>
    ServerName servername

    [...]

    <Directory "/path/to/project/project">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess project python-path=/path/to/project:/path/to/Envs/venv/lib/python3.5/site-packages                           
    WSGIScriptAlias / /path/to/project/project/wsgi.py

</VirtualHost>

I also have the following wsgi.py :

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")
application = get_wsgi_application()

I have no problem to serve STATIC files and MEDIA files.

I also checked permissions and tried to recursively use 755, then 777 to my virtualenv's site-package directory. It didn't work.

But when trying to reach the root of my site I get the following :

from django.core.wsgi import get_wsgi_application
ImportError: No module named 'django'

I guessed that it was a Python path related problem since django is installed in my virtualenv. But I added the relevant python paths to the WSGIDaemonProcess's python-path attribute so I don't get why it doesn't work.

I also guess I could add the relevant directory to my Python path in my wsgi.py by using the site module, but I'd like to understand why the Apache configuration I tried isn't enough. Did I miss something?


回答1:


You are missing a WSGIProcessGroup directive or equivalent option on WSGIScriptAlias, so your application isn't actually being run in that daemon process group where you have set the virtual environment.

See Using mod_wsgi daemon mode

I would also recommend ensuring application group is set to '%{GLOBAL}' if that is the only application you are running in the daemon process group.

Thus use:

WSGIScriptAlias / /path/to/project/project/wsgi.py \
    process-group=project application-group=%{GLOBAL}

Also better to use python-home for the virtual environment.

    WSGIDaemonProcess project python-path=/path/to/project \
        python-home=/path/to/Envs/venv

See:

  • http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html



回答2:


My rep is not over 50 so I cannot comment, but I'd like to share my discovery.

In WSGIDaemonProcess, if you are using Python 3.5, you need to set exactly as @graham-dumpleton say, with

python-home=/path/to/Envs/venv

set explicitly.

However, if you are using Python 3.4 (or some older version Python like 2.7 as far as I know), you'll have to configure it as

python-path=/path/to/project:/path/to/Envs/venv/lib/python3.4/site-packages

just like what the asker did.

Really weird.




回答3:


For me the problem was I had mod wsgi installed for python2. I had to reinstall it for python3:

sudo apt-get install libapache2-mod-wsgi-py3


来源:https://stackoverflow.com/questions/38756969/apache-with-virtualenv-and-mod-wsgi-importerror-no-module-named-django

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