1
votes

I am encountering a "No module named 'encodings'" error. It appears to me that Python virtual env was not correctly loaded. However I also included wsgi file just in case.

0> Error I got:

[Mon Mar 12 16:49:44.919934 2018] [:info] [pid 6546] mod_wsgi (pid=6546): Starting process 'abcd_server' with uid=501, gid=501 and threads=5.
[Mon Mar 12 16:49:44.920494 2018] [:info] [pid 6546] mod_wsgi (pid=6546): Python home /var/www/m.abcd.com/venv.
[Mon Mar 12 16:49:44.920556 2018] [:info] [pid 6546] mod_wsgi (pid=6546): Initializing Python.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
  ModuleNotFoundError: No module named 'encodings'

1> Installation:

  • Amazon EC2 linux instance (CentOS);
  • installed Python 3.6: sudo yum install python36
  • installed wsgi: sudo yum install mod24_wsgi-python36.x86_64

2> Verify:

  • httpd: Apache/2.4.27 (Amazon)
  • python3: Python 3.6.2
  • httpd uses modules/wsgi.so ldd wsgi.so:

    linux-vdso.so.1 =>  (0x00007ffe71de8000)
    libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 (0x00007f49a0ecf000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f49a0cb3000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f49a0aaf000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007f49a08ac000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f49a05aa000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f49a01e6000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f49a1616000)
    

3> Web site setup:

  • Create a new user "python_user:python_user" (uid=501);
  • Create a directory /var/www/m.abcd.com and owned by python_user:python_user. Also subdirectories are also owned by python_user:python_user
  • Under /var/www/m.abcd.com, create python-src, venv, html, logs, applogs etc.
  • python-src contains setup.py and application source files
  • venv is setup by running python3 -m venv venv (Edit: not python2) (setup virtual env)
  • inside python-src, pip install -e . to add app dependencies to virtual env.

4> httpd config:

<VirtualHost *:443>
  ......
  WSGIDaemonProcess abcd_server user=python_user group=python_user threads=5 python-home="/var/www/m.abcd.com/venv"
  WSGIProcessGroup abcd_server
  WSGIScriptAlias / "/var/www/m.abcd.com/python-src/src/abcd_server.wsgi"

  <Directory "/var/www/m.abcd.com/html">
    Require all granted
  </Directory>

  <Directory "/var/www/m.abcd.com/python-src/src">
    Require all granted
  </Directory>
  <Directory "/var/www/m.abcd.com/conf">
    Require all granted
  </Directory>
  <Directory "/var/www/m.abcd.com/applogs">
    Require all granted
  </Directory>
  <Directory "/var/www/m.abcd.com/data">
    Require all granted
  </Directory>

  ......
</VirtualHost>

5> abcd_server.wsgi

imort os
os.environ['P2SERVER_APP_CFG'] = '/var/www/m.abcd.com/conf/abcd_conf.yaml'
from p2events.app import app as application

6> (Edit) when activated the virtual env, in the interactive python session, sys.prefix = /var/www/m.abcd.com/venv. So that seems ok.

7> (Edit, follow up with Graham Dumpleton's comment about wsgi version). WSGI version seems the issue. It is 3.5 and the current version is 4.6.2. I will try to install the new one as Graham Dumpleton suggested. Thank you, Graham!

[Sun Mar 11 22:16:55.898442 2018] [mpm_prefork:notice] [pid 2465] AH00163: Apache/2.4.27 (Amazon) OpenSSL/1.0.2k-fips mod_wsgi/3.5 Python/3.6.2 configured -- resuming normal operations
1
Did you really mean python2 -m venv venv. That would be using Python 2 to create the virtual environment, which obviously isn't going to work with mod_wsgi compiled for Python 3.Graham Dumpleton
I am pretty sure I did not do python2 thing. After activating the virtual env, I could do python --version and get python 3.6BSharer App - Share Books
as of ffeast mentioned the stackoverflow question, they are totally different - even though it shows the same error message. Here, python 2 or 3 are all working fine. The problem seems HTTPD loading python virtual environment failed.BSharer App - Share Books
What version of mod_wsgi is that even? At a guess I would suggest it is ancient. Would suggest use pip install method to install mod_wsgi and configure Apache to use it. See pypi.python.org/pypi/mod_wsgiGraham Dumpleton

1 Answers

0
votes

Here is what I found with Graham Dumpleton's help in the comments section.

The issue is that the default installation of mod_wsgi from Amazon Linux is very old 3.5. After installing mod_wsgi (currently 4.6.2) using pip-3.6 and link it to httpd, the issue was resolved.