首页 > 解决方案 > 使用 -m 选项或复制到 Windows 中的不同路径位置时,无法使用嵌入式 python 运行覆盖工具

问题描述

我正在尝试使用从https://www.python.org/ftp/python/3.6.1/python-3.6.1-embed-amd64.zip下载的嵌入式 python 运行 python 覆盖工具 以下是我遵循的步骤使用覆盖模块运行测试代码。

1)下载python-3.6.1-embed-amd64.zip后解压到D:\some_directory\python-3.6.1-embed-amd64

2) cd python-3.6.1-embed-amd64

3) python get-pip.py

4) python -mpip 安装覆盖

5) python-3.6.1-embed-amd64\python36._pth 改为

python36.zip
Lib\site-packages
.

# Uncomment to run site.main() automatically
#import site

6) 如果要使用 Internet 代理,请使用以下命令

SET HTTP_PROXY=http://username:password@proxy_ip:proxy_port

7) python -mpip 安装覆盖

创建测试文件如下

文件module_prog.py如下

# -*- coding: utf-8 -*-
"""
    Dummy method for testing
"""


def method_to_tested() -> None:
    """
    Method To be tested
    """
    print("Called method to be tested")

文件 test_module.py 如下

""" Unit test script for testing File : test_module.py
"""

import unittest
from module_prog import method_to_tested


class TestMethods(unittest.TestCase):
    """ Testing the methods """

    def test_method_to_tested(self):
        method_to_tested()

if __name__ == "__main__":
    unittest.main(warnings='ignore')

8)我运行以下

D:\some_directory\python-3.6.1-embed-amd64>python test_module.py

Called method to be tested
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

9) 但是,如果我使用 converage 运行下面提到的指令,我会收到以下错误。

D:\some_directory\python-3.6.1-embed-amd64>python -mcoverage run test_module.py

Traceback (most recent call last):
  File "test_module.py", line 4, in <module>
    import unittest
ModuleNotFoundError: No module named 'unittest'

如果我运行命令

  Scripts\converage.exe run test_module.py

然后覆盖成功运行,但是如果我将嵌入式 python 移动到另一个目录并尝试运行与上面相同的命令,则会出现以下错误

D:\some_other_directory\python-3.6.1-embed-amd64>Scripts\coverage.exe run test_module.py

Fatal error in launcher: Unable to create process using 
'"d:\some_directory\python-3.6.1-embed-amd64\python.exe" "D:\some_other_directory\python-3.6.1-embed-amd64\Scripts\coverage.exe" run test_module.py'

如何使覆盖作为 python 模块运行

标签: pythonpython-3.xmoduleembeddedcode-coverage

解决方案


推荐阅读