【问题标题】:Flask don't work with debug modeFlask 不适用于调试模式
【发布时间】:2017-12-19 22:20:40
【问题描述】:

我正在处理我的小包,我遇到了奇怪的werkzeug 问题。

问题: 当我尝试在 debug 模式下运行我的应用程序时,为了运行我的应用程序,我使用了命令python3 -m my_app --start

我收到了错误 ModuleNotFoundError: No module named '__main__.helpers'; '__main__' is not a package

文件很简单

app.py

class Application:
    """Main class"""
    def __init__(self):
        self.reader = ApplicationReader()
        self.app = Flask(self.reader.name)

    def __start_server(self):
        """Custom method, for starting flask application"""
        self.app.run(debug=True)

    def run(self):
        """Run the server"""
        return self.__start_server()


# Instance of Application class
application = Application()


def start_app():
    """Start the mock server
    """
    return application.run()

__main__.py

def cli(run, init, start):
    if run:
        print('Your API is running...')
    if init:
        create_init_file()
    if start:
        start_app()

if __name__ == '__main__':
    cli()

而且似乎无法修复该错误,GitHub issue

那么,也许有人对此案有解决方案?谢谢。

【问题讨论】:

    标签: python flask werkzeug


    【解决方案1】:

    我为start_app函数创建了一个不同的文件,因为我发现在调试模式下无法实现导入。

    start_app.py(app.py 旁边)

    import sys, os
    sys.path.append(os.path.join(os.path.dirname(__file__)))
    print("sys.path", sys.path) # use this to check if the last entry has the right folder
    from app import start_app
    start_app()
    

    现在,您可以在调试模式下使用python -m start_app.py 运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多