Django 未运行 : ModuleNotFoundError: No module named 'encodings'

标签 django python-3.x ubuntu apache2 mod-wsgi

我已经在新的 Ubuntu 17.10 服务器上安装了 django 网站。它基于python3。当我使用

在开发模式下运行网站时,它工作正常

python manage.py runserver

但是当我尝试通过 apache2 Web 服务器在 端口 80 中配置它时,通过编辑默认配置文件在 apache 中添加 wsgi,如下所示..

<VirtualHost *:80>

    ServerAdmin webmaster@localhost

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /home/usr/mygpsproject/gps_restapi/gps_restapi>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess gps_restapi python-path=/home/usr/mygpsproject/gps_restapi/ python-home=/home/usr/mygpsproject/venv/bin/
    WSGIProcessGroup gps_restapi
    WSGIScriptAlias / /home/usr/mygpsproject/gps_restapi/gps_restapi/wsgi.py

</VirtualHost>

但网站无法正常工作。当我检查apache错误日志发现这个错误时...

[Sat Apr 14 16:16:09.201739 2018] [core:notice] [pid 5089:tid 140258267696064] AH00051: child pid 7500 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f906c390bc0 (most recent call first):

我无法找到这个编码模块是什么。它在开发模式下运行良好。所以 python 肯定不会缺少任何库。

我的配置可能有什么问题。

最佳答案

而不是:

python-home=/home/usr/mygpsproject/venv/bin/

您应该使用:

python-home=/home/usr/mygpsproject/venv

查看有关使用虚拟环境的文档。您应该指向 sys.prefix 给出的虚拟环境的根目录,而不是 bin 目录。

还要确保虚拟环境是从编译 mod_wsgi 的相同 Python 版本创建的。

最后,根据所使用的 Linux 发行版,您也不应该将代码放在个人主目录下,因为主目录的权限可能会导致在 Apache 下运行代码的用户无法访问主目录。

关于Django 未运行 : ModuleNotFoundError: No module named 'encodings' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49833632/

相关文章:

ubuntu - 如何测试 Stunnel?

python - Django 运行余额/累计余额

python - 针对 JSONField 的 Django 模型过滤器,其中键包含连字符/破折号

python - 防止测试代码因为参数不同而出现 "duplication"

python - 文件在 python try except 代码的 else block 中有零行

symfony - 为什么我在 docker 容器中看不到我的文件?

Django 自定义管理命令有时工作,有时不工作,具体取决于路径

python - 重建 Django 开发服务器数据库

python - STATICFILES_DIRS 设置不应包含 STATIC_ROOT 设置

python - for line in open(..) 在文件中间意外挂起