简体   繁体   English

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

[英]ModuleNotFoundError: No module named 'flask'

After reading title of this post, don't try to make duplicate first because herewith content may be asked in different way.阅读本帖标题后,请勿尝试先复制,因为此处可能会以不同的方式询问内容。 Btw, I'm very new in python and start learning now for work requirement.顺便说一句,我是 python 的新手,现在开始学习工作要求。

here are my dependencies这是我的依赖项

virtualenv --version => 15.0.2

pip --version => 19.0.3

flask --version => 1.0.2, Python 2.7.10 (default, Aug 17 2018, 19:45:58)

python --version => 3.7.1

And, here is my source code of main.py而且,这是我的main.py源代码

from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World!"

Problem is following error encountered when I render like python main.py问题是当我像python main.py渲染时遇到以下错误

Traceback (most recent call last): File "main.py", line 1, in from flask import Flask ModuleNotFoundError: No module named 'flask'回溯(最后一次调用):文件“main.py”,第 1 行,从 flask 导入 Flask ModuleNotFoundError:没有名为“flask”的模块

But when I render like FLASK_APP=main.py flask run , it was working.但是当我像FLASK_APP=main.py flask run这样渲染时,它正在工作。 Please let me know how's difference between python... and FLASH_APP=...请让我知道python...FLASH_APP=...之间的区别

pip can for some reason point to system-wide pip (which on many systems corresponds to Python 2.7). pip出于某种原因可以指向系统范围的pip (在许多系统上对应于 Python 2.7)。 In order to use pip from the virtualenv, use python -m pip command.为了使用 virtualenv 中的pip ,请使用python -m pip命令。 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 .另一种可能性是您通过apt而不是pip安装了烧瓶。 Here's the difference between the two: What is the difference between `sudo apt install python3-flask` and `pip3 install Flask`?两者的区别如下: `sudo apt install python3-flask` 和 `pip3 install Flask` 有什么区别?

So now the flask command is available system-wide.所以现在flask命令在系统范围内可用。

If this is the case, uninstalling flask with apt and installing it with pip should do the trick:如果是这种情况,使用apt卸载烧瓶并使用pip安装它应该可以解决问题:

sudo apt remove python-flask
pip install flask

(this is my guess that the apt package is called python-flask . (这是我猜测apt包被称为python-flask

As Pavel said;正如帕维尔所说; you might use python 2.7 instead of python 3 to run your project...您可能会使用 python 2.7 而不是 python 3 来运行您的项目...

I had this issue and by using this command instead of Pavel's command, problem fixed我遇到了这个问题,通过使用这个命令而不是 Pavel 的命令,问题得到了解决

pip uninstall flask && python3.7 -m pip install flask

You can replace flask by any other lib and this will work您可以用任何其他库替换烧瓶,这将起作用

Hope it works for everyone sees this too希望它适用于每个人也看到这个

如果您使用带有 pipenv 的 Visual Studio Code IDE 并收到上述错误,则应检查此链接。

The previous answers are fine but you may need to be careful of the version of python you use to install flask this time.前面的答案很好,但是这次你可能需要小心你用来安装flask的 python 的版本。 For me,为了我,

pip3 uninstall flask && python -m pip install flask

did not work, but没用,但是

pip3 uninstall flask && python3 -m pip install flask

did work.确实有效。 The issue of having the right version when installing flask and its installation in the virtual environment also has consequences.安装 flask 及其在虚拟环境中安装时版本正确的问题也会产生后果。 Find out more in this article: ModuleNotFoundError .在本文中了解更多信息: ModuleNotFoundError

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

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