Skip to main content

Pyjion

A JIT extension for CPython that compiles your Python code into native CIL and executes it using the .NET 5 CLR.

Compile and test C++ package Documentation Status PyPI

Installing

$ pip install pyjion

Compiling from source

Prerequisites:

  • CPython 3.9.0
  • CMake 3.2 +
  • .NET 5
 $ git clone git@github.com:tonybaloney/pyjion --recurse-submodules
 $ cd pyjion
 $ python -m pip install .

Using Pyjion

To get started, you need to have .NET 5 installed, with Python 3.9 and the Pyjion package (I also recommend using a virtual environment).

After importing pyjion, enable it by calling pyjion.enable() which sets a compilation threshold to 0 (the code only needs to be run once to be compiled by the JIT):

>>> import pyjion
>>> pyjion.enable()

Any Python code you define or import after enabling pyjion will be JIT compiled. You don't need to execute functions in any special API, its completely transparent:

>>> def half(x):
...    return x/2
>>> half(2)
1.0

Pyjion will have compiled the half function into machine code on-the-fly and stored a cached version of that compiled function inside the function object. You can see some basic stats by running pyjion.info(f), where f is the function object:

>>> pyjion.info(half)
{'failed': False, 'compiled': True, 'run_count': 1}

You can see the machine code for the compiled function by disassembling it in the Python REPL. Pyjion has essentially compiled your small Python function into a small, standalone application. Install distorm3 first to disassemble x86-64 assembly and run pyjion.dis.dis_native(f):

>>> import pyjion.dis
>>> pyjion.dis.dis_native(half)
00000000: PUSH RBP
00000001: MOV RBP, RSP
00000004: PUSH R14
00000006: PUSH RBX
00000007: MOV RBX, RSI
0000000a: MOV R14, [RDI+0x40]
0000000e: CALL 0x1b34
00000013: CMP DWORD [RAX+0x30], 0x0
00000017: JZ 0x31
00000019: CMP QWORD [RAX+0x40], 0x0
0000001e: JZ 0x31
00000020: MOV RDI, RAX
00000023: MOV RSI, RBX
00000026: XOR EDX, EDX
00000028: POP RBX
00000029: POP R14
...

The complex logic of converting a portable instruction set into low-level machine instructions is done by .NET's CLR JIT compiler.

All Python code executed after the JIT is enabled will be compiled into native machine code at runtime and cached on disk. For example, to enable the JIT on a simple app.py for a Flask web app:

import pyjion
pyjion.enable()

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

app.run()

FAQ

How do you pronounce "Pyjion"?

Like the word "pigeon". @DinoV wanted a name that had something with "Python" -- the "Py" part -- and something with "JIT" -- the "JI" part -- and have it be pronounceable.

How do this compare to ...

PyPy?

PyPy is an implementation of Python with its own JIT. The biggest difference compared to Pyjion is that PyPy doesn't support all C extension modules without modification unless they use CFFI or work with the select subset of CPython's C API that PyPy does support. Pyjion also aims to support many JIT compilers while PyPy only supports their custom JIT compiler.

Pyston?

Pyston is an implementation of Python using LLVM as a JIT compiler. Compared to Pyjion, Pyston has partial CPython C API support but not complete support. Pyston also only supports LLVM as a JIT compiler.

Numba?

Numba is a JIT compiler for "array-oriented and math-heavy Python code". This means that Numba is focused on scientific computing while Pyjion tries to optimize all Python code. Numba also only supports LLVM.

IronPython?

IronPython is an implementation of Python that is implemented using .NET. While IronPython tries to be usable from within .NET, Pyjion does not have a compatibility story with .NET. This also means IronPython cannot use C extension modules while Pyjion can.

Psyco?

Psyco was a module that monkeypatched CPython to add a custom JIT compiler. Pyjion wants to introduce a proper C API for adding a JIT compiler to CPython instead of monkeypatching it. It should be noted the creator of Psyco went on to be one of the co-founders of PyPy.

Unladen Swallow?

Unladen Swallow was an attempt to make LLVM be a JIT compiler for CPython. Unfortunately the project lost funding before finishing their work after having to spend a large amount of time fixing issues in LLVM's JIT compiler (which has greatly improved over the subsequent years).

Nuitka and Shedskin?

Both Nuitka and Shedskin are Python-to-C++ transpilers, which means they translate Python code into equivalent C++ code. Being a JIT, Pyjion is not a transpiler.

Will this ever ship with CPython?

Goal #1 is explicitly to add a C API to CPython to support JIT compilers. There is no expectation, though, to ship a JIT compiler with CPython. This is because CPython compiles with nothing more than a C89 compiler, which allows it to run on many platforms. But adding a JIT compiler to CPython would immediately limit it to only the platforms that the JIT supports.

Does this help with using CPython w/ .NET or UWP?

No.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Release notes

0.0.3

  • Installable bdist_wheel for Ubuntu, Debian, macOS 10.15, 11 (10.16) and Windows x64
  • Installable manylinux2014 wheel with clrjit.so bundled in
  • Added multithreading/multiprocessing support
  • Fixed a bug where the wheel would be broken if there are two distributions of Python 3.9 on the system
  • Has around 30% chance of working and not causing your computer to explode, or worse, segmentation fault.

0.0.2

  • Installable source distribution support for macOS, Windows and (barely) Linux.

0.0.1

  • It compiles on my machine

Metadata

Release files for pyjion 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyjion 0.0.3
File Size Uploaded
pyjion-0.0.3.tar.gz 101.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pyjion 0.0.3
File
pyjion-0.0.3-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
pyjion-0.0.3-cp39-cp39-manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
pyjion-0.0.3-cp39-cp39-macosx_10_16_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.16+ x86-64 Details
pyjion-0.0.3-cp39-cp39-macosx_10_15_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.15+ x86-64 Details

Total release size: 2.4 MB

Release files / pyjion-0.0.3.tar.gz

Download URL pyjion-0.0.3.tar.gz
Size 101.9 kB
Tags Source
SHA-256 checksum
How to use checksums
660a1ddc9ca877135c2b4e4a985a38ffc6c15e95eff681d895d513d4a7d9b7fc
BLAKE2b-256 checksum
How to use checksums
6a54a4e69df47a68735de26a70ffa709cef54648263db9c866559958c39601a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0rc2

Release files / pyjion-0.0.3-cp39-cp39-win_amd64.whl

Download URL pyjion-0.0.3-cp39-cp39-win_amd64.whl
Size 232.0 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
a2e38e23d03383badc9c3c9bf55b82d2552b2036f6589d78255b84594a4ddba5
BLAKE2b-256 checksum
How to use checksums
fc14a00b8eef4620657f54a7cd29fc9567f5e1f89d891fa5075c69fb99575b2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0rc2

Release files / pyjion-0.0.3-cp39-cp39-manylinux2014_x86_64.whl

Download URL pyjion-0.0.3-cp39-cp39-manylinux2014_x86_64.whl
Size 1.6 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e6b188f90c9b79970b96f6a56a8d580ad62634f7352b661358c5263974d9f658
BLAKE2b-256 checksum
How to use checksums
dfbab9297f931f6111121e50e18c094bc35e6128923a7e9099ca0f8afd64d321
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0rc2

Release files / pyjion-0.0.3-cp39-cp39-macosx_10_16_x86_64.whl

Download URL pyjion-0.0.3-cp39-cp39-macosx_10_16_x86_64.whl
Size 237.8 kB
Tags CPython 3.9 macOS 10.16+ x86-64
SHA-256 checksum
How to use checksums
40a4cdf716459d7c45008cda7cedc6c2c3fef08f0d7cc392e964d7bd7441d937
BLAKE2b-256 checksum
How to use checksums
d1d5a2a0a4bd611e579b27d50ef5651d1a4396dd6d917151385e59e6dba146d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0rc2

Release files / pyjion-0.0.3-cp39-cp39-macosx_10_15_x86_64.whl

Download URL pyjion-0.0.3-cp39-cp39-macosx_10_15_x86_64.whl
Size 234.2 kB
Tags CPython 3.9 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
2c29ca0f1bb7878ca83d8405df0603d97a5571bbd644884bd9baa14adafd6298
BLAKE2b-256 checksum
How to use checksums
e938d561349f84f2d954980fbdf6941e611a287b2cf4495af951f1c8f02ff71b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0rc2

Release history Release notifications | RSS feed

2.0.0

6 release files

1.2.6

5 release files

1.2.5

5 release files

1.2.4

5 release files

1.2.3

5 release files

1.2.2

5 release files

1.2.1

5 release files

1.2.0

5 release files

1.1.1

5 release files

1.1.0

6 release files

1.0.0

5 release files

0.15.0

4 release files

0.14.1

1 release file

0.14.0

4 release files

0.13.0

5 release files

0.12.0

5 release files

0.11.0

6 release files

0.9.0

6 release files

0.8.0

6 release files

0.7.0

6 release files

0.6.0

5 release files

0.5.0

5 release files

0.4.0

5 release files

0.3.0

5 release files

0.2.1

5 release files

0.2.0

5 release files

0.1.0

5 release files

0.0.7

5 release files

0.0.6

5 release files

0.0.5

5 release files

0.0.4

5 release files

This release

0.0.3 This release

5 release files

0.0.2

4 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page