我有一个模块,我将作为独立应用程序分发。该模块具有以下结构:
$ tree -L 2 ./
./
├── mymodule
│ ├── __main__.py
│ ├── fun.py
└── mymodule.pyz
mymodule/__main__.py
包含下一行:
#!/usr/bin/env python
import argparse
import sys
import os.path
from mymodule.fun import Fun
def main():
sys.stdout.write('main is running')
Fun().run()
if __name__ == '__main__':
main()
mymodule/fun.py
包含下一行:
#!/usr/bin/env python
import sys
class Fun:
"""FUN"""
def __init__(self):
pass
def run(self):
sys.stdout.write("fun")
如果我使用$ python -m mymodule
输出运行模块是
main is runningfun
$ python -m zipapp -p "/usr/bin/evn python" mymodule
但是,如果我使用并运行它创建一个独立的应用程序,$ python mymodule.pyz
我会收到错误
Traceback (most recent call last):
File "/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "mymodule.pyz/__main__.py", line 6, in <module>
ModuleNotFoundError: No module named 'mymodule'
有什么问题import
?
UPD:sys.path 输出
$ python -m mymodule
['', '/Users/igork/.pyenv/versions/3.6.4/lib/python36.zip', '/Users/igork/.pyenv/versions/3.6.4/lib/python3.6', '/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/lib-dynload', '/Users/igork/.local/lib/python3.6/site-packages', '/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/site-packages']
$ python mymodule.pyz
['mymodule.pyz', '/Users/igork/.pyenv/versions/3.6.4/lib/python36.zip', '/Users/igork/.pyenv/versions/3.6.4/lib/python3.6', '/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/lib-dynload', '/Users/igork/.local/lib/python3.6/site-packages', '/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/site-packages']