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')
.)