简体   繁体   English

Flask不适用于调试模式

[英]Flask don't work with debug mode

I working on my little package, and I came across with the weird werkzeug problem. 我在werkzeug我的小包装时遇到了奇怪的werkzeug问题。

Problem : When I tried to run my application in debug mode, and in order to run my application I use the command python3 -m my_app --start 问题 :当我尝试以调试模式运行应用程序时,为了运行我的应用程序,我使用命令python3 -m my_app --start

I got the error ModuleNotFoundError: No module named '__main__.helpers'; '__main__' is not a package 我收到错误 ModuleNotFoundError: No module named '__main__.helpers'; '__main__' is not a package ModuleNotFoundError: No module named '__main__.helpers'; '__main__' is not a package

the files are very simple 文件很简单

app.py 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 __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()

And seems like the bug can't be fixed, GitHub issue 似乎无法修复该错误, GitHub问题

So, maybe someone have a solution for this case? 那么,也许有人对此案有解决方案? Thanks. 谢谢。

I created a different file for the start_app function because I found that in debug mode, the import could not be realized. 我为start_app函数创建了另一个文件,因为我发现在调试模式下无法实现导入。

start_app.py (next to app.py) start_app.pyapp.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()

Now, you can run it with python -m start_app.py in debug mode. 现在,您可以在调试模式下使用python -m start_app.py运行它。

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

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