简体   繁体   English

Flask - ImportError:没有名为 app 的模块

[英]Flask - ImportError: No module named app

First I created __init__.py首先我创建了__init__.py

from flask import Flask

app = Flask(__name__)

Then in a separate file, in the same directory, run.py然后在一个单独的文件中,在同一目录中, run.py

from app import app 

app.run(
    debug = True
)

When I try to run run.py , I get the error当我尝试运行run.py时,出现错误

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

__init__.py is imported using a directory. __init__.py是使用目录导入的。 if you want to import it as app you should put __init__.py file in directory named app如果要将其作为app导入,则应将__init__.py文件放在名为app的目录中

a better option is just to rename __init__.py to app.py更好的选择是将__init__.py重命名为app.py

This is probably an error in flask application's folder structure.这可能是烧瓶应用程序的文件夹结构中的错误。
Anyone looking for a simple beginner-friendly structure for the flask project may find this helpful:任何为烧瓶项目寻找简单的初学者友好结构的人都可能会发现这很有帮助:

   |__movies 
     |__run.py 
     |__app     
        ├── templates
        │   └── index.html
        │   └── signup.html
        └── __init__.py
        └── routes.py

Here ' movies ' is the name given for the main application.这里的“电影”是主应用程序的名称。 It contains ' run.py ' and a folder called ' app '.它包含“ run.py ”和一个名为“ app ”的文件夹。 ' app ' folder contains all necessary flask files such as ' templates ' folder, ' __init __.py ', and ' routes.py '. app ”文件夹包含所有必要的烧瓶文件,例如“ templates ”文件夹、“ __init __.py ”和“ routes.py ”。

Contents of:内容:

run.py :运行.py

from app import app

__init__.py : __init__.py

from flask import Flask

app = Flask(__name__)

from app import routes


app.run(debug=True)

routes.py :路线.py

from app import app

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

Your __init__.py file needs to go in the folder named app, not the same directory as the run.py file.您的__init__.py文件需要放在名为 app 的文件夹中,而不是与 run.py 文件相同的目录。

from app import app is looking in the app folder, so the __init__.py file needs to sit in there. from app import app正在查看 app 文件夹,因此__init__.py文件需要放在其中。

Ensure to set your PYTHONPATH to the src/ directory as well.确保也将 PYTHONPATH 设置为 src/ 目录。 Example export PYTHONPATH="$PYTHONPATH:/path/to/your/src"示例export PYTHONPATH="$PYTHONPATH:/path/to/your/src"

(In case someone else is lost even after trying other solutions..) (万一其他人在尝试其他解决方案后迷路了..)

I get the No module named app error only during Debugging, not Running , in my IDE (VSCode)我在我的 IDE (VSCode) 中仅在 Debugging 而不是 Running 期间收到No module named app错误

That's because I had set debug=True in my app.py's __main__ (which auto-reloads flask when we save code changes), like so :那是因为我在 app.py 的 __main__ 中设置了debug=True (当我们保存代码更改时它会自动重新加载烧瓶),如下所示:

app.run(debug=True)应用程序运行(调试=真)

How to fix - set debug=False in above :如何修复 - 在上面设置debug=False

app.run(debug=False) app.run(调试=假)

and error goes away.并且错误消失了。

只需将您的文件重命名为 app.py 即可。

对我来说, export PYTHONPATH=/path/to/your/src && python app/main.py有效

I solved this as follows -我解决了这个问题 -

$export FLASK_APP=run

$flask run

After executing this command.执行此命令后。 I don't get any error, my app running smoothly.我没有收到任何错误,我的应用程序运行顺利。

This may be an isolated case, but for me this was a VS Code issue.这可能是一个孤立的案例,但对我来说这是一个 VS Code 问题。 The "no module found" error would only happen when debug=True. “未找到模块”错误只会在 debug=True 时发生。

In VS Code, you can "Run Python File" or "Debug Python File".在 VS Code 中,您可以“运行 Python 文件”或“调试 Python 文件”。 I was using debug in VS Code AND I had app.run(debug=True) .我在 VS Code 中使用调试并且我有app.run(debug=True) Once I just ran the file normally in VS Code, the problem went away and Flask debugger is working as expected.一旦我在 VS Code 中正常运行文件,问题就消失了,Flask 调试器按预期工作。

I guess it doesn't like 2 layers of Inception!我猜它不喜欢 2 层的 Inception!

In case anybody else is still struggling with this.以防其他人仍在为此苦苦挣扎。 I had the __init__.py<\/code> in the app<\/code> folder but it still didn't work.我在app<\/code>文件夹中有__init__.py<\/code> ,但它仍然无法正常工作。 Running the following command in app<\/code> parent<\/em> folder worked for me.app<\/code>父<\/em>文件夹中运行以下命令对我有用。

export PYTHONPATH=$PWD

you are probably running from inside your app folder.您可能是从您的应用程序文件夹中运行的。 Move out to the previous directory and run the command again.移出到上一个目录并再次运行该命令。

I just want to leave this solution for whom other solutions aren't working.我只想将此解决方案留给其他解决方案不起作用的人。

Assuming here the module "app" is actually referring to your "app.py" source file, in the app.run() ensure to set debug to FALSE ie app.run(debug=False) .假设此处模块“app”实际上是指您的“app.py”源文件,在 app.run() 中确保将调试设置为 FALSE,即app.run(debug=False) Otherwise cd to the folder in which your app.py file is and then run python app.py否则cd到你的 app.py 文件所在的文件夹,然后运行python app.py

These are workarounds, but it seems there is a bug in the debug=True flow as old as 2016-17, perhaps it hasn't been fixed yet这些是解决方法,但似乎早在 2016-17 年的 debug=True 流程中就有一个错误,也许它还没有被修复

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

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