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
What are the other ways of compiling python?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
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:
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:
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.
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.
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
Goto http://nuitka.net/doc/user-manual.html for more information.