23

I'm trying to use flask and python. I did a simple file named hello.py. tHis file contains this code:

from flask import Flask
app = Flask(__name__)


@app.route("/")
def main():
    return "Welcome!"

if __name__ == "__main__":
    app.run()

This is a simple hello world with flask. I want to execute it but actually, I have a problem. In the terminal, I typed python hello.py and I get this error:

File "hello.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask

Even that I installed flask globally. I understand that this is a basic question, but I'm stuck?

Lindages
  • 399
  • 1
  • 3
  • 12

6 Answers6

23

You don't have installed flask

Linux:

Install flask as global package:

sudo pip install flask

Install in virtualenv

virtualenv venv
source venv
pip install flask

Install system package

  • debian, ubuntu

    apt-get install python-flask
    
  • arch

    pacman -S python-flask
    
  • fedora

    yum install python-flask
    

Install via Anaconda

conda install flask

Windows:

python -m pip install flask
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
1

You've installed flask for python2 you need to use sudo pip3 install ... to get it for python3. I had spent a day when I got that I was using pip for python 2.

Ata
  • 33
  • 1
  • 11
0

You have to install flask with pip install flask

Noah -
  • 33
  • 7
0

Make sure that you entered the correct entry into the requirements.txt file. It should not be blank.

enter Flask==2.0.0 into the requirements.txt file.

  • 1
    Rather than editing the requirements.txt file manually, it should be auto-generated it so that you are sure it matches your current environment. See [Automatically create requirements.txt](https://stackoverflow.com/questions/31684375/automatically-create-requirements-txt) – Gino Mempin May 12 '21 at 23:37
  • It was nice to know that it could be generated automatically. – Snehasish Chowdhury May 14 '21 at 02:00
0

Try to install flask package globally using the following command,

pip install flask

Still you are having the same error then you have more than one version of python installed in your machine. So better to create a virtual environment for your app and install all package in it. For that install virtual environment.

py -m pip install --user virtualenv

Activate your venv by

.\venv\Scripts\activate.bat

Now try to install flask in the venv

python -m virtualenv venv
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

if you install the flask and still get the error. Then, You should activate the source by following:

source env/bin/activate

Borhan
  • 1
  • 1
  • 1
    This solution is already provided in another answer. If you have any different solution please mention it. If there is any change/extension required to the original answer, then edit it. – Azhar Khan Dec 17 '22 at 08:11