7

Is there a way to detect that the interpreter that executes the code is Jython or CPython?

I have another post: Jython does not catch Exceptions. For this case, if I know the interpreter is Jython, I can have different code that should work.

if JYTHON: sys.path.insert(0, os.path.dirname(__file__)) from utils import * else: from .utils import * 
5
  • Python is the language specification. The most well known implementations are Jython and CPython. Commented Mar 10, 2014 at 16:14
  • @JayanthKoushik: So, is your point that there is no (easy or programatic) way? Commented Mar 10, 2014 at 16:15
  • No no; I'm was pointing out that the interpreter is Jython or CPython, not Jython or Python. I think the edit makes it clear. Commented Mar 10, 2014 at 16:19
  • Is the Jython executable named jython.exe or is it just python.exe Commented Mar 10, 2014 at 16:27
  • Also see stackoverflow.com/questions/1854/python-what-os-am-i-running-on Commented Mar 10, 2014 at 16:36

2 Answers 2

8

There is an official way to do it! :-) Please have a look at

http://docs.python.org/2/library/platform.html#platform.python_implementation

Returns a string identifying the Python implementation. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.

New in version 2.6.

I did not know that before.

Old answer:

There probably is no standardized interface, but you can use some educated guessing, based on e.g. sys.executable (http://docs.python.org/2/library/sys.html#sys.executable), and sys.version. Furthermore, some interpreters for sure provide features that are specific to them, which you can make use of.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: starting from python3.3 there's also sys.implementation which contains the name and version of the implementation and might contain other relevant information.
1

I'm not sure this is safe way, but I got a hint from Find full path of the Python interpreter?.

With print sys.executable, I have different results.

context> jython context/context.py None context> python context/context.py /usr/bin/python 

So, checking sys.executable might be one way for the checking.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.