简体   繁体   English

from Flask import Flask ImportError: No module named Flask

[英]from Flask import Flask ImportError: No module named Flask

I am following the tutorial here .我正在关注这里的教程。

My file looks like this:我的文件看起来像这样:

from flask import Flask
app = Flask(__name__)

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

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

I run python app.py and get the following:我运行python app.py并得到以下信息:

Traceback (most recent call last):
File "app.py", line 1, in <module>
from flask import Flask
ImportError: No module named Flask

I do have flask installed.我确实安装了烧瓶。 I was thinking it's a $PATH issue.我在想这是一个 $PATH 问题。 I don't really know where to begin as far as troubleshooting goes.就故障排除而言,我真的不知道从哪里开始。

which flask gives me: /usr/local/bin/flask which flask给了我: /usr/local/bin/flask

which python gives me: /usr/bin/python which python给了我: /usr/bin/python

Any help is much appreciated, there are other similar issues out there but those solutions have not helped.非常感谢任何帮助,还有其他类似的问题,但这些解决方案没有帮助。 Happy to answer any questions.很高兴回答任何问题。 Thank you.谢谢。

Answers to questions:问题的回答:

Q. Which Python version?问:哪个 Python 版本? A. Python 2.7.10一、 Python 2.7.10

Q. How did you install Flask?:你是如何安装 Flask 的? A. pip install flask A. pip 安装烧瓶

在运行您的 app.py 之前,您需要通过此命令激活您的服务器

. venv/bin/activate

It looks like that you are using linux, if you are using ubuntu try: (similar for other linux distributions)看起来您正在使用 linux,如果您使用的是 ubuntu,请尝试:(其他 linux 发行版类似)

sudo apt-get install python-flask

It helped me install many packages that pip fails to install.它帮助我安装了许多 pip 无法安装的软件包。

The app package and run.py file need to be in the same directory level. app包和run.py文件需要在同一目录级别。

app\
    templates\
    __init__.py
venv\
run.py

Above folder structure should fix.以上文件夹结构应该修复。

PS pip install all necessary packages into virtual enviroment ( venv in my example) PS pip install所有必需的包pip install到虚拟环境中(在我的示例中为venv

You can try to uninstall and to reinstall it again(you can follow this link for more detaile) :您可以尝试卸载并重新安装它(您可以点击此链接了解更多详情):

http://flask.pocoo.org/docs/0.12/installation/#installation http://flask.pocoo.org/docs/0.12/installation/#installation

You can take anaconda and there you can make enviroment for every version of python .Anaconda is getting very easy from this link: https://docs.anaconda.com/anaconda/install/您可以使用 anaconda,在那里您可以为每个版本的 python 制作环境。从这个链接中,Anaconda 变得非常容易: https ://docs.anaconda.com/anaconda/install/

Then you can open terminal and pip install flask (if you are on windows) or conda pip install flask .然后你可以打开终端和 pip install flask (如果你在 Windows 上)或 conda pip install flask 。

I recommand to use python3.5 not python 2.7 .我建议使用 python3.5 而不是 python 2.7 。

if you are on Python 3, try installing flask with如果您使用的是 Python 3,请尝试使用以下命令安装烧瓶

pip3 install flask

and if you fail (you are likely to get an command prompt error as below)如果失败(您可能会收到如下命令提示错误)

'pip3' is not recognized as an internal or external command, operable program or batch file.

try试试

py -3 -mpip install flask on windows py -3 -mpip install flask在 Windows 上py -3 -mpip install flask

this command installs flask module on python 3, and then you could run your script.这个命令会在 python 3 上安装 Flask 模块,然后你就可以运行你的脚本了。 worked for me!为我工作!

Python 3 :蟒蛇3:

pip3 install flask

Python 2 :蟒蛇2:

pip install flask

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

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