简体   繁体   中英

Flask app imports fail when launched under uWsgi

First, let me point out that this works fine using flask run . Also note the behavior is identical on Ubuntu 16.04 and macOS 10.12.4. I'm using a Python 3.6.0 virtualenv on macOS and 3.5.2 on Ubuntu.

I have the following Flask app structure:

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

__init__.py :

from flask import Flask

gApp = Flask(__name__)

#   Config

gApp.config.from_envvar('TSINT_CONFIG')

#
# DB
#

gDB = SQLAlchemy(gApp)

#
#   Import the views.
#

import tsint.app
import tsint.admin

app.py is the main Flask routes and such, and defines the Flask user datastore and security.

wsgi.py :

from tsint import gApp

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

When I try testing wsgi , I get this “No module named '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)

I'm running in a virtualenv, which contains all the modules. I'm guessing wsgi isn't aware of those? I'm not clear on what I'm missing.

EDIT:

I've changed things up a bit to match the various tutorials a bit better, but the result is unchanged.

My directory structure now looks like this:

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

Again, when I run uwsgi , I get this error:

(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)

Again, it seems uwsgi can't find the installed modules like flask . I've seen talk of the -H option to point the the virtualenv, but this causes other problems for me, and none of the tutorials showing virtualnv & uWsgi use this option.

Thanks!

Just sharing how I setup my virtual env for my flask application, it maybe useful to you or otherwise.

Whenever I run a virtualenv, (on Fedora), I get my project name in round braces and it is not clear to me if you are indeed using a virtual env.

Also, in the my logtrail for the uwsgi is cleaner that the one u posted. Anyways, for a point of reference, Here's how I did it:-

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

References:-

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

Hope this helps. If not, hope it gives others a point of reference .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM