upgrading python for django site running on Apache & wsgi - django

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?

Related

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.

django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3

I tried re-installing python 2 in my environment.
Re-installed pip, setuptools
when I try to run python manage.py runserver it throws the error
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3
I tried installing sqlite 3 , its already installed
I tried installing pysqlite2 , its already installed
both python 2 and installed in my ubuntu 16.04, django version = 1.8.X
Still unaware of what cause the error?
For Ubuntu users:
Download the source file of the Python version that you need, from source. Extract the folder somewhere, cd into the folder,
./configure
make
make install
this should fix your issue.
(This can be done outside VE, i just had the same issues with python 3.4)

running django python 3.4 on mod_wsgi with apache2

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.

django wont work with python3.3.3

Hello I recently installed the django on my Mac OS X 10.9 using the pip install Django.
The installation was successful, now my python3.3.3 is installed in the following directory:
/usr/local/lib/python3.3/site-packages
Inside this directory there is a directory for django so I am assuming that the installation was success on python 3.
The problem arise when I am trying the following procedure:
django-admin.py startproject mysite
The above command creates with success the directory with the files
Then on this command:
python manage.py runserver
or
python3 manage.py runserver
The error is:
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django'
Moreover, I do find a solution but wont work. Was to change the first comment of the file django-admin.py to link the compiler to the correct version of python, which was fine, but the problem is the same.
Any help?
Check out this tutorial on python development on OSX: http://hackercodex.com/guide/python-development-environment-on-mac-osx/
Virtualenv allows you to create sandboxes where you can install different python versions and python libraries without interfering with other virtual environments or with your system python version/libraries.
Your specific problem though, seems to be that Django was not added to your system path, so python doesn't know where to find the django files. It can be risky to add new things to your system path because it could end up conflicting with existing libraries.
If you use virtualenv, each environment will have its own "system" path and the python executable in that environment will know where to find the django files.

importerror: No module named django

I installed python 2.6 alongside my mac's 2.5.2 version. As soon as I did, python2.6 manage.py runserver failed because it couldn't find django.core.management.
From a shell, import django returns importerror: No module named django.
Why?
Did you reinstall Django?
This happens when I install side by side versions of Python on Gentoo. Whenever I install a new version, I have to either reinstall the new ones or make a symlink to the old site-packages.
Because each installation of Python uses its own directory to store libraries. On a Mac, they are in /Library/Python/2.x/site-packages/. Presumably you originally installed Django in the 2.5 directory, but it isn't yet in the 2.6 one. You can symlink it there if you want to, or reinstall it using the new version.
Add site-packages to PYTHONPATH:
export PYTHONPATH="/home/jerome/bin/django-1.1/lib/python2.6/site-packages:$PYTHONPATH"
Worked on Ubuntu, with a python/django virtual environment using virtualenv and pip.
Source: http://benfsayer.com/importerror-no-module-named-django-core-management/
I use Bitnami's Django installer, and this happened for me when I wasn't in their custom shell, which I believe sets related python path environment variables. I ran ./use_djangostack in the root of the Bitnami package and then was successful running the server again.