Flask ImportError: No Module Named Flask
Try deleting the virtualenv you created. Then create a new virtualenv with:
virtualenv flask
Then:
cd flask
Now let's activate the virtualenv
source bin/activate
Now you should see (flask)
on the left of the command line.
Edit: In windows there is no "source" that's a linux thing, instead execute the activate.bat file, here I do it using Powershell: PS C:\DEV\aProject> & .\Flask\Scripts\activate
)
Let's install flask:
pip install flask
Then create a file named hello.py
(NOTE: see UPDATE Flask 1.0.2
below):
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
and run it with:
python hello.py
UPDATE Flask 1.0.2
With the new flask release there is no need to run the app from your script. hello.py
should look like this now:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
and run it with:
FLASK_APP=hello.py flask run
Make sure to be inside the folder where hello.py
is when running the latest command.
All the steps before the creation of the hello.py apply for this case as well
Python ModuleNotFoundError: No module named 'flask'
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.
ModuleNotFoundError: No module named 'flask'
pip
can for some reason point to system-wide pip
(which on many systems corresponds to Python 2.7). In order to use pip
from the virtualenv, use python -m pip
command. The following command will do the trick:
pip uninstall flask && python -m pip install flask
Another possibility is that you installed flask via apt
and not pip
. Here's the difference between the two: What is the difference between `sudo apt install python3-flask` and `pip3 install Flask`?
So now the flask
command is available system-wide.
If this is the case, uninstalling flask with apt
and installing it with pip
should do the trick:
sudo apt remove python-flask
pip install flask
(this is my guess that the apt
package is called python-flask
.
ModuleNotFoundError: No module named 'flask' - Ubuntu Remote Server
@Abhilash looks like the problem isn't the code, it's about the setup.
By default, Python will look for flask environment configurations in the home directory, which generally is stored in the .env
file. In your case that is the virtual environment. Hence the error.
Follow the steps below:
Go to your user's document directory, e.g.
cd /home/rahul/Documents/
Create a separate folder for your flask application:
mkdir flask_test_app
Go into the above directory:
cd flask_test_app
Create Virtual Environment here:
virtualenv .env
Activate this virtual environment:
source .env/bin/activate
It should show the something like below:
(.env) rahul@ramco:~/Documents/flask_test_app
Install Flask:
pip install Flask
Put the flask app file created in the current folder, i.e., app.py file.
Run the application:
python app.py
This will start the flask app on flask's default port 5000, and will display something like below in the terminal:
* Tip: There are .env files present. Do "pip install python-dotenv" to use them.
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Tip: There are .env files present. Do "pip install python-dotenv" to use them.
* Debugger is active!
* Debugger PIN: 303-406-109
NOTE: Try to avoid creating a virtual environment in the home directory, if you have to then use some other name like .venv
or venv
. So that it won't conflict with the default configurations.
Related Topics
How to Open Different Urls At the Same Time by Using Python Selenium
How to Check Whether All Elements of Array Are in Between Two Values
Hiding Axis Text in Matplotlib Plots
Regex to Match Digits and At Most One Space Between Them
Python: Requests.Exceptions.Connectionerror. Max Retries Exceeded With Url
How to Copy/Repeat an Array N Times into a New Array
Retrieve Top N in Each Group of a Dataframe in Pyspark
Python Multiprocessing Pool Hangs At Join
Windowserror: [Error 126] the Specified Module Could Not Be Found
A Better Way Than Looping and Calling Functions That Loop and Call Another Functions
How to Move to One Folder Back in Python
How to Share Single Sqlite Connection in Multi-Threaded Python Application
How to Extract Rar Files Inside Google Colab
How to Get Max() to Return Variable Names Instead of Values in Python
Pandas, Remove Everything After Last '_'
Check If File Has a CSV Format With Python
Permissionerror: [Errno 13] Permission Denied
Pandas Open_Excel() Fails With Xlrd.Biffh.Xlrderror: Can't Find Workbook in Ole2 Compound Document