简体   繁体   English

导入模块时如何避免导入模块依赖性?

[英]How to avoid importing module dependencies when importing module?

I am trying to write tests for a script that requires the win32 api, while developing on a mac. 我正在尝试在Mac上开发时针对需要Win32 API的脚本编写测试。 I would like to write tests for the portions that don't require win32, and run them on a mac, but I can't because the script, obviously, automatically imports all dependencies, including win32, which I don't have on my mac machine. 我想为不需要win32的部分编写测试,并在Mac上运行它们,但是我不能这样做,因为显然,脚本会自动导入所有依赖项,包括我没有的win32。 Mac机器。

I have thought about faking an import in the test file. 我曾考虑过伪造测试文件中的导入。 Since python doesn't import the same module twice and it only checks the name of the dependencies and not the contents. 由于python不会两次导入同一模块,因此它仅检查依赖项的名称,而不检查其内容。 Is there anything less hacky that I can use to solve this problem? 有什么可以用来解决此问题的工具吗?

Anything stopping you from using this? 有什么阻止您使用此功能的吗?

try:
    import win32api
except ImportError:
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM