Is it possible to compile Python natively (beyond pyc byte code)?

I wonder if it is possible to create an executable module from a Python script. I need to have the most performance and the flexibility of Python script, without needing to run in the Python environment. I would use this code to load on demand user modules to customize my application.


Asked by: Kelsey547 | Posted: 28-01-2022






Answer 1

  • There's pyrex that compiles python like source to python extension modules
  • rpython which allows you to compile python with some restrictions to various backends like C, LLVM, .Net etc.
  • There's also shed-skin which translates python to C++, but I can't say if it's any good.
  • PyPy implements a JIT compiler which attempts to optimize runtime by translating pieces of what's running at runtime to machine code, if you write for the PyPy interpreter that might be a feasible path.
  • The same author that is working on JIT in PyPy wrote psyco previously which optimizes python in the CPython interpreter.

Answered by: Ada369 | Posted: 01-03-2022



Answer 2

You can use something like py2exe to compile your python script into an exe, or Freeze for a linux binary.

see: How can I create a directly-executable cross-platform GUI app using Python?

Answered by: Wilson858 | Posted: 01-03-2022



Answer 3

I've had a lot of success using Cython, which is based on and extends pyrex:

Cython is a language that makes writing C extensions for the Python language as easy as Python itself. Cython is based on the well-known Pyrex, but supports more cutting edge functionality and optimizations.

The Cython language is very close to the Python language, but Cython additionally supports calling C functions and declaring C types on variables and class attributes. This allows the compiler to generate very efficient C code from Cython code.

This makes Cython the ideal language for wrapping for external C libraries, and for fast C modules that speed up the execution of Python code.

Answered by: Emma696 | Posted: 01-03-2022



Answer 4

I think you can use jython to compile python to Java bytecode, and then compile that with GCJ.

Answered by: Rafael516 | Posted: 01-03-2022



Similar questions

python - SDL: What does "working natively in C++" mean?

I am trying to get a general sense of the phrase above. I was learning PyGame and ran into SDL (Simple DirectMedia Layer) reference. Reference to SDL from PyGame: "Pygame is a python wrapper for SDL, written by Pete Shinners." http://www.pygame.org/docs/tut/newbieguide.html I looked up SDL (SDL link in the URL above[just...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top