Python 调试==True时烧瓶中的相对导入问题
我有一个flask应用程序,它作为一个模块运行。结构看起来像这样Python 调试==True时烧瓶中的相对导入问题,python,flask,Python,Flask,我有一个flask应用程序,它作为一个模块运行。结构看起来像这样 app/ __init.py __main__.py app.py cli.py \uuuu main\uuuuu.py如下所示: from . import cli cli.main() import argparse import ConfigParser as configparser import sys from . import app def command_line_arguments():
app/
__init.py
__main__.py
app.py
cli.py
\uuuu main\uuuuu.py
如下所示:
from . import cli
cli.main()
import argparse
import ConfigParser as configparser
import sys
from . import app
def command_line_arguments():
parser = argparse.ArgumentParser(description='application')
parser.add_argument(
'-c', '--config-file', type=str,
help='Load settings from the chosen configuration file')
return parser.parse_args()
def main():
"Run the app with the configuration chosen in command_line_arguments()"
options = command_line_arguments()
app = app.App(options.config_file)
try:
app.run(
host=app.host,
port=8000)
except KeyboardInterrupt:
pass
cli.py如下所示:
from . import cli
cli.main()
import argparse
import ConfigParser as configparser
import sys
from . import app
def command_line_arguments():
parser = argparse.ArgumentParser(description='application')
parser.add_argument(
'-c', '--config-file', type=str,
help='Load settings from the chosen configuration file')
return parser.parse_args()
def main():
"Run the app with the configuration chosen in command_line_arguments()"
options = command_line_arguments()
app = app.App(options.config_file)
try:
app.run(
host=app.host,
port=8000)
except KeyboardInterrupt:
pass
应用程序运行正常,但如果我尝试在运行参数中设置debug=true(或在初始化应用程序时,我尝试运行应用程序时会出现以下错误):
Traceback (most recent call last):
File "" line 1, in <module>
from . import cli
ValueError: Attempted relative import in non-package
回溯(最近一次呼叫最后一次):
文件“”中的第1行
从.import cli导入
ValueError:尝试在非包中进行相对导入
关于如何让debug使用这种设置有什么想法吗?应该是
\uuu init\uuuuuuuuuupy
而不是\uuuuu init.py
您直接以脚本的形式运行文件(python-app/\uuuuuuuuuuuu main\uuuuuuu
)。不要这样做,而是运行包(python-m-app
)。只有先将\uuuu init.py
重命名为\uuuu init\uuuu.py
,这才有效。