我正在尝试将我的基本Flask应用与setup.py
正确捆绑在一起,以便在Python 3.6.6上与gunicorn
或uwsgi
一起运行。
mygame/games/board.py
mygame/games/game.py
mygame/games/__init__.py
mygame/__init__.py
setup.py
如果我从__init__.py
目录中运行mygame
,一切正常。
如果我尝试使用python -m mygame
运行,它将失败,并显示以下错误:
Traceback (most recent call last):
File "/usr/lib64/python3.6/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib64/python3.6/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/usr/lib64/python3.6/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "mygame/__init__.py", line 14, in <module>
from games.errors import GameCommandError
ModuleNotFoundError: No module named 'games'
这是怎么回事?
如果我希望python -m mygame
和python __init__.py
都可以使用哪种导入方案?
如果我将__init__.py
中的导入更改为显式相对(.games.foo
),则在运行python -m mygame
时会得到以下提示:
No module named mygame.__main__; 'mygame' is a package and cannot be directly executed
当我运行__init__.py
时,我得到的是相同的东西:
Traceback (most recent call last):
File "__init__.py", line 14, in <module>
from .games.errors import GameCommandError
ModuleNotFoundError: No module named '__main__.games'; '__main__' is not a package