【问题标题】:flask run giving me ModuleNotFoundError烧瓶运行给我 ModuleNotFoundError
【发布时间】:2021-04-27 09:31:16
【问题描述】:

我对 python 比较陌生,正在尝试构建一个烧瓶服务器。我想做的是有一个名为“endpoints”的包,它有一个模块列表,其中每个模块定义应用程序路由的子集。当我使用以下代码创建一个名为 server.py 的文件时,它的工作原理是这样的

import os

from flask import Flask


app = Flask(__name__)

from endpoint import *

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

现在只有一个名为 hello.py 的端点模块,它看起来像这样

from __main__ import app


# a simple page that says hello
# @app.route defines the url off of the BASE url e.g. www.appname.com/api + 
#    @app.route
# in dev this will be literally http://localhost:5000/hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

所以...当我运行 python server.py 时,上述工作有效,当我尝试使用 flask 运行应用程序时会出现问题。

它只是调用__init__.py 而不是 server.py,看起来像这样

import os

from flask import Flask

# create and configure the app
# instance_relative_config states that the 
#     config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
try:
    os.makedirs(app.instance_path)
except OSError:
    pass

from endpoint import *

当我在终端运行flask run 时,我得到ModuleNotFoundError: No module named 'endpoint'

但如果我再次更改代码使其如下所示,那么 flask run 可以工作。

import os

from flask import Flask

# create and configure the app
# instance_relative_config states that the 
#     config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
try:
    os.makedirs(app.instance_path)
except OSError:
    pass

# a simple page that says hello
# @app.route defines the url off of the BASE url e.g. www.appname.com/api + 
#   @app.route
# in dev this will be literally http://localhost:5000/hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

我很确定会发生这种情况,因为我不完全了解导入的工作原理...

那么,我如何设置__init__.py 以便它从“端点”包中导入所有模块并在我调用flask run 时工作?

【问题讨论】:

    标签: python flask import package modulenotfounderror


    【解决方案1】:

    当您使用__init__.py 文件时,Python 将目录视为一个包。

    这意味着,您必须从包中导入,而不是直接从模块中导入。

    另外,通常您不会在 __init__.py 文件中添加太多或任何代码。

    您的目录结构可能如下所示,其中stack 是我使用的包的名称。

    stack/
    ├── endpoint.py
    ├── __init__.py
    └── main.py
    

    __init__.py文件是空的。

    ma​​in.py

    import os
    
    from flask import Flask
    
    # create and configure the app
    # instance_relative_config states that the 
    #     config files are relative to the instance folder
    app = Flask(__name__, instance_relative_config=True)
    
    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    
    from stack.endpoint import *
    

    端点

    from stack.main import app
    
    # a simple page that says hello
    # @app.route defines the url off of the BASE url e.g. www.appname.com/api + 
    #   @app.route
    # in dev this will be literally http://localhost:5000/hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'
    

    然后您可以运行您的应用程序...

    export FLASK_APP=main.py
    # followed by a...
    flask run
    

    这就是说,当我创建一个新的 Flask 应用程序时,我通常只使用一个文件,这使得初始开发更容易,并且只有在应用程序真正变大时才拆分为模块。

    另外,为了分离视图或者我们称之为子包,Flask 提供了所谓的Blue prints。您现在无需担心这一点,但在尝试将应用拆分为子应用时尤其方便。

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      相关资源
      最近更新 更多