简体   繁体   English

Python “ModuleNotFoundError:没有名为‘flask’的模块”

[英]Python "ModuleNotFoundError: No module named 'flask'"

I am a beginner with the python programming.我是 python 编程的初学者。 I have python 3 installed in my local system.我在本地系统中安装了 python 3。 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.我作为教程视频的一部分进行编码,作为教程的一部分,我创建了一个虚拟环境并创建了一个包含以下内容的 app.py 文件。

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我已经按照教程使用 gitbash 在虚拟环境中安装了 flask 和 pytest 等所有依赖项。但是当我在 gitbash 中运行命令 python3 app.py 时,我收到以下错误消息

 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我检查了 python 版本,它是 python 3.9.7

If i run python app.py i get the output.如果我运行 python app.py 我得到 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.尝试删除venv或创建一个新的。

  1. Then create a new venv like this: virtualenv flask然后像这样创建一个新的venvvirtualenv flask

  2. Go to the flask directory : cd flask转到烧瓶目录: cd flask

  3. Activate it: scripts\\activate激活它: scripts\\activate

    • You should see ( flask ) on the left of the command line.您应该在命令行的左侧看到 ( flask )。
  4. Install flask again: pip install flask再次安装flask: pip install flask

  5. Run your file again.再次运行您的文件。

After you do what @krmogi says in their post above: https://stackoverflow.com/a/69623603/13855522在你按照@krmogi 在上面的帖子中说的话之后: https ://stackoverflow.com/a/69623603/13855522

Start the app with python app.py not python3 app.py使用python app.py而不是python3 app.py启动应用程序

Some times you need to change/select de versión/env if you after installed.如果安装后,有时您需要更改/选择 de 版本/环境。 like it if work for you.喜欢它,如果为你工作。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM