Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 572
vmonteco Avatar asked Aug 04 '16 01:08

vmonteco


Video Answer


3 Answers

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
like image 77
Marijus Avatar answered Oct 22 '22 15:10

Marijus


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
like image 29
Graham Dumpleton Avatar answered Oct 22 '22 14:10

Graham Dumpleton


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.

like image 22
Valorad Avatar answered Oct 22 '22 15:10

Valorad