running django python 3.4 on mod_wsgi with apache2 - django

Hi I am getting the error below when going to the website url on ubuntu server 14.10 running apache 2 with mod_wsgi and python on django.
My django application uses python 3.4 but it seems to be defaulting to python 2.7, I am unable to import image from PIL and AES from pycrypto.
ImportError at / cannot import name _imaging Request
Method: GET Request URL: Django Version: 1.7.3
Exception Type: ImportError Exception Value: cannot import
name _imaging Exception
Location: /usr/local/lib/python3.4/dist-packages/PIL/Image.py in
, line 63 Python Executable: /usr/bin/python Python
Version: 2.7.6 Python Path: ['/var/www/blabla',
'/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages', '/var/www/blabla',
'/usr/local/lib/python3.4/dist-packages']

I believe that mod_wsgi is compiled against a specific version of python, so you need a py3.4 version of mod_wsgi. You may be able to get one from your os's package repository or you can build one without too much drama. From memory you'll need gcc and python-dev packages (python3-dev?) to build.
OK, quick google, for ubuntu 14.10: sudo apt-get install libapache2-mod-wsgi-py3 should install a py3 version of mod_wsgi (will probably want to remove the existing py2 version).
Adding a shebang line won't do any good as the python interpreter is already loaded before the wsgi.py script is read.

From what I see here your application is using py2 interpreter with py3 compiled modules, which is no-go.
One simple possible solution that comes me in mind is to add or change first line of manage.py to #!/usr/bin/python3. This will tell script to be interpreted with py3.
Next on guess list would be misconfiguration in *.wsgi file or apache config, whichever you are using.

Thanks guys,
I actually fixed the issue myself this morning by running the make install of mod_wsgi with .configure pointing to python3.4.
I think you were right Adam.

Related

upgrading python for django site running on Apache & wsgi

I'm trying to switch a django site from running in python 3.6 to python 3.7 on Ubuntu 16.04. To do so I created a new environment in the same path as the old and have re-installed all of the dependencies.
The Apache server, however, fails with the following error:
Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'
Which error, after a bit of research, seems to be due to the WSGI configuration, as per:
You cannot for example force mod_wsgi to use a Python virtual
environment created using Python 3.5 when mod_wsgi was originally
compiled for Python 2.7. This is because the Python library for the
Python installation it was originally compiled against is linked
directly into the mod_wsgi module. In other words, Python is embedded
within mod_wsgi. When mod_wsgi is used it does not run the command
line python program to run the interpreter and thus why you can’t
force it to use a different Python installation.
from: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
thanks to: Django Apache wsgi changes python version
The OP in the above ended up just wiping the setup clean and starting from scratch. This seems a bit extreme to me, as it seems like I should just be able to re-install/re-configure mod-wsgi to run with python3.7
In this line of thinking, I uninstalled and reinstalled both, libapache2-mod-wsgi-py3 and mod_wsgi. The former, however, just reconfigures to 3.6 automatically, as seen below:
root#server1:~# dpkg -S mod_wsgi
libapache2-mod-wsgi-py3: /usr/lib/apache2/modules/mod_wsgi.so
libapache2-mod-wsgi-py3: /usr/lib/apache2/modules/mod_wsgi.so-3.6
and the mod_wsgi installation fails with:
RuntimeError: The 'apxs' command appears not to be installed or is not executable.
How can I re-build wsgi with python 3.7?

ImportError: No module named 'django' despite having it installed running apache2 server

I know that many have posted similar question however I tried most solution without success.
I'm trying to host a webpage with apache2 and django in python3.
In the error log I found ImportError: No module named 'django' when accessing the wsgi.pyfile, where I also added import sys, sys.version to confirm which python version is used and from the error log I can see that I'm running following python version 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609].
When I run python3.5 I see that I uses the same python version and here I can run import django without any error!
EDIT: I checked django.__file__and saw that it was located in /home/USERNAME/.local/lib/python3.5/site-packages/django/init.py and that path /home/USERNAME/.local/lib/python3.5/site-packages wasn't in the sys.path that tried to run django. But adding it with sys.path.append(path) didn't help :(
Any thoughts what I might have messed up?
If you have Setup the whole configuration in VirtualEnv then , i suggest you to activate it by,
source /location to /env/bin activate
pip3 install django=version_id
or pip install django=version id
if you want to pass version id then its good or it will install the latest django from your repo.
Now test Django Version there.
Hope you will no get the error.
Location - means the path where env will be located in project directory, if you have followed the standard installation process of django or else you don't need, and version id- vesion of django framework.
I finally understood how to solve it! first I had to run pip3 uninstall django then run sudo pip3 install django.

Virtual environment not recognized in WSGIPythonPath

I'm trying to set up my Django project in production by using a virtual environment, using the documentation:
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#using-a-virtualenv
So, in my configuration I have:
WSGIPythonPath /srv/zboss/zboss:/srv/zboss/venv/lib/python3.4/site-packages
I restart Apache and I get the following error:
Internal Server Error: /prot/
InvalidTemplateLibrary at /
Invalid template library specified. ImportError raised when trying to load 'core.templatetags.wiki_formatter': No module named parse
Request Method: GET
Request URL: http://babylon/prot/
Django Version: 1.10.1
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path: ['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/srv/zboss/zboss']
However the error is something because that library is not used anymore in Python3. I see the python path and no trace of my virtual environment directory.
Your mod_wsgi is compiled for Python 2.7. You cannot point it at a virtual environment for Python 3.4. You must uninstall mod_wsgi and install a mod_wsgi version built for Python 3.4, the same version as your virtual environment.

ImportError when I try to run a .py script using CherryPy

I'm using python 2.7 and cherrypy 3.7, and am running them on Ubuntu 14.04.
I have a simple hello world file that I am trying to run with the command:
python HelloWorld.py
This results in the following error:
ImportError: No module named cherrypy
Please explain why Python couldn't find the module.
Ubuntu is going to have a native version of python 2.6 for instance. So you'll need the full path to your python file. Try to run this and if it fails search your system for a python2.7 folder.
/usr/local/bin/python2.7 tut01.py
Hope this helps!

Django not loading Module Pycrypto into model, will load into shell

I am using a Virtualenv on my Django install and have loaded in Pycrypto using
pip install pycrypto
after sourcing my environment.
When I try to import Crypto into my model, I get an import error, however if load the shell using
manage.py shell
it does load in, with out an issue.
I am pretty sure that django is running from the virtualenv as is it running 1.3.1 which more current than what is installed on the path and I have installed South via pip which is working.
I have also tried to install Pycrypto using
setup.py install
Any thoughts on how I fix/debug this?
Thanks
Edit:
I am relatively new to Django and Python so it could be something simple
Edit 2:
Hey Issac,
Is there any way that you can find all the modules getting loaded in?
I don't understand why it wouldn't work in browser but would work in the interactive shell? I am not saying that it can't, i just don't know if/how that could happen.
The import error as requested:
ImportError at /
No module named Crypto
Request Method: GET
Request URL: http://*/
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:
No module named Crypto
Exception Location: /home/*************/project/app/models.py in <module>, line 1
Python Executable: /home/*************/env/bin/python
Python Version: 2.6.6
Python Path:
['/usr/local/dh/passenger/lib/phusion_passenger/wsgi',
'/home/*************/env/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg',
'/home/*************/env/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg',
'/home/*************/env/src/django-debug-toolbar',
'/home/*************/env/lib/python2.6',
'/home/*************/env/lib/python2.6/plat-linux2',
'/home/*************/env/lib/python2.6/lib-tk',
'/home/*************/env/lib/python2.6/lib-old',
'/home/*************/env/lib/python2.6/lib-dynload',
'/usr/lib/python2.6',
'/usr/lib64/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-tk',
'/home/*************/env/lib/python2.6/site-packages',
'/home/*************',
'/home/*************/project']
Server time: Tue, 3 Jan 2012 20:08:19 +1100
Just before the line which causes the import error insert this code:
import sys
assert False, sys.path
Compare both results.
In the shell (where you can import the module). You can do this:
import Crypto
assert False, Crypto.__file__
Is the directory of this result in sys.path of your webserver?