Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个用 C++ 编写的 Python pyd 模块。我还有一个 Python 版本的模块(速度要慢得多)。我的问题是,由于我希望程序在多个操作系统上运行,我是否可以尝试在程序中导入 C++ 版本并在该版本失败时导入较慢的 Python 版本(其他操作系统、架构)?
是的,您可以导入如下内容:
try: import CppModule as Module except ImportError: import PurePythonModule as Module
是的你可以:
try: import CppModule except ImportError: import PythonModule
编辑:这个答案虽然不正确,但并不是很有用。正如@Best Games 的回答所示,这仅在您使用通用名称导入模块时才真正有用。