I am new to the Python programming language. I was wondering if it is possible to compile a program to written in Python.
Is it possible to convert Python scripts to some lower level programming languages which then can be compiled to binary code?
A developer who is considering to code in Python might want to keep the possibility open to be able to go for binary distribution later.
python compile on the fly when you run it.
Run a .py file by(linux): python abc.py
Avoiding redundancy I don't repeat my answer here again.
Please refer to my answer here. (note that answer only covers compiling to python bytecode.)
Python, as a dynamic language, cannot be "compiled" into machine code statically, like C or COBOL can. You'll always need an interpreter to execute the code, which, by definition in the language, is a dynamic operation.
You can "translate" source code in bytecode, which is just an intermediate process that the interpreter does to speed up the load of the code, It converts text files, with comments, blank spaces, words like 'if', 'def', 'in', etc in binary code, but the operations behind are exactly the same, in Python, not in machine code or any other language. This is what it's stored in .pyc files and it's also portable between architectures.
Probably what you need it's not "compile" the code (which it's not possible) but to "embed" an interpreter (in the right architecture) with the code to allow running the code without an external installation of the interpreter. To do that, you can use all those tools like py2exe or cx_Freeze.
Maybe I'm being a little pedantic on this :-P
You dont have to compile it. the first you use it (import) it is compiled by the CPython interpreter. But if you really want to compile there are several options.
To compile to exe
Or 2 compile just a specific *.py file, you can just use
I think Compiling Python Code would be a good place to start:
python is an interpreted language, so you don't need to compile your scripts to make them run. The easiest way to get one running is to navigate to it's folder in a terminal and execute "python somefile.py". This depends on you having python installed from the python site.
You can compile python apps, but that is generally not something a new developer needs to do initially. If that is what you're looking for, take a peek at py2exe. This will take your python script and package it up as an executable file like any program on your windows-based computer. You can also compile individual files using python, as described in the "Compiling Python modules to byte code" section at this site.