问题描述
我有一个模块,它将作为独立应用程序分发。该模块具有以下结构:
$ 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/python3.6/site-packages']
解决方法
首先,我使用错误的命令运行该应用,正确的是:
$ python mymodule
要创建并运行单个文件应用程序,我需要使用:
$ python -m zipapp -p "/usr/bin/evn python" mymodule
$ python mymodule.pyz
要运行单元测试,我需要使用:
$ python -m unittest
但是单元测试由于错误而失败:
======================================================================
ERROR: test_fun (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_fun
Traceback (most recent call last):
File "/Users/igork/.pyenv/versions/3.6.4/lib/python3.6/unittest/loader.py",line 153,in loadTestsFromName
module = __import__(module_name)
File "/Users/igork/developer/github/python/testzipapp/tests/test_fun.py",line 2,in <module>
from mymodule.fun import Fun
File "/Users/igork/developer/github/python/testzipapp/mymodule/fun.py",line 8,in <module>
from bar import Bar
ModuleNotFoundError: No module named 'bar'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
为纠正错误,我在tests/__init__.py
文件中添加了以下几行:
import sys
sys.path.append('mymodule')
总结: 我使用的项目的结构是:
testzipapp $ tree -L 2
.
├── create-pyz.sh
├── mymodule
│ ├── __main__.py
│ ├── bar.py
│ ├── fun.py
├── mymodule.pyz
└── tests
├── __init__.py
├── test_fun.py
文件tests/test_fun.py
是:
import unittest
from mymodule.fun import Fun
class TestFunImports(unittest.TestCase):
def test_fun_run(self):
s = Fun()
s.run()
self.assertEqual(2+2,4)
文件mymodule/__main__.py
是:
#!/usr/bin/env python
import sys
from fun import Fun
def main():
sys.stdout.write('main is running ')
Fun().run()
if __name__ == '__main__':
main()