Can I detect if my code is running on cPython or Jython?

后端 未结 5 996
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 00:49

I\'m working on a small django project that will be deployed in a servlet container later. But development is much faster if I work with cPython instead of Jython. So what I

5条回答
  •  执笔经年
    2020-12-07 01:22

    As sunqiang pointed out

    import platform
    platform.system()
    

    works for Jython 2.5, but this doesn't work on Jython 2.2 (the previous Jython release). Also, there has been some discussion about returning more operating system specific details for calls like these in Jython 3.x. Nothing has been decided there, but to be safely backwards and forwards compatible, I would suggest using:

    import sys
    sys.platform.startswith('java')
    

    Which will return True for Jython and False everywhere else (actually in Jython 2.2 or older it returns 1 for Jython and 0 everywhere else, but this will still work fine in if statements and other checks). This call works in Jython at least as far back as 2.1, and will work for the foreseeable future.

    In Python versions 2.6 or above (note Jython 2.6 has not yet been released) another option is:

    import platform
    platform.python_implementation
    

    Which returns 'CPython' for the C implementation of Python, 'IronPython' for IronPython and will return 'Jython' for Jython. Obviously this one isn't backwards compatible below 2.6, but will be forwards compatible.

提交回复
热议问题