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!
Related
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?
Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)
This is my docker file:
FROM python:3-alpine
MAINTAINER foo
COPY app /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python3", "gateway.py"]
I run the container by this command:
docker run --name mei_converter -p 5000:5000 mei_converter:latest
And receives this:
ModuleNotFoundError: No module named 'verovio'
I already fixed the dependencies in order to run it on my local machine
Terminal: python3 gateway.py
['/usr/local/lib/python3.7/site-packages',
'/Users/max/PycharmProjects/flaskmicroservice/app',
'/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-
dynload', '/Users/max/Library/Python/3.7/lib/python/site-packages',
'/usr/local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/site-packages/verovio-
2.0.0-py3.7-macosx-10.13-x86_64.egg']
* Serving Flask app "gateway" (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
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
This works well
I also checked the project interpreter in PyCharm and found the package as installed.
Maybe some information about this third party package could help:
It is a converter for music-encoding from these guys https://www.verovio.org/python.xhtml
and not "native python" (SWIG is necessary)
By the way.. I come from the java world and have in fact zero experience with python. I am using this flask app as an microservice for my spring boot app.
I would be grateful for any help!
You need to do in the container the same you did in your local host. That is, you need to add steps to the Dockerfile to compile verovio and use it in the container.
I have a flask app that are running in a local machine when runing flask run but it's not running in an AWS EC2.
Both environment have Ubuntu 16.04 and Python 2.7.
Both are running in virtualenvs created in the same way and path (only user's folder is different).
Both have the same Flask version.
Both have python-dotenv and .flaskenv file and are set to
development and debug.
Both have the same project folder (same git repo) and I flask run
in the same folder for both.
I defined the PORT in .flaskenv and I can see that the PORT defined
in there is where flask is running in both.
When I try to get the same URL, in EC2 I got an error: NoAppException: Could not import "app.initialize"
Running in local machine:
$ flask run
* Serving Flask app "app.initialize:web_app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://0.0.0.0:5324/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 179-083-368
Log:
127.0.0.1 - - [07/Mar/2019 09:10:13] "GET /api/directors/me/info HTTP/1.0" 200 -
Running un EC2:
$ flask run
* Serving Flask app "app.initialize:web_app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://0.0.0.0:5324/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 104-758-876
Log:
127.0.0.1 - - [07/Mar/2019 12:05:42] "GET /api/directors/me/info HTTP/1.0" 500 -
Traceback (most recent call last):
File "/home/myuser/.virtualenvs/myvenv/lib/python2.7/site-packages/flask/cli.py", line 325, in __call__
self._flush_bg_loading_exception()
File "/home/myuser/.virtualenvs/myvenv/lib/python2.7/site-packages/flask/cli.py", line 313, in _flush_bg_loading_exception
reraise(*exc_info)
File "/home/myuser/.virtualenvs/myvenv/lib/python2.7/site-packages/flask/cli.py", line 302, in _load_app
self._load_unlocked()
File "/home/myuser/.virtualenvs/myvenv/lib/python2.7/site-packages/flask/cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "/home/myuser/.virtualenvs/myvenv/lib/python2.7/site-packages/flask/cli.py", line 371, in load_app
app = locate_app(self, import_name, name)
File "/home/myuser/.virtualenvs/myvenv/lib/python2.7/site-packages/flask/cli.py", line 246, in locate_app
'Could not import "{name}".'.format(name=module_name)
NoAppException: Could not import "app.initialize".
For this app, I have a custom flask command and when I try to call this command for both environment, on the local machine works fine, but in EC2 I got a No such command error.
I really tried everything I found in SO about checking if things are getting imported right, packages versions and etc. In EC2, when I ipython, I can import app.initialize and I can even import models and use my app code to run things, including the command function.
Finally, if I run this app using Gunicorn in EC2, its works fine, but sometime I need to run with flask for debug (its a sandbox environment) and I need to run custom commands too.
Any idea what may be wrong in EC2?
Ty All
You need to add the path to app to the ec2's python path. If you're in the directory of the project, run export PYTHONPATH="$(pwd):$PYTHONPATH".
Basically if your project looks something like this in the file system:
/home
/myuser
/myproject
/app
__init__.py
somemodule.py
Then you want to have /home/myuser/myproject present in the environment variable PYTHONPATH.
You'll want to automate this since that environment variable will only be present in the shell you ran the command in. To do that you these are some potential options:
There may be a way for you to configure environment variables via the AWS UI (not sure).
You can create or use an image that already has PYTHONPATH set up and make sure you put your project in the path.
You can add the export command to ~/.profile if you are running a shell as the same user that will be running the application, or /etc/profile if you just want to add the path to the application for all users. Then reboot the server.
But before worrying about that, to test that the solution is correct follow these steps:
SSH into the ec2 instance
Start a python shell with python
Run this statement (it should fail due to an import error): import app.initialize
Exit the shell with exit() or ctrl-d
Export the path to app
Repeat steps 2-3, except this time they should succeed
I've built a Django project that works, even after I freeze it using Cx_Freeze and Py2exe.
Now I'm trying to set up the project for distribution, which requires a real webserver. I'm going for Gunicorn (will add Nginx once it works). I managed to run the Gunicorn server properly through the command line using :
gunicorn wsgi:application
However, I need to be able to run the server from my Python script, as the server is ment to be localhost. Gunicorn used to be shipped with a command 'run_gunicorn' designed for Django, but this command is now deprecated.
I tried understanding the following method :
How to use Flask-Script and Gunicorn
But I can't figure out how to make it work with Django.
The following doesn't work:
from django.core.wsgi import get_wsgi_application
from gunicorn.app.base import Application
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = get_wsgi_application()
Application().run(application)
Could someone tell me how to start the gunicorn server from my Python script ?
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