75

I have started learning python and I'm also a new user to Ubuntu. I need to know the ways of compiling the .py files. I have tried with the command

python "hello.py"

What are the other ways of compiling python?

hagello
  • 103
Bharath Lakshman
  • 861
  • 1
  • 7
  • 3

5 Answers5

73

Adding to Bryan's answer, if you simply want to compile a file or a bunch of files from a terminal, the py_compile module can be executed as a script in the following manner:

python -m py_compile fileA.py fileB.py fileC.py ...

Yamaho
  • 4,413
  • 11
    If you are compiling because you want speed, you can also add the -O flag, like python -O -m py_compile …, which will “turn on basic optimizations”. It mainly strips out assert statements and if __debug__ code, so for most code it has no effect. See man python and “What does Python optimization … do?” for details. – Rory O'Kane Oct 03 '14 at 05:09
  • That's not compilation, that's transliteration to some other representation of the python language. You still need the python interpreter to run this. In case anyone else thinks "oh that was a surprisingly easy answer!" and then discovers it's not actually producing a compiled binary at all. – Luc Dec 08 '21 at 12:34
30

Also be aware that you don't need to compile a .py file to run it. Python is an interpreted language, and you can run the scripts directly, either using:

python hello.py

Or make your script executable by adding #!/usr/bin/env python to the top of the script, making the file executable with chmod +x hello.py and then running:

./hello.py

The fact that Python internally compiles your .py script to bytecode .pyc files for performance reasons is an implementation detail, and unless you have a strong reason to do so, let python itself decide when and if to compile.

MestreLion
  • 20,086
  • I'm confused as to why this works. Doesn't the Python interpreter needs to be able to read the script in order to run it? If you give only execute permission (chmod +x), how is the interpreter reading the script? – fabda01 Aug 10 '18 at 17:27
  • 1
    @yellow01: when you execute a script that contains a "shebang", ie, first line starts with #!/path/to/interpreter, the kernel actually executes whatever is declared there, passing the script path as an argument. In my example, when you run ./hello.py what is actually executed is /usr/bin/env python ./hello.py. See https://en.wikipedia.org/wiki/Shebang_(Unix) – MestreLion Aug 11 '18 at 13:49
  • 3
    @yellow01 you are right that the interpreter will need reading permissions once started through the process MestreLion describes. However, chmod +x grants execute, and does not touch existing permissions so presumably it's readable in addition to now being executable. – spectras Jun 11 '19 at 14:08
17

You may also try compileall:

python -m compileall ./
Jens Erat
  • 5,051
  • 7
  • 31
  • 37
MediaVince
  • 271
  • 2
  • 4
15

Check out this link Compile in Python

In the middle of the page, it talks about the py_compile module that can be imported. The syntax is as follows:

import py_compile

py_compile.compile("file.py")

This method of compiling will not execute the module either like running python file.py.

There is also a method that compiles an entire directory tree but I'll let you check out the link to see how that is executed.

Hope this helps.

Bryan
  • 3,532
  • 1
  • 12
  • 12
8

You can compile Python scripts to a binary code using various methods, but I have found out that using Nuitka is more efficient.

Nuitka is a Python-to-C++ compiler that supports almost all versions of python.

The command syntax is as easy as

nuitka hello.py

Goto http://nuitka.net/doc/user-manual.html for more information.

user258532
  • 1,258
  • 2
  • 16
  • 25
  • 7
    More efficient how? Why would anyone want to use it? – muru Jan 31 '17 at 00:02
  • nuitka is indeed very useful - for example, it allows you to ship Python programs to computers where Python is not installed. But as nuitka compiles Python programs to C++ machine code, you can circumvent that. – user258532 Aug 24 '18 at 13:44