【问题标题】:django, pyenv, uwsgi - ModuleNotFoundError: No module named 'django'django, pyenv, uwsgi - ModuleNotFoundError: No module named 'django'
【发布时间】:2017-02-07 09:54:21
【问题描述】:

我在/etc/uwsgi/vassals/gsd.ini中有以下vassal配置:

[uwsgi]
plugins = python
env = DJANGO_SETTINGS_MODULE=%n.settings
virtualenv = /home/toogy/.pyenv/versions/%n
chdir = /home/webapps/%n
module = %n.wsgi:application
master = true
vacuum = true
pidfile = /tmp/uwsgi-%n.pid
socket = /tmp/uwsgi-%n.sock
daemonize = /var/log/uwsgi/%n.log
chmod-socket = 666
uid = toogy
gid = toogy

这是我得到的 uwsgi 日志

Tue Feb  7 10:49:12 2017 - received message 1 from emperor
...gracefully killing workers...
Gracefully killing worker 1 (pid: 31406)...
worker 1 buried after 1 seconds
binary reloading uWSGI...
chdir() to /etc/uwsgi/vassals
closing all non-uwsgi socket fds > 2 (max_fd = 1024)...
found fd 3 mapped to socket 0 (/tmp/uwsgi-gsd.sock)
running /usr/bin/uwsgi
*** has_emperor mode detected (fd: 7) ***
[uWSGI] getting INI configuration from gsd.ini
*** Starting uWSGI 2.0.14 (64bit) on [Tue Feb  7 10:49:13 2017] ***
compiled with version: 6.3.1 20170109 on 18 January 2017 00:35:47
os: Linux-3.14.32-xxxx-grs-ipv6-64 #7 SMP Wed Jan 27 18:05:09 CET 2016
nodename: renard
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /etc/uwsgi/vassals
detected binary path: /usr/bin/uwsgi
chdir() to /home/webapps/gsd
your processes number limit is 15700
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 inherited UNIX address /tmp/uwsgi-gsd.sock fd 3
Python version: 3.6.0 (default, Jan 16 2017, 12:12:55)  [GCC 6.3.1 20170109]
PEP 405 virtualenv detected: /home/toogy/.pyenv/versions/gsd
Set PythonHome to /home/toogy/.pyenv/versions/gsd
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x39d21f0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
added /home/webapps/gsd/ to pythonpath.
Traceback (most recent call last):
  File "/home/webapps/gsd/gsd/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
gracefully (RE)spawned uWSGI master process (pid: 27844)
spawned uWSGI worker 1 (pid: 32312, cores: 1)

它找不到django,我不知道为什么,因为uwsgi 似乎检测到了python 环境(其中安装了django )。

另外,它显示Python version: 3.6.0,而我的 virtualenv Python 版本是 3.5.2。我不知道这是否应该发生。系统Python版本为3.6.0

我正在使用最新版本的 uwsgiuwsgi-plugins-python Arch Linux 官方软件包。

【问题讨论】:

    标签: python django uwsgi pyenv


    【解决方案1】:

    问题是链接到uwsgi的系统范围的python版本需要和virtualenv的版本一样,我认为这是一件非常愚蠢的事情。

    编辑 2021 年 4 月:我现在推荐使用 gunicorn,它没有这个问题

    【讨论】:

    • 你拯救了我的一天!这确实是一个愚蠢的要求。
    • @valentin 如何验证两者是否相同?如果没有,我该如何解决?
    • @SaitejaParsi 要知道您的 Python 系统版本:python --version 从任何终端,要知道 UWSGI Python 版本,只需运行 uwsgi 并阅读日志即可知道正在使用哪个 UWSGI Python 版本。我当时修复它的方法是使用 UWSGI 使用的 Python 版本创建一个新环境。
    • 我很恼火地宣布情况仍然如此。这篇文章需要更多的支持。
    • 我可以说在 debian buster 上使用 uWSGI==2.0.18 仍然是这种情况......
    【解决方案2】:

    就我而言,它使用的是系统范围的 uwsgi,我正在使用 virtualenv,所以如果我执行

    $ which uwsgi
    

    我收到了/usr/local/python3.6/bin/uwsgi

    根据 Valentin Iovene 的建议,您需要使用虚拟环境中的 uwsgi

    我的目录结构是这样的:

    ~/Env
    --/app
    --/bin
    ----/....
    ----/uwsgi <-- This should be the good one
    ----/...
    --/include
    --/lib
    

    app 目录是我的 django 应用程序所在的位置)

    在我的情况下,uwsgi 文件没有执行权限,所以我只执行了:

    $ chmod +x ~/Env/bin/uwsgi
    

    最后在我的app目录下执行了uwsgi命令如下:

    ../bin/uwsgi --http :8000 --module app.wsgi
    

    现在我可以看到我的应用正在运行 :)

    我正在关注本指南:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

    接下来的步骤是配置 nginxhttps...

    我知道这是一个较晚的回复,但希望这会有所帮助并分享对我有用的方法。

    【讨论】:

      【解决方案3】:

      看看这个gsd.ini

      virtualenv = /home/toogy/.pyenv/versions/%n
      

      你有没有在这个virtualenv下安装django

      【讨论】:

      • 感谢您的回答。确实安装好了。
      【解决方案4】:

      我还发现了一些我想分享的可能的陷阱:

      • 检查是否将virtualenv(与venvpyhomehome 相同)设置为包含binincludelib 的目录。 . 目录
      • 检查用户 (uid) 是否可以读取您项目中的文件和虚拟环境中的库(这以 ModuleNotFoundError 而不是权限错误结束)
      • 使用need-app 退出失败(这有助于调试,应该是默认的恕我直言)
      • 使用strict 避免配置中的拼写错误(这也应该是默认设置...)
      • 如果您的test.py 运行,请尝试导入您的项目和虚拟环境的模块,以测试它是否有效。它还有助于添加一个

        import sys
        print(sys.path)
        

      你也可以复制打印出来的sys.path,打开一个python shell,把sys.path设置成相同的值,然后尝试导入需要的wsgi模块。

      【讨论】:

        猜你喜欢
        • 2018-11-05
        • 2020-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-22
        • 1970-01-01
        • 2023-01-27
        相关资源
        最近更新 更多