2.如何使它工作?删除您在__init__.py和__main__.py中使用的os.chdir和sys.path.append黑客.至少,python unittest不需要它们.使用文档(通过将unittest子类化)中显示的模式创建单元测试.TestCase)运行单元测试的依据python -m unittestI am working on a Jenkins CI/CD pipeline for my Python package. My project file hierarchy is as follows:project/- package_name - file1.py - file2.py - etc...- tests - unit - __main__.py - __init__.py - test1.py - test2.pyAll unit tests (I am using unittest) are run using a single commandpython -m tests.unitvia adding __init__.py of the following content:contentsimport osos.chdir(os.path.dirname(os.path.abspath(__file__)))and __main__.py which looks like thiscontentsimport unittestimport syssys.path.append('../..')loader = unittest.TestLoader()start_dir = '.'suite = loader.discover(start_dir)runner = unittest.TextTestRunner(verbosity=2).run(suite)First, the path is changed to the ./tests/unit. After that, the top directory is added to the import path so that the package can be imported in tests. This works as intended (i.e., all rests are executed by running python -m test.unit at the top of the project directory) on my personal laptop (Python 3.6.4).However, when I use the same trick on a remote Jenkins server (Python 3.6.4 as well), I am getting the following error:no module named test.unit.__main__; 'test.unit' is a package and cannot be directly executedI have researched the issue but none of the proposed solutions seems to be working in my case.How can I modify my code to create a test suite in unittest that will run without any issues locally and remotely?EDITI tried modifying the PYTHONPATH variable, but no success 解决方案 1. Why it does not work?1.1. About python -m and __main__.pyWhen you run python -m tests.unit, what code will be ran by the python interpreter, is, in this ordertests.__init__.pytests.unit.__init__.pytests.unit.__main__.py1.2. Reproducing the errorNow, if you remove the __main__.py, you will get the following error:No module named tests.unit.__main__; 'tests.unit' is a package and cannot be directly executedThis is almost the same message as you did get. If you have in your sys.path a folder that contain folder called test with structure like this (Note: the test-folder is not in prular, and there is no __main__.py!)test├── __init__.py└── unit └── __init__.pyand run commandpython -m test.unitwhat python interpreter tries to run, in this order istest.__init__.pytest.unit.__init__.pytest.unit.__main__.py <-- missing!Since test.unit.__main__.py is missing, you will get error messageNo module named test.unit.__main__; 'test.unit' is a package and cannot be directly executedwhich is the error message you are getting. So, the reason for your error message most probably, that you have somewhere in your sys.path directories a directory called test with the structure shown above, and you are trying to call python -m test.unit instead of python -m tests.unit.2. How to make it work?Remove your os.chdir and sys.path.append hacks that you are using in your __init__.py and __main__.py. At least, they are not needed for python unittest to work.Create unittests using the pattern shown in the documentation (by subclassing unittest.TestCase)Run your unit tests bypython -m unittest 这篇关于unittest,在本地工作,但不在远程服务器上,没有名为x .__ main__的模块; 'x'是一个包,不能直接执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 14:44