4

After many strenuous hours I am still not able to get a virtual environment for a Django project run.

My Django project works locally (with runserver, no apache, with python 2.7 and venv). It also has been working before on the development server with Apache but without venv and with default python version 2.7.

Now I am trying to use a virtual environment that uses python 3.7.


STEP 1:

I created a virtual environment with python 3.7:

sudo virtualenv -p /usr/bin/python3.7 /var/www/abcd.com/venvs/venv_core
cd /var/www/abcd.com/venvs/venv_core/bin
source activate
sudo ./pip install --upgrade pip
cd ../../../
sudo venvs/venv_core/bin/pip install -r requirements.txt

(--system-site-packages is turned off by default)

The python version 3.7 is already included in Ubuntu 18.04.1. So I used it. (In a prior try to compile python 3.7 myself I got completely lost and had to roll back to a prior snapshot of the virtual machine.)

system:

Ubuntu 18.04.1 LTS "bionic"
included Python versions: 2.7.15rc1 (sys default), 3.6.5 (sys default), 3.7.0b3
Apache/2.4.29 (Ubuntu)

https://docs.python.org/3/library/venv.html https://virtualenv.pypa.io/en/stable/reference/

This venv seems to be created correctly.

I also adapted the wsgi config (see below).

http://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#

But when I restart the apache I get the error:

ImportError: No module named site

https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#virtual-environment-and-python-version

Django Apache and Virtualenv ImportError: No module named site

https://serverfault.com/questions/285229/python-django-wsgi-apache-importerror-no-module-named-site

It seems that I have to compile the mod_wsgi with python 3.7.0.


STEP 2:

Then I tried to compile mod_wsgi with python 3.7:

sudo apt-get remove libapache2-mod-wsgi
sudo apt-get build-dep libapache2-mod-wsgi
sudo mkdir /tmp/wsgi
cd /tmp/wsgi
sudo apt-get source libapache2-mod-wsgi
cd mod-wsgi-4.5.17
sudo dpkg-buildpackage -rfakeroot -b
sudo dpkg -i /tmp/wsgi/libapache2-mod-wsgi-py3_4.5.17-1_amd64.deb

The I get the following error (detailed log below):

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

mod_wsgi is compiled in one version and running in a different version even after following the given steps

https://bugs.launchpad.net/ubuntu/+source/libapache2-mod-python/+bug/1073147/comments/8

Django / Apache / mod_wsgi: No module named importlib

https://code.google.com/archive/p/modwsgi/wikis/CheckingYourInstallation.wiki#Python_Installation_In_Use


At the end I don't know what I can do any more to get the system up and running. I don't know much about Linux and I'm not a Python expert.

1) Do I have to compile mod_wsgi for python 3.7 or is there a simpler way?

2) Did I compile mod_wsgi correctly? If not, how do I have to do it?

3) Is dev.conf properly configured? If not, what do I have to change?

Please can anyone help me with that?


dev.conf (Apache config: etc/apache2/sites-available/dev.conf):

Define server_name abcde.com
WSGIPythonHome /var/www/${server_name}/venvs/venv_core/lib/python3.7/
...
<IfModule mod_ssl.c>
    WSGIDaemonProcess ${server_name} processes=2 threads=15 display-name=%{GROUP} python-path=/var/www/${server_name}:/var/www/${server_name}/venvs/venv_core/lib/python3.7:/var/www/${server_name}/venvs/venv_core/lib/python3.7/site_packages
    WSGIProcessGroup ${server_name}
    WSGIScriptAlias / /var/www/${server_name}/index.wsgi
...

index.wsgi:

import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application

sys.path.append('/var/www/abcde.com/')
sys.path.append('/var/www/abcde.com/abcde')
os.environ['DJANGO_SETTINGS_MODULE'] = 'abcde.settings'

try:
    application = get_wsgi_application()
except Exception:
    print 'handling WSGI exception'
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)

error.log:

Current thread 0x00007f2e8992dbc0 (most recent call first):
[Wed Aug 08 21:21:05.853747 2018] [core:notice] [pid 6566:tid 139837853326272] AH00051: child pid 6985 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:05.853852 2018] [mpm_event:notice] [pid 6566:tid 139837853326272] AH00491: caught SIGTERM, shutting down
[Wed Aug 08 21:21:05.995273 2018] [mpm_event:notice] [pid 7007:tid 139971756051392] AH00489: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.0g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Wed Aug 08 21:21:05.995471 2018] [core:notice] [pid 7007:tid 139971756051392] AH00094: Command line: '/usr/sbin/apache2'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
...
Current thread 0x00007f4db6cc4bc0 (most recent call first):
[Wed Aug 08 21:21:06.996663 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7009 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:06.997392 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7010 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:06.997804 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7011 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:06.997870 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7012 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
...
Harald Mandl
  • 91
  • 1
  • 8
  • If you are willing to use Python 3.6 instead of 3.7, then you can install the [`libapache2-mod-wsgi-py3`](https://packages.ubuntu.com/bionic/libapache2-mod-wsgi-py3) package on Ubuntu 18.04 and you won't have to compile it. – Alasdair Aug 09 '18 at 10:21
  • Great, that fixed it! Thank you, Alasdair! – Harald Mandl Aug 10 '18 at 11:03
  • Just to resume: I recognized that python 3.7 is "only" a beta version. Therefore I prefer to use 3.6 anyway. And for 3.7 there is already a compiled mod_wsgi available, as Alasdair mentioned. Using this, it was no problem any more. see: https://packages.ubuntu.com/bionic/libapache2-mod-wsgi-py3 see: https://stackoverflow.com/questions/19344252/how-to-install-configure-mod-wsgi-for-py3 use: sudo apt-get install libapache2-mod-wsgi-py3 – Harald Mandl Aug 10 '18 at 11:08
  • Glad that you got it working using Python 3.6. Just to clarify, Python 3.7 was released in June 2018, so it is no longer beta. But the default Python 3 for Ubuntu 18.04 will always be Python 3.6, so it's easier to stick with that if you don't need any specific features from Python 3.7. – Alasdair Aug 10 '18 at 12:08
  • 1
    And what for those requiring those specific features from Python3.7? – Fejs Dec 28 '18 at 17:39

0 Answers0