Trying to run a python file in flask - python

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?

Related

Python: "Cannot import flask"

I tried everything but couldn't import app.py. It gives me the following error
set FLASK_APP = "C:\Users\Hp\PycharmProjects\sakshi.py\app.py"
(env) C:\Users\Hp\Documents\flask_app>flask run
Serving Flask app " app.py"
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]
Error: Could not import " app".
Your help would be much appreciated!
Change the sever environment to development env.
export FLASK_APP=yourapp
export FLASK_ENV=development
and then start the server with:
flask run
If you still get error try this way:
set FLASK_ENV=development
flask run
And then run your python file.
At least one problem: spaces in the set command. From the syntax in the microsoft doc:
set [<Variable>=[<String>]]
set [/p] <Variable>=[<PromptString>]
set /a <Variable>=<Expression>
There is no space on either side of the = in the set command.
And, the path name "C:\Users\Hp\PycharmProjects\sakshi.py\app.py" is questionable. It is, at the very least, a bad idea to name a directory sakshi.py.
I could see the problem is with space between the variable and value in set command,
In windows,
set var=value
In *nix family,
export var=value
Your script name is app.py which by default flask recognizes as entry point for the app.
You only have to explicitly set the FLASK_APP variable when you are running from another directory (which you are) or the name of the script is something other than app.py. You can directly run the flask run command from the project base directory itself.

ModuleNotFoundError: No module named 'verovio'

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.

Error: Could not locate a Flask application in VSCode

I am trying to learn Flask using VScode.
The tutorial that I am following is: Python Flask Tutorial: Full-Featured Web App Part 1 - Getting Started.
I did the following things:
Created a new virtualenv in a folder using: virtualenv venv
activated it as: venv\Scripts\activate (I am on Windows 10)
After that, I created a new directory named Flask_Blog using mkdir Flask_Blog and in it, I created a new flaskblog.py file containing the following code:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
Then, in the terminal of VScode, I changed my working directory in order to be in the Flask_Blog directory using cd Flask_Blog.
Now, when I am doing set FLASK_APP=flaskblog.py followed by flask run, I am getting the following error:
(venv) PS C:\Users\kashy\OneDrive\Desktop\Flask\Flask_Blog> flask run
* 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]
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
But
When I do the same in the cmd prompt, the code runs and I get to see the output.
I am completely new to this. Can anyone please tell me what is the mistake I am doing in VSCode and why is it working in the cmd?
Issue raised in VsCode
Under Powershell, you have to set the FLASK_APP environment variable as follows:
$env:FLASK_APP = "webapp"
Then you should be able to run "python -m flask run" inside the hello_app folder. In other words, PowerShell manages environment variables differently, so the standard command-line "set FLASK_APP=webapp" won't work.
Try Set FLASK_APP = Full path of the folder/filename.py.
This worked for me
This worked for me on the VSCode:
$env:FLASK_APP= 'C:\Python\ex003\main:app'

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.

flask.cli.NoAppException: The file/path provided (new_app.py) does not appear to exist

I keep getting this error flask.cli.NoAppException: The file/path provided (new_app.py) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py it goes away after I restart the Flask server.
I am running flask run in the correct directory where my app is. This just started happening after working for 2 weeks. I've read that it could be due to an import error, but I am not finding any modules that are not installed on my virutalenv.
from flask import Flask
app = Flask(__name__)
app.debug=True
Most likely you haven't set the FLASK_APP environment variable.
To run the application you can either use the flask command or
python’s -m switch with Flask. Before you can do that you need to tell
your terminal the application to work with by exporting the FLASK_APP
environment variable:
$ export FLASK_APP=hello.py
$ flask run * Running on http://127.0.0.1:5000/
If you are on Windows you need to use set
instead of export.
Alternatively you can use python -m flask:
$ export FLASK_APP=hello.py
$ python -m flask run * Running on http://127.0.0.1:5000/
EDIT
If you have FLASK_APP set then try adding this to new_app.py
app.run(debug=True, port=8800)
Or if you're on Windows:
if __name__ == '__main__':
app.run(debug=True, port=8800)
And then just execute the app with python new_app.py.

Categories

Resources