0

I have a python script that is running on OSX; I use PyObjC library to perform some operations. Now, I would like to run the same script on windows; although there is no PyObjC on Windows, so when I call the module; I get a failure as expected.

Is there a way in Python to tell the interpreter that if I am running on OSX, it should load that module, otherwise ignore it? Kinda like the IF statements in a C++ include section; which allow you to fall back to a different library, based on the hardware or software on which that program is running on?

EDIT: I know how to find my OS; I did not ask how to find the Os on which my script run upon. The question was about how to import modules selectively, so I won't get a failure when running on a OS that does not require that module.

  • Possible duplicate of [Python: What OS am I running on?](https://stackoverflow.com/questions/1854/python-what-os-am-i-running-on) – tgikal Sep 13 '18 at 16:21
  • Possible duplicate of [Python: What OS am I running on?](https://stackoverflow.com/questions/1854/python-what-os-am-i-running-on) – Marcin Sep 13 '18 at 16:22
  • No, I am not asking how to find the OS on which the script is running on. The question is how to selectively import modules, so that I can use certain modules when running on Windows or on OSX or on any other OS. –  Sep 13 '18 at 16:40

1 Answers1

0

import statements are just normal Python statements, perfectly capable of being placed inside if blocks:

import platform

if platform.system() == 'Darwin':
    # Running on Mac OS X
    import macosx_specific_module
jwodder
  • 54,758
  • 12
  • 108
  • 124