Python module win32com on Linux

匆匆过客 提交于 2019-12-03 17:19:43

Use a try..except ImportError:

try:
    from pythoncom import PumpWaitingMessages
    from pythoncom import Empty
    from pythoncom import Missing
    from pythoncom import com_error
    import win32api
except ImportError:
    # handle exception

What to do when there is an exception is up to you. You could add Linux-specific code that provides an analogous interface, or you could use the warnings module to tell the user that certain functions/features are unavailable.


Alternatively, you could use an if-statement based on the value of sys.platform:

import sys
if sys.platform == "win32":
    ...
elif sys.platform == 'cygwin':
    ...
elif sys.platform[:5] == 'linux':
    ...
elif sys.platform == 'darwin':
    ...
else:
    ...

Other values which may be important for cross-platform code may be os.name (which could equal 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'), or platform.architecture (which could equal things like ('32bit', 'ELF').)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!