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

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.

Related

mod_wsgi can't import flask with pipenv

I'm trying to deploy my first flask application and I'm running into some issues. I had my app working on my local machine with the build in flask development server, and all my dependencies were managed by pipenv. I uploaded my app to /var/www/directory_printer and ran pipenv install. Then I created a apache vhost file and pointed it to my .wsgi file:
WSGIScriptAlias / /var/www/directory_printer/directory.wsgi
In directory.wsgi, I import my app. At the beginning of my main app file, I import flask. When I try to access my app, I get a 500 error. In the apache error log I get:
ModuleNotFoundError: No module named 'flask'
If I start an interactive python shell in the directory_printer folder with the pipenv shell activated, I can import flask just fine.
I tried putting the path to my virtual env at the beginning of my directory.wsgi file:
#!/path/to/venv
but that doesn't seem to help. I'm sure I'm missing something simple, but I can't seem to see what it is. Any help would be appreciated. Thanks!
Often on Linux systems a home directory is not accessible to other users so the Apache user will not be able to read anything under the directory. Permissions can also be wrong on Python packages that have been installed making them inaccessible as well.
Ok, not sure if this is the correct answer, but it is now working for me.
First
create a .venv folder in the project root folder
then change permissions:
sudo chown www-data:www-data .venv
Then create a virtual environment and install your requirements from Pipfile as the user wsgi will run as:
sudo -su www-data python3 -m virtualenv -p python3 .venv pipenv install
This will install your virtual environment in the project folder. Check out this answer for more:
How to set PIPENV_VENV_IN_PROJECT on per-project basis
Then add this to your .wsgi folder:
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
Bottom of this page for more info:
https://flask.palletsprojects.com/en/2.0.x/deploying/mod_wsgi/
And now it works! Hopefully this will help somebody else out as well.

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?

ModuleNotFoundError: No module named 'django' by Deploying on Azure

I'm trying to deploy a django web app to the Microsoft Azure and this is correctly deployed by the pipeline on DevOps Azure, but I get the error message (ModuleNotFoundError: No module named 'django) on portal Azure and cannot reach my app via the URL.
The app also works properly locally
Here is the whole error message: '''https://pastebin.com/mGHSS8kQ'''
How can I solve this error?
I understand you have tried the steps suggested in the SO thread Eyap shared, and few things here are already covers that. Kindly review these settings.
You can use this command instead - source /antenv3.6/bin/activate.
As a side note- The antenv will be available only after a deployment is initiated. Kindly check the “/” path from SSH and you should see a folder with name starting from antenv.
Browse to .python_packages/lib/python3.6/site-packages/ or .python_packages/lib/site-packages/. Kindly review the file path exists.
Review the Application logs as well (/home/LogFiles folder) from Kudu- https://<yourwebpp-name>.scm.azurewebsites.net/api/logs/docker
The App Service deployment engine automatically activates a virtual environment and runs
pip install -r requirements.txt
The requirements.txt file must be in the project root for dependencies to be installed.
For Django apps, App Service looks for a file named wsgi.py within your app code, and then runs Gunicorn using the following command:
is the name of the folder that contains wsgi.py
gunicorn --bind=0.0.0.0 --timeout 600 .wsgi
If you want more specific control over the startup command, use a custom startup command, replace with the name of folder that contains wsgi.py, and add a --chdir argument if that module is not in the project root.
For additional details, please checkout this document
Configure a Linux Python app for Azure App Service
Quickstart: Create a Python app in Azure App Service on Linux

Running flask as package in production

I am trying to deploy my flask app. Usually I would have an app.py and put all code in it.
app.py
templates/
|
|--index.html
for my really small projects. But then I have a slightly larger app and follow the larger app guide by flask.
So I have this:
setup.py
app/
__init__.py
views.py
models.py
forms.py
templates/
| ---index.html
I now have all my routes and views in views.py and running the app in __init__.py:
from flask import Flask
app = Flask(__name__)
import app.views # Name in setup.py
if __name__ == "__main__":
app.run()
(This is just an example)
So now I follow the guide by running it with pip install -e . and running with:
>set FLASK_APP=app(name I set in setup.py) flask run and it works. Except I do not know how to run it with one command. Since there is no one file to run I can not use gunicorn or anything like that. I am not sure how to go about executing this app. How would I run pip install . on the cloud server heroku?
My problem is because I have to import the app from __init__.py and views using import blog.[insert import] (models, views etc.) Any help is appreciated. Thank you.
EDIT: I do not want to use blueprints though. That might be too much. My app is medium, not small but not large either
You absolutely can use Gunicorn to run this project. Gunicorn is not limited to a single file, it imports Python modules just the same as flask run can. Gunicorn just needs to know the module to import, an the WSGI object to call within that module.
When you use FLASK_APP, all that flask run does is look for module.app, module.application or instances of the Flask() class. It also supports a create_app() or make_app() app factory, but you are not using such a factory.
Gunicorn won't search, if you only give it a module, it'll expect the name application to be the WSGI callable. In your case, you are using app so all you have to do is explicitly tell it what name to use:
gunicorn app:app
The part before the : is the module to import (app in your case), the part after the colon is the callable object (also named app in your module).
If you have set FLASK_APP as a Heroku config var and want to re-use that, you can reference that on the command line for gunicorn:
gunicorn $FLASK_APP:app
As for heroku, it can handle requirement.txt or setup.py
c.f. https://devcenter.heroku.com/articles/python-pip#local-file-backed-distributions
If your Python application contains a setup.py file but excludes a requirements.txt file, python setup.py develop will be used to install your package and resolve your dependencies.
If you already have a requirements file, but would like to utilize this feature, you can add the following to your requirements file:
-e .
And about run command, i think you cat put Procfile like
web: FLASK_APP=app flask run
or
web: FLASK_APP=app python -m flask run

No module named flask while running uWSGI

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

Categories

Resources