【问题标题】:Error ImportError: No module named 'encodings' in virtual environment using pyenv错误 ImportError:在使用 pyenv 的虚拟环境中没有名为“编码”的模块
【发布时间】:2018-11-01 20:56:59
【问题描述】:

我正在尝试发布由 Django 使用 apache 创建的站点。 服务器的设置如下。

・ CentOS 7.2
・ Python 3.6
・ Django 2.0
・ Apache 2.4

我正在使用pyenv准备一个虚拟环境,如下所示。

git clone https://github.com/yyuu/pyenv.git ~/.pyenv
…
pyenv install anaconda3-5.1.0
pyenv rehash
pyenv global anaconda3-5.1.0
…
yum install httpd httpd-devel
systemctl start httpd
systemctl enable httpd
….
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.14.tar.gz
tar -zxvf 4.5.14.tar.gz
cd mod_wsgi-4.5.14/
./configure --with-python=/home/username/.pyenv/versions/anaconda3-5.1.0/bin/python
make
sudo make install
…

■ httpd.conf

NameVirtualHost *:80
LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>

ServerName xxx.com
DocumentRoot /var/www/html

WSGIScriptReloading On
WSGIDaemonProcess xxx python-path=/home/username/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages python-home=/home/username/.pyenv/versions/anaconda3-5.1.0
WSGIProcessGroup xxx
WSGIScriptAlias / /var/www/html/xxx/xxx/wsgi.py

<Directory "/xxx/">
Order deny,allow
</Directory>
</VirtualHost>

使用此设置会出现以下错误。

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

设置 chmod 应该没问题。还可以考虑哪些其他原因?

【问题讨论】:

    标签: python django apache


    【解决方案1】:

    mod_wsgi 4.5.14 版本太旧,请使用最新版本。

    最好使用pip install mod_wsgi 将它安装到虚拟环境的 Python 安装中,因为这样可以确保拾取正确的 Python 共享库,如果不发生,可能会导致此问题。

    然后运行mod_wsgi-express module-config 以显示配置行以添加到 Apache 以加载 mod_wsgi。这可能还包括LoadFile 行,以进一步强制使用正确的 Python 库,这对于 Anaconda Python 很重要。

    然后使用配置:

    # I really hope you haven't thrown away your complete Apache
    # config file. This should already be defined as part of the
    # default Apache config file.
    
    NameVirtualHost *:80
    
    # Put here the output of running mod_wsgi-express module-config.
    
    # Also turn of embedded mode using the following since using
    # daemon mode.
    
    WSGIRestrictEmbedded on
    
    
    # This may not actually be required. Only needed on some RHEL
    # and CentOS systems.
    
    WSGISocketPrefix /var/run/wsgi
    
    <VirtualHost *:80>
    
    ServerName xxx.com
    DocumentRoot /var/www/html
    
    WSGIScriptReloading On
    
    # The python-path wasn't needed. BTW, don't recommend using pyenv
    # if using PSF Python as their default formula for building Python
    # from source code doesn't create a shared library so it will not
    # work with mod_wsgi. Anaconda Python is probably okay as it would
    # use binary distro.
    
    # Ensure python-home is what is value of sys.prefix for Python.
    
    WSGIDaemonProcess xxx python-home=/home/username/.pyenv/versions/anaconda3-5.1.0
    
    WSGIProcessGroup xxx
    
    # Also force main interpreter if have only one app.
    
    WSGIApplicationGroup %{GLOBAL}
    
    WSGIScriptAlias / /var/www/html/xxx/xxx/wsgi.py
    
    # Apache 2.4 should use 'Require all granted'.
    
    # The ``/xxx`` path should match full absolute directory of WSGI
    # script file used in ``WSGIScriptAlias``, so not right here.
    
    <Directory "/xxx/">
    Require all granted
    </Directory>
    </VirtualHost>
    

    顺便说一句,将 Python 源代码放在与 DocumentRoot 相同的目录下是个坏主意。如果你注释掉WSGIScriptAlias,人们可以下载你的代码。

    【讨论】:

    • 顺便说一句,您也可以删除WSGIScriptReloading On,因为这是默认设置。
    【解决方案2】:

    你应该添加

    WSGIPythonHome /path/to/virtualenv/
    

    到 httpd.conf

    https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 2019-03-03
      • 2017-09-11
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多