错误ImportError:没有名为'编码的模块'在使用pyenv的虚拟环境中

时间:2018-05-23 01:48:56

标签: python django apache

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

·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应该没问题。可以考虑其他什么原因?

2 个答案:

答案 0 :(得分:0)

你应该添加

function getIncomes() {
  return fetch('/get_incomes/January').then(response => response.json());
}

到httpd.conf

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

答案 1 :(得分:0)

mod_wsgi的4.5.14版本已经过时,请使用最新版本。

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

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

然后使用config:

# 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,则可以下载您的代码。