python烧瓶导入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14792605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:27:01  来源:igfitidea点击:

python flask import error

pythonflaskimporterror

提问by Manarjan Singh Ghai

I am running the following code

我正在运行以下代码

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

and getting the following error

并收到以下错误

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from flask import Flask
  File "/home/pi/programs/flask.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name Flask

I have tried installing flask through various methods , but still the problem persists

我尝试通过各种方法安装烧瓶,但问题仍然存在

also, is there any alternative to flask???

还有,烧瓶有什么替代品吗???

采纳答案by LtWorf

Just run apt-get install python3-flask

赶紧跑 apt-get install python3-flask

Edited to install the python3 version, since nobody should be using python2 now.

编辑安装 python3 版本,因为现在没有人应该使用 python2。

回答by Nishant

I ran into this error because I named the test fileas flask.pyand tried to run it! It creates namespace conflictwith the real flask module!

我遇到了这个错误,因为我将测试文件命名为flask.py并尝试运行它!它会与真正的烧瓶模块产生命名空间冲突

Deletethe local test filethat you named flask.pyand the respective flask.pyc. Give some other name! This will happen with other modules like socketetc where you are likely to give the same name for the test file as the standard module :-)

删除您命名为flask.py本地测试文件和相应的flask.pyc。给个别的名字吧!这将发生在其他模块(如etc)中,您可能会为测试文件提供与标准模块相同的名称:-)socket

回答by hardik k

Just rename flask.pyfile also delete flask.pycfile

只需重命名flask.py文件同时删除flask.pyc文件

回答by Vijay

exactly when we create file name as flask.py and then execute it for the first time it will execute and at the same time framework creates another file called flask.pyc .Now stop the process and start it again it will throw this error as instead of looking to actual framework flask file it is looking in to the one you created . To solve this problem Go to the folder you created flask.py and delete flask.pyc and then rename flask.py to some test_1.py ..save it and execute it . You should see no errors .

正是当我们将文件名创建为 flask.py 然后第一次执行它时它会执行,同时框架创建另一个名为 flask.pyc 的文件。现在停止进程并再次启动它会抛出这个错误,而不是查看实际框架烧瓶文件时,它正在查看您创建的文件。解决这个问题 进入你创建的flask.py文件夹并删除flask.pyc,然后将flask.py重命名为一些test_1.py ..保存并执行它。您应该看不到任何错误。

回答by user6699563

The reason is your python file name is flask.

原因是你的python文件名是flask。

回答by Jose Angel Lopez

Restart virtual environment

重启虚拟环境

$ virtualenv flask

Into dir flask run

进入目录烧瓶运行

$source ./bin/activate

Install python module again

再次安装python模块

$pip install "module"

回答by Chase Roberts

I had the same issue. Apparently you can'tname your file socket.pyeither.

我遇到过同样的问题。显然你也不能命名你的文件socket.py

回答by user9200165

It's because of the name flask.py. It will import itself if the name is flask.py. Change the name and try again.

这是因为名称是flask.py。如果名称为flask.py,它将自行导入。更改名称并重试。

回答by Carlos Oliveira

Add PYTHONPATHto your environment: export PYTHONPATH=/root/environments/my_env/lib/python3.6/site-packages/

添加PYTHONPATH到您的环境: export PYTHONPATH=/root/environments/my_env/lib/python3.6/site-packages/