Python "ModuleNotFoundError: No module named 'flask'" - python

I am a beginner with the python programming.
I have python 3 installed in my local system.
I coding along as part of a tutorial video and as part of the tutorial, i have created a virtual environment and created an app.py file with the below content.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, World!"
if __name__ == "__main__":
app.run()
I have installed all the dependencies like flask and pytest in the virtual environment as per the tutorial using gitbash.But when i run the command python3 app.py in gitbash i get the below error message
File "C:\path\Python\python-github-actions-example\src\app.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'
(myvenv)
I checked the python version and it is python 3.9.7
If i run python app.py i get the output.
Why is it not running even though correct version is installed
Any idea why ?

Try to delete the venv or make a new one.
Then create a new venv like this:
virtualenv flask
Go to the flask directory
: cd flask
Activate it: scripts\activate
You should see (flask) on the left of the command line.
Install flask again: pip install flask
Run your file again.

Some times you need to change/select de versiĆ³n/env if you after installed. like it if work for you.

After you do what #kmogi says in their post above, start the app with python not python3.

Related

Flask fails with "Error: While importing 'X', an ImportError was raised", but does not display the error. How to find the source of the error?

When starting a Flask app with:
$ flask run
I received the error:
Error: While importing 'wsgi', an ImportError was raised.
Usage: flask [OPTIONS] COMMAND [ARGS]...`
...
However, there is no stack trace or other information provided. What is the best way to get the ImportError stack trace?
Import the Flask app at the Python interpreter prompt
To see the ImportError stack trace, open a Python interpreter prompt and import the module that loads the Flask app (usually app.py or wsgi.py). If applicable, be sure that your virtual environment is activated.
$ python
>>> from my_app_folder import app
Set the FLASK_APP environment variable
If you can import the Flask app module using the Python interpreter without error, try setting the FLASK_APP environment variable to point to the Flask app module.
$ FLASK_APP='my_app_folder/app' FLASK_ENV=development flask run
This error can be caused if Flask is unable to import any libraries(in my case it was Flask_restful)
This is the workaround I found to find the missing libraries:-
I found which library was missing by just running the Flask App file (wsgi.py) directly with python
python wsgi.py
which gave an Importerror listing the missing libraries
after finding the missing libraries, simply install libraries using pip, for me Flask_restful was missing so I installed Flask_restful
.
After installing missing libraries simply run the flask app using
flask run
The only thing that I would add to Christopher Peisert's answer is the option that gave me the error messages that I was searching for:
(venv) ~/example_flask_app/$ python
>>> import app
Or in my case
>>> import microblog

Can not import a module when run the flask server using the flask run command

I'm new to flask framework and I want to write a simple flask app that uses another python module (librosa package). I have successfully installed librosa in the same virtual environment that I have installed flask and I can easily import it in the python interpreter. Here is the python script.
# app.py
from flask import Flask
import librosa
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
if __name__ == '__main__':
app.run()
The problem is that when I want to run the flask app using the flask run command (after setting export FLASK_APP=app.py) I get the ModuleNotFoundError error as follows:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "/home/masoud/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/masoud/projects/my_projects/seperator_rewriten/app.py", line 2, in <module>
import librosa
ModuleNotFoundError: No module named 'librosa'
The strange thing is that there is no import error when I run the flask server using python app.py command.
if you are using an IDE such as pycharm, then you may need to install it from the terminal in the IDE itself, not cmd
This may be the solution to your problem:
python -m flask run
The reason is that flask run may use the python executable somewhere else, not the virtual environment you created for the project.
You can add these lines of code in your script:
import sys
import flask
print(sys.executable)
print(flask.__version__)
And then you can check the python executable path using python app.py and flask run.

cannot import name 'Flask'

Im using window 10, I did the command pip install flask but I kept getting a ImportError: cannot import name 'Flask'. When I worked with flask couple months back it was running fine. Came back to run my old programs today and i get this error? I was just trying to run a simple html website.
from flask import Flask, render_template
from flask import request
app = Flask(__name__)
app.static_folder = 'static'
#app.route('/')
def index():
return render_template('index.html')
if __name__=='__main__':
app.run(debug = True)
Also before this error I had No module named 'Flask' so I did(found this in other stackoverflow post):
1. virtualenv
2. pip install flask (getting output that requirements are already satisfied)
3. Then I just try to run my flask which is called i.py and I get cannot import name 'Flask'. Went through many solutions on here still no idea how to fix it.
Basically i had 2 versions of python 3.6 and 3.5.2 when I deleted 3.6 its working fine.

python3 : Cannot import name flask

I tried the following simple code,
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
it is running fine with,
python hello.py
but it gives an error when i try with python3
ImportError: cannot import name 'Flask'
A package is installed against a specific Python version/location. Installing Flask for Python 2 (which is probably what the python and pip commands are aliased to), doesn't install it for Python 3.
You should really just use virtualenv to control exactly what versions and packages you are using.
This creates a Python 3 environment and installs Flask:
virtualenv -p /usr/bin/python3 my_py3_env
source my_py3_env/bin/activate
pip install flask
When you open a new terminal, just source the activate script again to keep using the environment.

python flask import error

I am running the following code
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
and getting the following error
Traceback (most recent call last):
File "test.py", line 1, in <module>
from flask import Flask
File "/home/pi/programs/flask.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
I have tried installing flask through various methods , but still the problem persists
also, is there any alternative to flask???
I ran into this error because I named the test file as flask.py and tried to run it! It creates namespace conflict with the real flask module!
Delete the local test file that you named flask.py and the respective flask.pyc. Give some other name! This will happen with other modules like socket etc where you are likely to give the same name for the test file as the standard module :-)
The reason is your python file name is flask.
Just run apt-get install python3-flask
Edited to install the python3 version, since nobody should be using python2 now.
Just rename flask.py file also delete flask.pyc file
It's because of the name flask.py. It will import itself if the name is flask.py. Change the name and try again.
Restart virtual environment
$ virtualenv flask
Into dir flask run
$source ./bin/activate
Install python module again
$pip install "module"
I had the same issue. Apparently you can't name your file socket.py either.
exactly when we create file name as flask.py and then execute it for the first time it will execute and at the same time framework creates another file called flask.pyc .Now stop the process and start it again it will throw this error as instead of looking to actual framework flask file it is looking in to the one you created . To solve this problem
Go to the folder you created flask.py and delete flask.pyc and then rename flask.py to some test_1.py ..save it and execute it . You should see no errors .
Add PYTHONPATH to your environment:
export PYTHONPATH=/root/environments/my_env/lib/python3.6/site-packages/

Categories

Resources