在uWsgi下启动时,Flask应用程序导入失败

2024-09-30 16:35:22 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,让我指出使用flask run可以很好地工作。另外请注意,Ubuntu16.04和MacOS10.12.4的行为是相同的。我在macOS上使用python3.6.0virtualenv,在Ubuntu上使用3.5.2。在

我有以下Flask应用程序结构:

tsint/
    __init__.py
    app.py
    wsgi.py
    ...

__init__.py

^{pr2}$

app.py是主要的烧瓶路径等,它定义了烧瓶用户 数据存储和安全。在

wsgi.py

from tsint import gApp

if __name__ == "__main__":
    gApp.run()

当我尝试测试wsgi时,我得到了一个“没有名为‘flask’的模块”:

$ cd <directory containing tsint above>
$ uwsgi --socket 0.0.0.0:5000 --protocol=http -w tsint
*** Starting uWSGI 2.0.14 (64bit) on [Wed May 10 19:57:59 2017] ***
compiled with version: 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1) on 15 March 2017 15:31:30
os: Darwin-16.5.0 Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64
nodename: EclipseSSD.local
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /Users/rmann/Projects/Fiber/SignupApp
detected binary path: /Users/rmann/Projects/Fiber/SignupApp/venv/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 709
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: OSX spinlocks
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:5000 fd 3
Python version: 3.6.0 (default, Mar  9 2017, 14:47:47)  [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x7fe77e000000
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72752 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "./tsint/__init__.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
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 ***
spawned uWSGI worker 1 (and the only) (pid: 26305, cores: 1)

我运行的是一个virtualenv,它包含了所有的模块。我猜wsgi 不知道这些吗?我不清楚我遗漏了什么。在

编辑:

为了更好地匹配各种教程,我做了一些修改,但是 结果不变。在

我的目录结构如下所示:

tsint/
    requirements.txt
    setup.py
    wsgi.py
    env/
    tsint/
        __init__.py
        app.py
        static/
        templates/
        ...

同样,当我运行uwsgi时,我得到了这个错误:

(env) tsint/ (master)$ uwsgi --socket 127.0.0.1:8080 --protocol=http -w wsgi
*** Starting uWSGI 2.0.15 (64bit) on [Thu May 11 17:06:43 2017] ***
...
current working directory: /Path/to/tsint
detected binary path: /Path/to/tsint/env/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
...
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "./wsgi.py", line 1, in <module>
    from tsint import gApp
  File "./tsint/__init__.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
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 ***
spawned uWSGI worker 1 (and the only) (pid: 36939, cores: 1)

同样,uwsgi似乎找不到安装的模块,比如flask。我见过 讨论-H选项来指向virtualenv,但这会导致其他问题 对我来说,没有一个教程显示virtualnv&uWsgi使用这个选项。在

谢谢!在


Tags: toinfrompyimportappflaskwsgi
1条回答
网友
1楼 · 发布于 2024-09-30 16:35:22

只是分享一下我如何为我的烧瓶应用程序设置虚拟环境,它可能对你有用或其他。在

每当我运行virtualenv(在Fedora上)时,我的项目名称都用圆括号括起来,我不清楚您是否真的在使用虚拟env。在

另外,在我的日志记录中,uwsgi的记录比u发布的更干净。 不管怎样,作为参考,我是怎么做到的:

cd my_app/
virtualenv my_app_venv/  
New python executable in /home/js/mypython/my_app/my_app_venv/bin/python
Installing setuptools, pip, wheel...
Installing setuptools, pip, wheel...done.
mkdir app 
(this is for modules)
[root@localhost my_app]# source my_app_venv/bin/activate
(my_app_venv) [root@localhost my_app]#
**(Notice how my prompt has changed   indicating I now am in a virtualenv)**
pip install uwsgi

uwsgi  socket 127.0.0.1:8080  protocol=http -w wsgi (foreground process).


(my_app_venv) [root@localhost my_app]# /etc/init.d/nginx start
bash: /etc/init.d/nginx: No such file or directory

(my_app_venv) [root@localhost my_app]# yum install epel-release
(my_app_venv) [root@localhost my_app]# yum install nginx
Loaded plugins: langpacks, refresh-packagekit
Package 1:nginx-1.4.7-5.fc20.x86_64 already installed and latest version
Nothing to do

(my_app_venv) [root@localhost my_app]# service nginx start
Redirecting to /bin/systemctl start  nginx.service

(my_app_venv) [root@localhost my_app]# ps -ef | grep 'nginx'
root     58682     1  0 11:25 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx    58683 58682  0 11:25 ?        00:00:00 nginx: worker process
root     58705 21263  0 11:27 pts/2    00:00:00 grep  color=auto nginx

R参考文献:在

https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-uwsgi-web-server-with-nginx

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-6-with-yum

希望这有帮助。如果没有,希望能给别人一个参考点。在

相关问题 更多 >