173

In my program I want to import simplejson or json based on whether the OS the user is on is Windows or Linux. I take the OS name as input from the user. Now, is it correct to do the following?

osys = raw_input("Press w for windows,l for linux")
if (osys == "w"):
    import json as simplejson
else:
    import simplejson  
Brad Koch
  • 17,848
  • 18
  • 106
  • 133
Tim
  • 1,763
  • 2
  • 11
  • 3
  • 37
    Why are you taking the os name as input from the user? Look into the platform module. http://docs.python.org/library/platform.html `platform.platform()` or `platform.system()` should do what you need, rather than having a user have to input something every time the code is run. – Joe Kington Aug 16 '10 at 19:32
  • 63
    @S.Lott: seriously? You do know that up/downvotes are meant to reflect the quality of the *question*, right? And not whether you agree with something *else* in the OP's code. – jalf Apr 28 '11 at 09:35
  • 2
    @jalf: It's a bad question in every way except grammar. It should not be found as a top response to any kind of search. – S.Lott Apr 28 '11 at 09:46
  • 15
    @S.Lott there is nothing wrong with wanting to know if conditional imports are pythonic. And obviously others agree since it has +50 up votes despite your efforts. – SARose Sep 30 '16 at 14:35
  • Related principles: https://stackoverflow.com/questions/11360858/what-is-the-eafp-principle-in-python – Brandon Bertelsen May 27 '19 at 12:26

3 Answers3

215

I've seen this idiom used a lot, so you don't even have to do OS sniffing:

try:
    import json
except ImportError:
    import simplejson as json
Matt Williamson
  • 37,633
  • 10
  • 61
  • 71
72

To answer the question in your title but not the particular case you provide, it's perfectly correct, tons of packages do this. It's probably better to figure out the OS yourself instead of relying on the user; here's pySerial doing it as an example.

serial/__init__.py

import sys

if sys.platform == 'cli':
    from serial.serialcli import Serial
else:
    import os
    # chose an implementation, depending on os
    if os.name == 'nt':  # sys.platform == 'win32':
        from serial.serialwin32 import Serial
    elif os.name == 'posix':
        from serial.serialposix import Serial, PosixPollSerial, VTIMESerial  # noqa
    elif os.name == 'java':
        from serial.serialjava import Serial
    else:
        raise ImportError(
            "Sorry: no implementation for your platform ('{}') available".format(
                os.name
            )
        )

This should be only used in cases where you're assuming and need a strong guarantee that certain interfaces/features will be there: e.g. a 'file' called /dev/ttyX. In your case: dealing with JSON, there's nothing that is actually OS-specific and you are only checking if the package exists or not. In that case, just try to import it, and fall-back with an except if it fails:

try:
    import some_specific_json_module as json
except ImportError:
    import json
Nick T
  • 24,120
  • 11
  • 77
  • 117
  • 12
    No, it's very incorrect to hardcode OS names to decide whether `simplejson` or `json` is available. You're quoting code from *inherently OS-specific* imports, which is a very different case. See Matt's answer for the correct approach. – Glenn Maynard Aug 16 '10 at 20:01
  • 19
    @Glenn Maynard: I'd defer to you then; I've never used the `json` package and was trying to answer the more general "can you do conditional imports of modules" question. – Nick T Aug 16 '10 at 20:26
11

It is not advisable to use to bind json or simplejson with OS platform. simplejson is newer and advanced version of json so we should try to import it first.

Based on python version you can try below way to import json or simplejson

import sys
if sys.version_info > (2, 7):
    import simplejson as json
else:
    import json
Aashutosh jha
  • 504
  • 6
  • 8