简体   繁体   中英

Error while configuring Django with mod_wsgi on Python inside Anaconda: “ImportError: No module named django.core.wsgi”

I'm having few problems running django+apache2 on ubuntu. Django 1.7, Apache2.4.7

I'm running anaconda stack with 3.4 as main + another environment on python 2

I was trying to set up simple test site and received internal error 500, with below error messages in error log of Apache:

[Sat Jan 10 11:56:16.095032 2015] [:error] [pid 14441:tid 140015774267136] [client 127.0.0.1:59135] ImportError: No module named django.core.wsgi
[Sat Jan 10 12:03:01.895432 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] mod_wsgi (pid=14439): Target WSGI script '/PATH/mysite/wsgi.py' cannot be loaded as Python module.
[Sat Jan 10 12:03:01.895483 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] mod_wsgi (pid=14439): Exception occurred processing WSGI script '/PATH/mysite/wsgi.py'.
[Sat Jan 10 12:03:01.895498 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] Traceback (most recent call last):
[Sat Jan 10 12:03:01.895515 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157]   File "/PATH/mysite/wsgi.py", line 13, in <module>
[Sat Jan 10 12:03:01.895565 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157]     from django.core.wsgi import get_wsgi_application
[Sat Jan 10 12:03:01.895582 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] ImportError: No module named django.core.wsgi

wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

its running ok with

python manage.py runserver

So the issue here is (I think) mod-wsgi wasn't configured on correct python, as default python is ("mod_wsgi/3.4 Python/2.7.6 configured") and django is installed on anaconda 2.7.9 one.

If my guess is good (please let me know what other info would we need for soultion) My question is: how do I recompile mod-wsgi so its linked against 2.7.9 environment from anaconda? Another question would be, can I leave it as it is and just add some directive to link to anaconda libraries? Which folder would be that and where do I put WSGIPythonHome directive then?

Thank you for having a look

Had similar problem, answer was in virtual host file:

WSGIDaemonProcess mysite python-path=/path/to/mysite:/path/to/anaconda3/envs/python2/lib/python2.7/site-packages/
WSGIProcessGroup mysite
WSGIScriptAlias / /path/to/mysite/mysite/wsgi.py

ServerName www.mysite.com
ServerAlias mysite.com
ServerAdmin webmaster@mysite.com

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

ErrorLog /path/to/web/test/error.log

Check this setting in httpd.conf of your Apache:
In your <VirtualHost x:y> section, make sure that value of python-path in WSGIDaemonProcess is including the Python site-packages directory in your Anaconda installation .
Othwerwise, mod_wsgi may not find Django that you've installed for Python inside Anaconda .

For example, if you've installed Anaconda to: /opt/anaconda2
Python site-packages directory may be: /opt/anaconda2/lib/python2.7/site-packages
Therefore, change WSGIDaemonProcess as:
WSGIDaemonProcess YOUR_PROJECT_NAME python-path=/opt/anaconda2/lib/python2.7/site-packages:YOUR_PROJECT_DIRECTORY ......
(Separate each path by colon : )

Note:
1. The version number within the path may change according to your tools' version. Please find similar path and try it.
2. This issue may happen on every Linux distribution (not only Ubuntu), even Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM