相关文章推荐
听话的硬盘  ·  flask-socketio的connect ...·  1 周前    · 
傻傻的松树  ·  npm ...·  3 月前    · 
刚毅的硬币  ·  sendmailR ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

Had this before, have you got more than one instance of python running? Sometimes Flask installs in 3 for example but the terminal/command line is in 2 etc – Daniel Casserly Jan 14, 2016 at 10:12 Please run python -m pip list? Is Flask listed? If not, run python -m pip install flask. – dirn Jan 14, 2016 at 12:44 I'm not sure since OP said: Even that I installed flask globally. I understand that this is a basic question, but I'm stuck? – Remi Guan Jan 14, 2016 at 9:46

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.

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 – Gino Mempin May 12, 2021 at 23:37
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

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

source env/bin/activate

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, 2022 at 8:11

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.