Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know that the interpreter is Jython or CPython in the code? [duplicate]

Tags:

python

jython

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 *
like image 264
prosseek Avatar asked Mar 10 '14 16:03

prosseek


People also ask

How is CPython different from Jython?

Reference implementation of Python, called CPython, is written in C language. Jython on the other hand is completely written in Java and is a JVM implementation. Standard Python is available on multiple platforms. Jython is available for any platform with a JVM installed on it.

Is CPython interpreted or compiled?

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from major compiled languages, such as C and C + +, as Python code is not required to be built and linked like code for these languages.

Is CPython the default interpreter for Python?

CPython is the default byte-code interpreter of Python, which is written in C.

How do I know what version of CPython I have?

Check Python Version: Script To check which Python version is running, you can use either the sys or the platform module. The script will be the same for Windows, macOS, and Linux. Of course, you can easily obtain the individual components of this tuple using an index (e.g. sys. version_info[0] ) or a name (e.g. sys.


1 Answers

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.

like image 107
Dr. Jan-Philip Gehrcke Avatar answered Sep 26 '22 00:09

Dr. Jan-Philip Gehrcke