Django Apache wsgi changes python version

末鹿安然 提交于 2019-12-24 08:12:00

问题


I've installed my Django app on an Ubuntu server with Apache2.4.7 and configured it to use py3.5.2 from a virtual environment.

However, from what I can see in the errors, it's starting at 3.5 and defaulting to 3.4.

Please explain why this is happening:

/var/www/venv/lib/python3.5/site-packages
/usr/lib/python3.4

See the full error below:

SyntaxError at /

invalid syntax (forms.py, line 2)

Request Method:     GET
Request URL:    http://intranet.example.com/
Django Version:     1.10.1
Exception Type:     SyntaxError
Exception Value:    

invalid syntax (forms.py, line 2)

Exception Location:     /var/www/intranet/formater/views.py in <module>, line 7
Python Executable:  /usr/bin/python3
Python Version:     3.4.3
Python Path:    

['/var/www/intranet',
 '/var/www/venv/lib/python3.5/site-packages',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/var/www/intranet',
 '/var/www/intranet/venv/lib/python3.5/site-packages']

Here's my apache2.conf file:

WSGIScriptAlias / /var/www/intranet/intranet/wsgi.py
#WSGIPythonPath /var/www/intranet/:/var/www/intranet/venv/lib/python3.5/site-packages

WSGIDaemonProcess intranet.example.com python-path=/var/www/intranet:/var/www/venv/lib/python3.5/site-packages
WSGIProcessGroup intranet.example.com

<Directory /var/www/intranet/intranet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

What am I doing wrong here?


回答1:


The mod_wsgi module for Apache is compiled for a specific Python version. You cannot make it run using a different Python version by pointing it at a Python virtual environment for a different Python version. This is cleared mentioned in the mod_wsgi documentation about use of Python virtual environments at:

  • http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

The only way you can have mod_wsgi run as Python 3.5, if it was original compiled for Python 3.4, is to uninstall that version of mod_wsgi and build/install a version of mod_wsgi compiled for Python 3.5.




回答2:


The source of the problem was Graham Dumpleton's answer. I just want to give some more information in case it helps someone facing the same problem as me.

There's no official repo for Python 3.5.2 in Ubuntu server 14.04. Rather than using some unsupported repo like this one, I compiled Python 3.5.2 from source using this very simple tutorial here. After jumping through many hoops, I couldn't install mod_wsgi for Python 3.5.2 because of a library path that was different.

Having already spent too much time on this, I uninstalled everything: Python, Apache, libraries and installed everything from scratch, using Python 3.4 this time.

It's officially supported for Ubuntu 14.04 and for my project, I noticed no compatibility issues.

So here's my shortlist for what to install: from apt: python3, python3-pip, apache2, apache2-dev, libapache2-mod-wsgi-py3 and from pip: Django, mod-wsgi, virtualenv (if you plan to use a venv).

Then just configure "/etc/apache2/apache.conf", run "apache2ctl configtest" and restart the service. For extra help see this guide here.



来源:https://stackoverflow.com/questions/40946211/django-apache-wsgi-changes-python-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!