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

I am trying to execute a small django project with the following Apache configuration:

Apache virtual host 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 problems with STATIC and MEDIA files.

I also checked permissions and tried recursively using 755 and then 777 in the virtualenv site-package directory. This did not work.

But when I try 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 assumed that this is a problem with the Python path since django is installed in my virtualenv. But I added the appropriate python paths to the WSGIDaemonProcess python-path attribute, so I donโ€™t understand why it is not working.

I also assume that I can add the appropriate directory to my Python path in my wsgi.py using the site module , but I would like to understand why the Apache configurations I tried are not enough. Did I miss something?

+6
source share
2 answers

WSGIScriptAlias not have the WSGIScriptAlias directive or an equivalent option, so your application does not actually run in this daemon process group where you installed the virtual environment.

See Using the mod_wsgi daemon mode .

I would also recommend that the application group be set to "% {GLOBAL}" if this is the only application that you use in the daemon process group.

So use:

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

It is also better to use python-home for a virtual environment.

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

Cm:

+15
source

My reputation is not more than 50, so I can not comment, but I would like to share my discovery.

In WSGIDaemonProcess, if you are using Python 3.5, you need to specify exactly how @ graham-dumpleton say

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

set explicitly.

However, if you are using Python 3.4 (or some older version of Python, for example 2.7, as far as I know), you will have to configure it as

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

just like asser did.

Really strange.

+2
source

All Articles