mod_wsgi: Unable to stat Python home and ImportError: No module named 'encodings'

11,153

A couple of things to check.

  • The pyenv tool doesn't install Python with a shared library by default. That could result in problems as mod_wsgi wants a shared library. You need to explicitly tell pyenv to build Python with a shared library.

  • A home directory on many Linux systems is not readable to other users. When mod_wsgi is being initialised, it is running as the Apache user and will not be able to see inside of the home directory. There isn't an issue when the mod_wsgi module is loaded by Apache as Apache is running as root at that point.

There are other issues with your configuration where don't follow best practices, but the above, especially the second item is likely the cause of your problem.

Share:
11,153
jun68ykt
Author by

jun68ykt

I'm a web developer as an independent contractor. I live in Tokyo. My mother tongue is ja-JP. Excuse me that my English is sometimes not fluent.

Updated on July 21, 2022

Comments

  • jun68ykt
    jun68ykt almost 2 years

     I am trying to construct a web site on Apache 2.4 using Django and mod_wsgi on Python 3.5.2. But when apache daemon httpd is started, following error message is output on /var/log/httpd/error_log.

    [Fri Sep 16 17:44:57.145900 2016] [wsgi:warn] [pid 20593] (13)Permission denied: mod_wsgi (pid=20593): Unable to stat Python home /home/ec2-user/.pyenv/versions/3.5.2. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
    Fatal Python error: Py_Initialize: Unable to get the locale encoding
    ImportError: No module named 'encodings'

     So, I explored some articles about similar problems, e.g.

    but the causes of error message above are not yet resolved.
     Please indicate some points to be checked or documents to read and so on.

    My development environments are as follows.

    (1) Host and OS

    • Hosting Service: Amazon AWS EC2
    • OS: Amazon Linux AMI release 2016.03

    (2) Django Project

     I made Django project named testprj in the directory /home/ec2-user/django-sites with user account of ec2-user.

    [ec2-user@MyEC2 django-sites]$ pwd
    /home/ec2-user/django-sites
    [ec2-user@MyEC2 django-sites]$ ls -l
    total 4
    drwxrwxr-x 3 ec2-user ec2-user 4096 Sep 16 14:50 testprj
    

    Database which the testprj uses is already set up. So, development server provided in Django is successfully started with no error as bellow.

    [ec2-user@MyEC2 testprj]$ python manage.py runserver
    Performing system checks...
    
    System check identified no issues (0 silenced).
    September 16, 2016 - 14:23:47
    Django version 1.10.1, using settings 'testprj.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.
    

    (3) Environments of Python

     I installed Python 3.5.2 by pyenv install 3.5.2. And then I set pyenv virtualenv 3.5.2 django-sites. And I set local of /home/ec2-user/django-sites to env django-sites as bellow.

    [ec2-user@MyEC2 django-sites]$ pwd
    /home/ec2-user/django-sites
    [ec2-user@MyEC2 django-sites]$ pyenv versions
      system
      3.5.2
      3.5.2/envs/django-sites
    * django-sites (set by /home/ec2-user/django-sites/.python-version)
    [ec2-user@MyEC2 django-sites]$ python -V
    Python 3.5.2
    

     And I installed following modules through pip command.

    [ec2-user@MyEC2 django-sites]$ pwd
     /home/ec2-user/django-sites
    [ec2-user@MyEC2 django-sites]$ pip list
    configparser (3.5.0)
    Django (1.10.1)
    mod-wsgi (4.5.7)
    pip (8.1.2)
    PyMySQL (0.7.9)
    setuptools (27.2.0)
    

    (4) Web server

     Web server which I use is Apache 2.4 as bellow.

    [ec2-user@MyEC2 ~]$ httpd -v
    Server version: Apache/2.4.23 (Amazon)
    Server built:   Jul 29 2016 21:42:17
    

     User and Group of executing Apache are both apache as next lines in /etc/httpd/conf/httpd.conf.

    User apache
    Group apache 
    

    (5) Additinal conf file for the Django project testprj

     I intend to configure such that the URL

    http://[MyEC2 domain]/testprj/
    

    can access a top page of Django project located at /home/ec2-user/django-sites/testprj.
     [MyEC2 domain] above is like

    ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
    

    So, I wrote testprj.conf in /etc/httpd/conf.d as bellow.

    [ec2-user@MyEC2 conf.d]$ pwd
    /etc/httpd/conf.d
    [ec2-user@MyEC2 conf.d]$ cat -n testprj.conf 
    
     1  LoadModule wsgi_module /home/ec2-user/.pyenv/versions/django-sites/lib/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so
     2  
     3  WSGIPythonHome /home/ec2-user/.pyenv/versions/3.5.2
     4  WSGIScriptReloading On
     5  WSGIScriptAlias  /testprj/ /home/ec2-user/django-sites/testprj/testprj/wsgi.py
     6  WSGIPythonPath /home/ec2-user/django-sites/testprj:/home/ec2-user/.pyenv/versions/django-sites/lib/python3.5/site-packages
     7  
     8  <VirtualHost *:80>
     9  
    10    ServerName http://ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
    11    SuexecUserGroup apache apache
    12  
    13    <Directory /home/ec2-user/django-sites/testprj/testprj>
    14      <Files wsgi.py>
    15      Require all granted
    16      </Files>
    17    </Directory>
    18  
    19    Alias /static/ "/home/ec2-user/django-sites/testprj/static/"
    20  
    21    <Directory /home/ec2-user/django-sites/testprj/static>
    22      <Files *>
    23      Require all granted
    24      </Files>
    25    </Directory>
    26  
    27  </VirtualHost>
    28  
    
    [ec2-user@MyEC2 conf.d]$
    

    Best regards.

  • jun68ykt
    jun68ykt almost 8 years
    Thank you for your advice.I already considered the 1st item of two things you gave me. I wrote I installed Python 3.5.2 by pyenv install 3.5.2. in my question. But this was not correct. In reality, I used command line: CONFIGURE_OPTS=--enable-shared pyenv install 3.5.2. So, following your advice "especially the second item is likely the cause of your problem.", I will research about execute permissions more according to your 2nd item.Thanks !
  • jun68ykt
    jun68ykt almost 8 years
    I tried sudo chmod 755 /home/ec2-user, and then It worked !
  • Divij Sehgal
    Divij Sehgal about 7 years
    I am stuck @ Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings' Please help
  • Divij Sehgal
    Divij Sehgal about 7 years
    Virtual environment was made using mkvirtualenv
  • Graham Dumpleton
    Graham Dumpleton about 7 years
    Create a new question. Don't post your new problems in comments to an existing question. In your new question properly explain your issue, what steps you did to build Python and mod_wsgi etc.
  • ThePhi
    ThePhi about 4 years
    @jun68ykt I'm a bit anxious of changing the access of my HOME folder. Isn't there other solution?