Anaconda3 - No modules named 'encodings'

256 views
Skip to first unread message

Peer Boerner

unread,
Apr 5, 2019, 5:42:20 PM4/5/19
to modwsgi
I am trying to get mod_wsgi working with Apache 2.4.6 on RHEL.  I have access to the sudo command but not root. 

The net-net: No matter what I have tried, I can not get mod_wsgi to see the associated python modules for the virtual environment that it was configured for using Anaconda 3. 

The good news: According to the Apache error log files, mod_wsgi has loaded successfully.
The bad news is that I receive numerous messages: Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'

I've spent two days trying what I believe is every combination of configurations I've seen from the various posts I've read on virtual environments, all to no avail.

I even pasting the output of mod_wsgi module-config into the httpd.conf file.  The LoadModule statement fails with a permissions error even though all of the files in the entire directory have been set to 777.

The only way I have been able to get the mod_wsgi module to load has been to:
1. Build mod_wsgi from source in the virtual environment of my application
2. Use the make install command to load the module into the apache modules directory
3. Copy the libpython3.6m.so.1.0 directly into the /usr/lib64 directory (allows mod_wsgi.so to see it).

But still, I get the ModuleNotFoundError: No module named 'encodings' message

I'd really appreciate some help.  Here's the state of my configuration:

Here is a section of the Apache Error Log:

[Fri Apr 05 14:56:25.878394 2019] [core:notice] [pid 4621] SELinux policy enabled; httpd running as context system_u:system_r:httpd_
t:s0
[Fri Apr 05 14:56:25.879482 2019] [suexec:notice] [pid 4621] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Fri Apr 05 14:56:25.898250 2019] [auth_digest:notice] [pid 4621] AH01757: generating secret for digest authentication ...
[Fri Apr 05 14:56:25.899292 2019] [lbmethod_heartbeat:notice] [pid 4621] AH02282: No slotmem from mod_heartmonitor
[Fri Apr 05 14:56:25.900426 2019] [wsgi:info] [pid 4622] mod_wsgi (pid=4622): Starting process 'ImgOrientD' with uid=48, gid=48 and
threads=15.
[Fri Apr 05 14:56:25.901434 2019] [wsgi:info] [pid 4622] mod_wsgi (pid=4622): Python home /home/pboerner/.conda/envs/imgorient.
[Fri Apr 05 14:56:25.901496 2019] [wsgi:info] [pid 4622] mod_wsgi (pid=4622): Initializing Python.
[Fri Apr 05 14:56:25.901909 2019] [wsgi:info] [pid 4623] mod_wsgi (pid=4623): Initializing Python.
[Fri Apr 05 14:56:25.902368 2019] [mpm_prefork:notice] [pid 4621] AH00163: Apache/2.4.6 (Red Hat Enterprise Linux) mod_wsgi/4.6.5 Py
thon/3.6 configured -- resuming normal operations
[Fri Apr 05 14:56:25.902391 2019] [mpm_prefork:info] [pid 4621] AH00164: Server built: Jun 22 2018 01:19:25
[Fri Apr 05 14:56:25.902403 2019] [core:notice] [pid 4621] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Fri Apr 05 14:56:25.902778 2019] [wsgi:info] [pid 4625] mod_wsgi (pid=4625): Initializing Python.
[Fri Apr 05 14:56:25.903619 2019] [wsgi:info] [pid 4627] mod_wsgi (pid=4627): Initializing Python.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

The Apache configuration:

Server version: Apache/2.4.6 (Red Hat Enterprise Linux)
Server built:   Jun 22 2018 01:19:25
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

The httpd.conf file for apache:
LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so

# Configure the virtual host for mod_wsgi which will run the MLAI code

<VirtualHost *:80>
    ServerName duaidapst01
    # Set up a daemon process 
    WSGIDaemonProcess ImgOrientD python-home=/home/pboerner/.conda/envs/imgorient
    # Now make sure this runs within that process group
    WSGIProcessGroup ImgOrientD
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptAlias /mlai_test /var/www/wsgi-scripts/mlai_test.wsgi
    <Directory /var/www/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

My simple python wsgi code to test (this never runs):

ALLDIRS = ['/home/pboerner/.conda/envs/imgorient/lib/python3.6/site-packages']
import sys
import site
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add each new site-packages directory.
for directory in ALLDIRS:
    site.addsitedir(directory)
# Reorder sys.path so new directories at the front.
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
    new_sys_path.append(item)
    sys.path.remove(item)
sys.path[:0] = new_sys_path

def application(environ, start_response):
    ex = sys.executable
    status = '200 OK'
    output = "Executable: {} ".format(ex)
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]


Many, many thanks for any help this community can provide!!

Peer

Graham Dumpleton

unread,
Apr 5, 2019, 10:54:28 PM4/5/19
to mod...@googlegroups.com

On 6 Apr 2019, at 6:17 am, Peer Boerner <peer.b...@gmail.com> wrote:

I am trying to get mod_wsgi working with Apache 2.4.6 on RHEL.  I have access to the sudo command but not root. 

The net-net: No matter what I have tried, I can not get mod_wsgi to see the associated python modules for the virtual environment that it was configured for using Anaconda 3. 

The good news: According to the Apache error log files, mod_wsgi has loaded successfully.
The bad news is that I receive numerous messages: Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'

This usually indicates that you have multiple Python versions installed and the one you are trying to use is in a non standard location and mod_wsgi hasn't been compiled so it knows where to find the correct shared library, or mod_wsgi hasn't been configured to find the Python installation or virtual environment.

It is very important that mod_wsgi be compiled for the specific Python installation (distribution/version) you want to use. You cannot force mod_wsgi compiled for one Python installation to use another, not even a virtual environment created using a different version. If you do, issues like what you are seeing will occur.

I've spent two days trying what I believe is every combination of configurations I've seen from the various posts I've read on virtual environments, all to no avail.

I even pasting the output of mod_wsgi module-config into the httpd.conf file.  The LoadModule statement fails with a permissions error even though all of the files in the entire directory have been set to 777.

The only way I have been able to get the mod_wsgi module to load has been to:
1. Build mod_wsgi from source in the virtual environment of my application
2. Use the make install command to load the module into the apache modules directory
3. Copy the libpython3.6m.so.1.0 directly into the /usr/lib64 directory (allows mod_wsgi.so to see it).

A summary of the important steps you need to do are:

1. Ensure that the system package for mod_wsgi is not installed.

2. Build mod_wsgi from source code using the Python version you want to use. The easiest way to do this is use the 'pip install' method. See:


Best practice would be to create a Python virtual environment and activate it. Then run:

pip install mod_wsgi

3. Setup Apache to use this mod_wsgi. First run:

mod_wsgi-express module-config

Add the output of this to the Apache configuration.

It should include a LoadModule and WSGIPythonHome line. If it doesn't have WSGIPythonHome in your case, it may break.

I don't recommend Anaconda Python as it breaks the ability to use mod_ssl in Apache. If you have to use mod_ssl, Python will crash and you can't use Anaconda Python. This is an issue with Anaconda Python that cannot be fixed.

If you don't need mod_ssl, you will can also need an extra step, as Anaconda somehow seems to break Python shared library loading in some cases.

In this case, you may need to add:

LoadFile /some/path/libpython3.6m.so.1.0

where the path is to the Anaconda Python shared library in the Anaconda installation directory. Do not copy this to system library directory, as doing so will break a system Python of same version.

This should be added before the LoadModule line above.

I also recommend adding:

WSGIRestrictEmbedded On

after the WSGIPythonHome. This disables embedded mode, forcing you to use daemon mode. You will get an error when making a request if daemon mode isn't set up correctly.

A few more comments below.
The value for python-home must be a virtual environment created from the same Python installation that mod_wsgi was compiled for.

    # Now make sure this runs within that process group
    WSGIProcessGroup ImgOrientD
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptAlias /mlai_test /var/www/wsgi-scripts/mlai_test.wsgi

SELinux may prevent Apache from reading anything from certain directories. Anything under /var/www should be okay, but it may not be able to read things under /home. In addition to SELinux, if the home directory, eg., /home/pboerner, has permissions 0700 or 0750, then Apache will not be able to see under the home directory.

    <Directory /var/www/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

My simple python wsgi code to test (this never runs):

ALLDIRS = ['/home/pboerner/.conda/envs/imgorient/lib/python3.6/site-packages']
import sys
import site
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add each new site-packages directory.
for directory in ALLDIRS:
    site.addsitedir(directory)
# Reorder sys.path so new directories at the front.
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
    new_sys_path.append(item)
    sys.path.remove(item)
sys.path[:0] = new_sys_path

All of the above code fiddling with sys.path should be removed. You only need to set python-home option above for the virtual environment root.


def application(environ, start_response):
    ex = sys.executable
    status = '200 OK'
    output = "Executable: {} ".format(ex)
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]


Many, many thanks for any help this community can provide!!

Peer

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at https://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Peer Boerner

unread,
Apr 8, 2019, 10:19:27 AM4/8/19
to modwsgi
Graham,

Many, many thanks.  It works. 

The core issues you helped me uncover:
1. Ensured all mod_wsgi bits were removed from all anaconda installations and the default system python.
2. SELinux was indeed getting in the way.  Once we relaxed certain settings, the output generated by mod_wsgi-express module-conf was able to run without error in the httpd.conf file.
3. I built an Anaconda virtual environment and stored it under the /var/www/wsgi-scripts directory. It's access was set to 0775 for the whole tree.
4. Used pip install to install mod_wsgi
5. Added the various WSGI commands as you instructed in the order you specified to the http.conf as well.
6. Removed my path manipulation fiddling from my code.

For full disclosure, my code is throwing some errors but those are clearly my fault.

Many thanks again.

Peer

For anyone else interested, here is a section from my httpd.conf file dealing with mod_WSGI:

# Peer Boerner 4/6/19 - May be required in certain cases according to mod_wsgi support.
LoadFile /var/www/wsgi-scripts/py_svc/lib/libpython3.6m.so.1.0

# Peer Boerner 4/6/19 - Created by the mod_wsgi-express module-config script.
LoadModule wsgi_module "/var/www/wsgi-scripts/py_svc/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/wsgi-scripts/py_svc"

# Peer Boerner 4/6/19 - Recommended by mod_wsgi support.  Will force Daemon mode and show if any errors.
WSGIRestrictEmbedded On

# Configure the virtual host for mod_wsgi which will run the MLAI code
<VirtualHost *:80>
    ServerName XXXXXXXXX

    # Set up a daemon process called ImgOrientD and set the python home to the virtual environment where mod_wsgi was built
    WSGIDaemonProcess ImgOrientD python-home=/var/www/wsgi-scripts/py_svc

    # Now make sure this runs within that process group
    WSGIProcessGroup ImgOrientD
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptAlias /mlai_test /var/www/wsgi-scripts/mlai_test.wsgi

    <Directory /var/www/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>


On Friday, April 5, 2019 at 10:54:28 PM UTC-4, Graham Dumpleton wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to mod...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages