In this article, we will learn about precompiled standard library in python. Simply we are going to learn about .pyc extensions in python. At the end of the article, we will come to know about What is .pyc file? What are the advantages of .pyc files? How to generate .py to .pyc files? Many of us thought .py is an extension to save the files in python. And we don’t have any knowledge about the .pyc extension. After reading this article, we will be clear about it. Let us move to the article.
pyc stands for python compiled files. Python is a combination or package of interpreter and support libraries. Whenever we are installing python, we will get the two things. They are interpreters and support libraries. An interpreter is useful for running a python code. Support libraries contain all those definitions about all built-in functions.
What are pyc files?
pyc files are the byte code representation of the python scripts. These files are automatically generated when we create a python script. This .pyc file contains the “compiled bytecode” of the program. So It converts the source code to byte code only one time. .pyc files speed up the process. pyc files are created only for the files we import.
What are the advantages of .pyc files?
Before interpreting the python code, the scripts are converted to byte codes. Whenever .pyc files are there, they will help to speed up the execution. We don’t need to recompile. The only condition is that if there are .pyc files, there must be corresponding .py files.
Difference between .py and .pyc files
.py contains the source code of the program, and the pyc files contain the byte code of the program. After the compilation of .py files, the .pyc files are generated. The .pyc files are not created for every program that we run. It only generates for the files that we import.
Precompiled Standard Library
Precompiled Standard Library is the .pyc (byte code) files generated for each of the standard libraries in python. It just converts .py code to .pyc code. Simply, it converts source code to byte code. When we import a python library, it doesn’t have corresponding .pyc files. So it will compile “new byte code”. By doing this will speed up the execution of the code. And we don’t need to recompile the byte code.
While installing python, there is an option named precompiled standard library. It is useful to generate .pyc files for standard libraries. In the below image, I highlighted the option.
How to generate .pyc of any other .py files?
- py_compile method
- compile all module
1. Using py_compile method
Let us consider we have a python file named “program.py”. Now we are going to compile this program. We have to give the file name of the python, which we have to compile. After seeing the name of the file, it converts the source code into byte code.
import py_compile py_compile. compile("program.py")
It doesn’t generate any output for this. This code creates a byte code for the file. If we want to see the .pyc files, then we have to follow the steps.
- First, open the IDLE python folder. Right-click on it you will find the option like open file location.
- Just open it you will find the folder named __pycache__. In that, you will find many pyc files.
- Now I wrote a source code for program.py. Let us check the corresponding compiled file.
pyc file folder
Now we can see that a pyc file is there in the corresponding location.
2. Using compile all method
Now let us see how to compile all the files present in a python directory in a single time.
import compileall compileall.compile_dir("C:/Users/AppData/Local/Programs/Python/Python39")
This code will compile all the python scripts in a specified directory. “C:/Users/AppData/Local/Programs/Python/Python39” replace it with your python folder path.
This method is useful to compile multiple files at the same time.
import py_compile py_compile.main(['program.py','program1.py','program2.py'])
It doesn’t generate any output for this. This code creates a byte code for the file. If we want to see the pyc files, just follow the same method we have done for method one.
Generate pyc files in terminals
Whatever we have done using python programs is possible in terminals also. We can generate a pyc file using terminals also.
Using py_compile method
C:\Users\Username> python -m compile program.py program1.py program2.py
Opening a terminal, you will initially get that prompt symbol. Give the command that is in bold letters. The pyc files will be generated.
Compile_all method in a terminal
C:\Users\Username> python -m compileall
This will compile all the files in a python directory.
FAQs Related to Precompile Standard Library in Python
Byte code is a machine-level code that is understandable only by computer. These byte codes are automatically generated with a .pyc extension.
pyc stands for python compiled files.
.pyc files will speed up the execution.
Here we have learned about precompiled standard library in python. Now we are all known about the behind-scenes of python. As a good programmer, it is necessary to know What is happening behind the programming language. So we think this article will be useful for all Python programmers.
We hope this is easy to understand. Learn with us:) In case of any queries, ask it in the comments. We will clear you as soon as possible.