In this article we will see what is a pyc file ,how is it formed,how to convert and compile a pyc file.
When we run a code in python it actually goes through a couple of steps before our program reaches the virtual machine
- It is compiled to bytecode.
- Then it is routed to virtual machine.
When we execute source code, Python compiles it into byte code. The compilation is a translation step, and the byte code is a low-level platform-independent representation of source code. Note that the Python byte code is not binary machine code (e.g., instructions for an Intel chip).
when we save a python script it is saved as .py which are human-readable but .pyc files are byte codes(0’s and 1’s).
Python translates each statement of the source code into byte code instructions by decomposing them into individual steps. The byte code translation is performed to speed execution.
Byte code can be run much more quickly than the source code statements. It has.pyc extension and it will be written if it can write to our machine.
This complete process happens in the background of computer so that you don’t see the .pyc file.
Why do we need .pyc files
Compiling usually means converting to machine code which is what runs the fastest. But interpreters take human readable text and execute it. They may do this with an intermediate stage. In the old CBASIC language it was called p-code, in Python it is called byte code.
They’re pre-compiled versions of your program, kind of like a cache. As long as the program doesn’t change, there’s no reason for python to re-compile your program into bytecode, so it stores the result in the .pyc file.
So some times even when you changed the script in python it does not give you the improvised result this is because of .pyc file. It automatically runs the old pyc code, in this case, it is difficult to trace the error.
In this article i will give three ways to get rid of this error.
Converting .py files to .pyc files
In this article we will see three methods to convert .py files to .pyc files .Which can be use for both python2 and python3.
using Import module
import is a built-in Python module, it can be used directly in any Python version.
Python automatically compiles Python source code when a module is imported, so the easiest way to create a PYC file is to import it.
import yourfilename # Import yourfilename.py file
The disadvantage of this method is it not only imports your file but also executes it.so, sometimes it may not fit your requirements.
using py_compile()
It is one of the most common libraries available in both python 2 and python 3 it can be used directly without installing any third-party libraries.
import py_compile
py_compile.compile('yourfilename.py') # Compile single Python source file,
py_compile.main(['yourfilename1.py','yourfilename2.py']) # or compile multiple Python source files.
It directly complies your file but does not execute it. So it can be used in instances where you want to import the file but not execute it.
using compileall module
This module is mainly for compiling all Python source files in a directory tree recursively. It also can compile the single Python source file.
import compileall
compileall.compile_dir('.') # Recursively compile all Python source files in a directory tree,
compileall.compile_file('yourfilename.py') # or compile single Python source file.