How to Compile Code to Convert py to pyc in Python?
Do you want to convert py to
pyc in Python?
Let’s get some background understanding…
What is pyc file in Python?
Whenever you run the Python code, there are two major steps involved.
- The code (from .py file) gets compiled into byte code and save it as a .pyc file.
- Byte code from this file gets interpreted.
Python is called as a Byte code interpreted programming language.
This complete process happens in the background so that you don’t see the .pyc file.
This is the usual case when you simply execute any Python program.
py vs pyc file in Python:
If you open any py file, you can see the Python code which is human readable. Whereas, pyc file contains byte code. You can not read it simply.
Now, our interest is to convert the py file into
pyc file. For this, you have to compile Python py file without executing it.
When does .pyc file get created?
We import modules in our program. If you are importing module first time, Python compiles the module code and create
.pyc file. It is nothing but the bytecode file.
In another scenario, if there is any change in code, python compiles the code and save it as bytecode (
.pyc file) before running a python script.
The .pyc file is created in the same folder where the .py file exists.
These are all usual ways, Python compiles and generates the .pyc file.
Why does compiling .py to .pyc not happen?
In certain scenarios, Python does not generate the
This may occur in case you don’t have write permissions for the directory where you are compiling the code. It can also happen when you don’t have enough space to store bytecode (
In this case, if you are running Python code, it may run existing (old)
.pyc bytecode. This is the worst behavior to trace.
I still remember when I faced this issue. I was updating my code in a python script, but when I run the code, I was amazed not to see any changes in output even after doing changes in the Python script.
And here is a way to get rid of it.
Convert py to pyc in Python by Compiling without Executing
So let’s see how to create a .pyc file just with a simple command.
For this, you can use two modules for compiling Python script-
Method 1: using py_compile module
py_compile in your python code. There is a function called
compile(). Pass the Python file name that you want to compile as an argument. It will convert Python script into the bytecode (file with the file extension
import py_compile py_compile.compile('abc.py')
This will compile the
abc.py file and saved compiled bytecode in
abc.pyc file. Python stores bytecode in the same directory where the
abc.py file exists.
Method 2: using compile all module
Sometimes you may need to compile all the python script within a given directory. Then this method is good rather than compiling each file independently.
In that case, run the following command in a given directory.
python -m compileall
It will compile all the files in the current directory.
If you are not providing any of the directories in command, it compiles all the python script found in sys.path.
Note, it will just compile Python
py file without executing.
In this post, I have not covered anything related to compiler and interpreter as it is not the scope of this article. If you are interested, you can read the difference between Compiler and Interpreter.
This is all about how to convert
pyc in Python. If you have any doubt, write in the comment.