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?
Python is a scripting language. Whenever you run the code, first of all, the code is getting converted into byte code and save it as a .pyc file. Byte code from this file gets interpreted and executed line-by-line.
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 ode 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 files 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 or mishap, 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 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 compileall 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.
If you are Python developer, you can give a look for all python codes we have shared on this blog.
In this post, I have not covered anything related to compiler and interpreter as it is not part of 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.