Massimo Variolo
Massimo Variolo

Reputation: 4777

Target WSGI script '../wsgi.py' cannot be loaded as Python module

I'm deploying a django project and getting that 500 error (see Server log error).

Where am I doing wrong?

some notes:

Server log error

[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]  SyntaxError: invalid syntax
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Target WSGI script '/new_esmart/esmart2/esmart2/wsgi.py' cannot be loaded as Python module., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Exception occurred processing WSGI script '/new_esmart/esmart2/esmart2/wsgi.py'., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] Traceback (most recent call last):, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart2/esmart2/wsgi.py", line 13, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     import django.core.handlers.wsgi, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     from django.utils.version import get_version, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     from django.utils.lru_cache import lru_cache, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]      fasttypes = {int, str, frozenset, type(None)},, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]                      ^, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]  SyntaxError: invalid syntax, referer: http://192.168.30.17/logistics/alarms/

wsgi.py

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/new_esmart/esmart_env/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/new_esmart/esmart2')
sys.path.append('/new_esmart/esmart2/esmart2')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "esmart2.settings")

# Activate your virtual env
activate_env = os.path.expanduser("/new_esmart/esmart_env/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

application = django.core.handlers.wsgi.WSGIHandler()

httpd.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /new_esmart/esmart2
    ServerName logistica.org
    ServerAlias www.logistica.org
    WSGIScriptAlias / /new_esmart/esmart2/esmart2/wsgi.py
    ErrorLog logs/logistica.org-error_log
    CustomLog logs/logistica.org-access_log common
</VirtualHost>

WSGIPythonPath /new_esmart/esmart2:/new_esmart/esmart_env/lib/python2.7/site-packages

/etc/httpd/conf.d/wsgi.conf

LoadModule wsgi_module modules/mod_wsgi.so

Upvotes: 0

Views: 7935

Answers (2)

Graham Dumpleton
Graham Dumpleton

Reputation: 58563

As pointed out by Daniel, the error indicates your mod_wsgi is compiled for Python 2.6. In order to use Python 2.7 you will need to have mod_wsgi installed with it compiled against Python 2.7. You cannot try and force it to use Python 2.7 by simply referring to your Python 2.7 virtual environment, that isn't how it works.

You can verify what Python version mod_wsgi was compiled with using test application described in:

You will need to uninstall mod_wsgi module and install a version of it built for Python 2.7, either from system packages if available, or built from source code if no system packages available for mod_wsgi for Python 2.7.

I would also recommend that you check the Django documentation on using mod_wsgi and ensure that you use daemon mode as it explains.

Just be aware that the Django documentation still doesn't follow all best practices. Rather than add the site-packages explicitly in python-path, you should use python-home to refer to the virtual environment. See:

Upvotes: 1

vmonteco
vmonteco

Reputation: 15443

You're not using the right python version, but you can specify which one to use in your apache conf by using the WSGIPythonHome directive.

Add

WSGIPythonHome /path/to/your/virtualenv

to your Apache configuration,

This way you can use the interpreter from your virtualenv.

EDIT :

Since you may want to define the python home specifically to your VirtualHost (WSGIPythonHome can't be used in a VirtualHost scope), you could use the WSGIDaemonProcess directive :

<VirtualHost *:80>
  ServerName example.com
  [...]

  WSGIDaemonProcess example.com python-home=/path/to/venv python-path=<python-path>
  WSGIProcessGroup example.com

</VirtualHost>

Upvotes: 1

Related Questions