Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 want to do is test if my code is running on cPython or Jython in my settiings.py so I can tell it to use the appropriate db driver (postgresql_psycopg2 or doj.backends.zxjdbc.postgresql). Is there a simple way to do this?

like image 666
Vasil Avatar asked Jul 09 '09 12:07

Vasil


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.

Does Python use CPython by default?

Written in C and Python, CPython is the default and most widely used implementation of the Python language. CPython can be defined as both an interpreter and a compiler as it compiles Python code into bytecode before interpreting it.

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.

Is CPython an interpreter or compiler?

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.


2 Answers

In Python 3.3 and beyond you can use sys.implementation and look at the name attribute.

like image 97
ubiquibacon Avatar answered Nov 15 '22 21:11

ubiquibacon


The most clear-cut way is:

import platform

platform.python_implementation()

'CPython'

By default, most of the time the underlying interpreter is CPython only which is also arguably the most efficient one :)

like image 23
VineetChirania Avatar answered Nov 15 '22 21:11

VineetChirania