Reputation: 10812
So, I have two different versions of Python on my CentOS machine - 2.7 and 3.4. What I want is to simply run my Django application using Python 3.4 and not that old outdated garbage. To do this I created a virual environment, so that my folder structure now looks like this
\vwrapper
\bin
\include
\lib
\lib64
...
I created this virtual environment using Python 3.4. After that I activated this environment like so:
$ source vwrapper/bin/activate
(vwrapper) [root@...]
So, I clearly see, that I now inside this virtual environment. I now even can check, that indeed I'm using python 3.4:
(vwrapper) [root@...] # python --version
Python 3.4.5
At this stage everything looks good. Then I installed django:
(vwrapper) [root@...] # pip install django
Again it's ok. In \vwrapper\bin I can now see some django tools. I then use one of these tools to create my first django application:
(vwrapper) [root@...] # bin/django-admin startproject accent
Looks good. I go to wsgi.py and make a little correction:
import os, sys
path = '/var/www/vwrapper/accent'
if path not in sys.path:
sys.path.append(path)
Then I go to settings.py and also make a little correction:
ALLOWED_HOSTS = ['*']
Great! The only thing that remains to be done is apache virtual host. So, I create this virtual host and restart apache:
(vwrapper) [root@...] # apachectl restart
Fantastic! When I go to to this host in my browser, I see my first Django application. So far, so good. But the thing is, I see that old Python 2.7 is used, I checked it by returning to browser sys.version:
def index(request):
return HttpResponse(sys.version)
So that, when I restart apache again and go to this page, I see in browser this message:
2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
What the heck? I tried to fix it by putting
#!/usr/bin/python3.4
to every .py script, but it does not help. I hope someone knows what may be wrong with that. I spent two days to make it all work, but still I'm in trouble. I do not know what I'm missing. Thanks!
PS. This how my virtual host configuration looks like in Apache:
<VirtualHost *:8080>
ServerName django.localhost
WSGIScriptAlias / "/var/www/vwrapper/accent/accent/wsgi.py"
<Directory "/var/www/vwrapper/accent/accent">
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
So, as you can see, I don not mention Python in this config, but probably I should (though, I do not know exactly how).
EDIT
I made some corrections to my virtual host configuration, so that it know looks like:
WSGIPythonPath /var/www/vwrapper/accent:/var/www/vwrapper/lib/python3.4/site-packages
<VirtualHost *:8080>
ServerName django.localhost
WSGIScriptAlias / "/var/www/vwrapper/accent/accent/wsgi.py"
<Directory "/var/www/vwrapper/accent/accent">
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
and I also edited my wsgi.py script:
import site
site.addsitedir('/var/www/vwrapper/lib/python3.4/site-packages')
Restarted Apache, but to no avail. It seems like I did a dozen of steps to use Python 3.4, but still miss one some secret step.
Upvotes: 2
Views: 165
Reputation: 31260
WSGIScriptAlias / "/var/www/vwrapper/accent/accent/wsgi.py"
You are using mod_wsgi to run your Python. Do you use a mod_wsgi that is compiled to use Python 3?
For instance, on Ubuntu, there exist packages libapache2-mod-wsgi
which uses Python 2, and libapache2-mod-wsgi-py3
which uses Python 3. But I don't know about CentOS.
If you can compile mod-wsgi yourself, see https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#multiple-python-versions .
Upvotes: 2
Reputation: 5017
In the same setup with two Python versions I am using following wsgi.py which is working correctly. Did you activate virtualenv inside of wsgy.py? I think the problem goes from this file. Compare it with yours.
import os, site, sys
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/virtualenvs/projectenv/local/lib/python3.5/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/home/ubuntu/project')
sys.path.append('/home/ubuntu/project/project')
# Assign settings module for starting
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
# Activate your virtual env
activate_env=os.path.expanduser("/home/ubuntu/virtualenvs/projectenv/bin/activate_this.py")
exec(open(activate_env).read())
# Load environ variables
from prepare_server import prepare_server
prepare_server()
application = get_wsgi_application()
Upvotes: 0
Reputation: 7260
You have to ensure your Apache is accessing the correct virtualenv. Activate is only working for current shell. Most likely your Apache is having a config file somehow mapping your request to your application.
Upvotes: 0