# How to generate byte code file in python

PythonServer Side ProgrammingProgramming

All python program automatically compiles your source code to compile code also called as byte code, before executing it.

Whenever we import a module for the first time or when your source file is a new file or we have an updated file then the recently compiled file, a .pyc file will be created on compiling the file in the same directory as the .py file (from python 3- you might see .pyc file is in a subdirectory called __pycache__ instead in the same directory as your .py file). This is a time-saver mechanism as it prevents python to skip the compilation step when you run your program the next time.

No .pyc file will be created if you’re running a script with an import (another file). For example, if you have a script(file1.py) that imports another file(file2.py).

The easiest way to create a PYC file is to import it. Let’s assume you have a module name MainP.py. just do −

>>> import MainP
>>>

However, if you wish to create a .pyc file for a module that is not imported, we have to set of modules called py_compile and compile all modules to do that task.

The py_compile module can manually compile any module. We can also make the py_compile module interactively by using the py_compile. compile function.

>>> import py_compile
>>> py_compile.compile('test.py')
'__pycache__\\test.cpython-36.pyc'
>>>

Once we run the above statement in the python shell, we can see a .pyc file is created in the __pycache__ folder (python 3) else will be created in the same location as your test.py file.

In case you want to compile multiple files at a time, you can use the py_compile.main() function, like this −

>>> #Compiles several files at a time
>>> py_compile.main(['test1.py', 'test2.py', 'test_sample1.py', 'test_sample2.py'])
0

We can see that the four different compiled files are generated −

However, in case you want to compile all the files inside a folder, you can use the compileall.compile_dir() function.

>>> # Compile all the .py file from a particular folder.
>>> import compileall
>>> compileall.compile_dir('gmplot')
Listing 'gmplot'...
Listing 'gmplot\\.git'...
Listing 'gmplot\\.git\\hooks'...
Listing 'gmplot\\.git\\info'...
Listing 'gmplot\\.git\\logs'...
Listing 'gmplot\\.git\\logs\\refs'...
Listing 'gmplot\\.git\\logs\\refs\\remotes'...
Listing 'gmplot\\.git\\logs\\refs\\remotes\\origin'...
Listing 'gmplot\\.git\\objects'...
Listing 'gmplot\\.git\\objects\\info'...
Listing 'gmplot\\.git\\objects\\pack'...
Listing 'gmplot\\.git\\refs'...
Listing 'gmplot\\.git\\refs\\remotes'...
Listing 'gmplot\\.git\\refs\\remotes\\origin'...
Listing 'gmplot\\.git\\refs\\tags'...
Compiling 'gmplot\\__init__.py'...
Compiling 'gmplot\\color_dicts.py'...
Listing 'gmplot\\gmplot'...
Listing 'gmplot\\gmplot\\markers'...
Compiling 'gmplot\\gmplot.py'...
Compiling 'gmplot\\setup.py'...
Listing 'gmplot\\tests'...
True

Now we see the .pyc file is created inside the ‘folder_name\__pycache__’ location.

In case, you want to compile all files inside a directory or directories, you can do it with compile function.

C:\Users\rajesh>python -m compileall
Skipping current directory
Listing 'C:\\Python\\Python361\\python36.zip'...
Can't list 'C:\\Python\\Python361\\python36.zip'
Listing 'C:\\Python\\Python361\\DLLs'...
Listing 'C:\\Python\\Python361\\lib'...
Listing 'C:\\Python\\Python361'...
Compiling 'C:\\Python\\Python361\\BeautifulSoup_script1.py'...
Compiling 'C:\\Python\\Python361\\EDA_python1.py'...
Compiling 'C:\\Python\\Python361\\MainP.py'...
Compiling 'C:\\Python\\Python361\\NegativeAgeException.py'...
Compiling 'C:\\Python\\Python361\\NegativeNumberException.py'...
Compiling 'C:\\Python\\Python361\\OtherP.py'...
Compiling 'C:\\Python\\Python361\\__init__ Constructor.py'...
Compiling 'C:\\Python\\Python361\\attribute_access.py'...
…..
…
Compiling 'C:\\Python\\Python361\\variable_arguments_list.py'...
Compiling 'C:\\Python\\Python361\\variable_arguments_list1.py'...
Compiling 'C:\\Python\\Python361\\winquality1.py'...

and we can see that .pyc file is created for all the files inside a __pycache__ directory.

Published on 19-Feb-2019 12:33:47