No module named flask while running uWSGI - python

I have a very simple flask app (myflaskapp.py):
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "<span style='color:red'>I am app 1</span>"
If I run:
uwsgi --http-socket :3031 --plugin python --wsgi-file myflaskapp.py --callable app
I get the following output:
Traceback (most recent call last):
File "myflaskapp.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
and I don't understand why. I have flask installed (pip install flask). If I run ipython and import flask it also works there. Any ideas? Thanks!

In the end what worked for me was adding -H /path/to/virtualenv to the uWSGI command:
uwsgi --http-socket :3031 --plugin python --wsgi-file myflaskapp.py --callable app -H /path/to/virtualenv
I also had different Python versions in the virtualenv and for uWSGI. I'm still investigating if this could cause any problems.

I ran into same problem once, as there was some version conflict
then instead of using pip to install uwsgi I did it by my package manager
On ubuntu machine,
sudo apt-get install uwsgi
Also check and run myflaskapp.py without uwsgi that is by using app.run() in your code
*Note : That will be by werkzeug server.

I faced similar problem and found the reason that if we have a module installed in a virtual environment(Flask in this case) we may need to add --virtualenv path in addition to the basic instructions needed to run a Flask app using uWSGI
So the instruction according the uWSGI document would be:
uwsgi --http-socket :3031 --plugin python --wsgi-file myflaskapp.py --callable app --virtualenv /path_to_virtualenv

You can just add one line into you .ini file:
home=/your/virtual/env/path

Related

Trying to run a python file in flask

I am trying to learn flask. I created a venv and installed flask. I set the flaskblog.py as FLASK_APP. But it says serving flask app 'app.py' and it fails. why is this?
I'm not sure if the image is visible. So, I'll write what I've tried below,
In the windows powershell, I cd to the directory where my venv and python script is, then
$ . vnev\Scripts\activate
$ pip install flask
$ set FLASK_APP=flaskblog.py
$ flask run
it gives me the error saying,
Serving Flask app 'app.py' (lazy loading)
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: Could not import 'app'.
What is wrong here? I have also tried $ setx FLASK_APP "flaskblog.py and still get the same result. I think i created this app.py using pycharm's virtual environment and now, I cannot set other files to FLASK_APP.
What should I do?

Python script can't access library as www-data

I am running several flask apps on an Ubuntu 18.04 server with nginx and uwsgi workers in python 3.6. The overall architecture is as described in this post.
After deploying a new version of "myapp" the app quietly ceased to work. The logs generated by uwsgi show the terrifying "no python application found" error. Calling the URL resulted in an "Internal Server Error".
Calling the app directly from the terminal as the regular non-root user with python3 myapp.py in an activated virtual environment worked fine. I assumed that uwsgi calls the app as www-data and tried sudo -u www-data python3 myapp.py. Finally, the app refuses to fire up.
(env) user#server:~/projects/myapp$ python3 myapp.py
* Serving Flask app "MyApp" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^C(env) user#server:~/projects/myapp$ sudo -u www-data python3 myapp.py
Traceback (most recent call last):
File "myapp.py", line 20, in <module>
import post
File "/home/oliver/projects/myapp/post.py", line 5, in <module>
import qrcode
ModuleNotFoundError: No module named 'qrcode'
I pip3-uninstalled and re-installed qrcode[pil] in the activated environment. I even sudo installed qrcode[pil] systemwide in an act of desperation.
So my question: is there a way to install the library qrcode[pi] in a way the script can import it as www-data? Is my assumption calling the script as www-data to debug mainly allright?
Thanks a bunch for your help!

No module named flask with waitress-serve and virtualenv (App engine flex)

I'm working on a project which used to be run on Linux for test. It's an App Engine flex project, run with gunicorn. Gunicorn does not work on Windows if I understood well, so I've been adviced to use waitress.
I also use virtualenv in my project.
So when I'm in my virtualenv, I run waitress-serve main:app (the gunicorn cmd was gunicorn -b :8080 main:app). I get an error: It had these arguments:
1. No module named flask.
I use flask. I can see the flask folder in my virtualenv folder. And when I run python then from flask import Flask I have no error.
Is there compat issue between waitress and virtualenv ? Or I'm doing something else wrong ? (already tried to delete virtualenv folder and install all the things again)
Python modules are case sensitive
Try Flask not flask.

unable to deploy the python pyramid web apps to heroku

This is the Error logs I find in the heroku when deploying the python pyramid application. I have followed the each and every steps the python pyramid documentation has. Where have I exactly missed not able to figure out.
I doubt if my way of creating run file is incorrect. I have created a run.py
file and added the following code into it.
#!/bin/bash
set -e
python setup.py develop
python runapp.py
You cannot host a web app like that. You need a proper server, for example gunicorn - see the Pyramid docs on how to run with gunicorn, that is what needs to go in your Procfile. You don't need a run.py.
Try this:
Profile
web: ./run
run
#!/bin/bash
set -e
python setup.py develop
python runapp.py
runapp.py
#Heroku Startup
import os
from paste.deploy import loadapp
from waitress import serve
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:production.ini', relative_to='.')
serve(app, host='0.0.0.0', port=port)
requirements.txt
pyramid
pyramid_chameleon
pyramid_debugtoolbar
waitress
<add other dependencies here>
runtime.txt
python-3.3.0 #or whatever version you are running. Take this out

Run Falcon APP with uwsgi

I just started to learn Falcon (http://falcon.readthedocs.org/en/latest/user/quickstart.html)
but it need a web server running and docs suggesting use uwsgi or gunicorn.
though they have mentioned that how to use it with gunicorn
$ pip install gunicorn #install
$ gunicorn things:app #and run app through gunicorn.
But I want to run this sample app with uwsgi. but I have no clue how to.
I have installed it pip install uwsgi also gevent as suggested here http://falcon.readthedocs.org/en/latest/user/install.html
but what now. somebody guide me.
You'll probably find your answer on the uWSGI documentation site, specifically try this page:
http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html
I've never used Falcon or uWSGI, but it looks like you can probably get away with:
uwsgi --wsgi-file things.py --callable app
You can use uwsgi.ini to store configuration and easy run. Good way to setup uwsgi as services. Configuration with virtualenv
[uwsgi]
http = :8000
chdir = /home/user/www/uwsgi-ini
virtualenv = /home/user/www/uwsgi-ini/venv/
wsgi-file = main.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:9191
and run in app folder:
uwsgi uwsgi.ini
command with virtualenv
uwsgi --http :8000 --wsgi-file main.py --callable app -H $(pwd)/venv/

Categories

Resources