简体   繁体   English

在 python 中出现 flask 的 ModuleNotFound 错误

[英]Getting a ModuleNotFound error with flask in python

I installed the flask module via following command.[.[cmd image][1]][1] When I ran the following code.我通过以下命令安装了flask模块。[.[cmd image][1]][1] 当我运行以下代码时。

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
    #a = input('Enter name')
    return render_template(r'form.html')

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

It gave an error.它给出了一个错误。 ModuleNotFoundError: No module named 'flask'

Note:- I am not using virtualenv .注意:-我没有使用virtualenv And I am having my html page in templates folder [1]: https://i.stack.imgur.com/t4XzJ.png我在templates文件夹 [1] 中有我的html页面: https://i.stack.imgur.com/t4XzJ.png

It seems you have installed flask on the system wide environment.看来您已经在系统范围的环境中安装了 flask。 To access the flask in your virtualenv, you have to get into the virtualenv directory and activate it by using following command.要访问 virtualenv 中的 flask,您必须进入 virtualenv 目录并使用以下命令激活它。

source virtual_env_name/bin/activate

replace 'virtual_env_name' with the virtualenv name you created.将“virtual_env_name”替换为您创建的 virtualenv 名称。 After that in your command line, you will get (virtual_env_name) added before.之后在命令行中,您将获得(virtual_env_name)之前添加的内容。 the current directory path.当前目录路径。

Now you can install the flask framework using pip3.现在您可以使用 pip3 安装 flask 框架。

pip3 install flask

Keep in mind to activate venv everytime you are using the flask.请记住,每次使用 flask 时都要激活 venv。

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

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