How to generate and view Python Byte Code File .pyc from Source .py File

Generate Python Byte Code and View

You know that you can compile and run a Python program using just a single PYTHON command in the DOS command prompt. Let us know how to generate or convert the Python Byte Code File .pyc from the source file .py and view it on the console using this Last-minute python tutorial.

The words bytecode and byte-code are used interchangeably.

Remember that as a programmer or developer, you should distribute the code to the end-user. The end-user runs the byte-code file shared by you in his System with a compatible Python Runtime Environment.

Note: Sharing a Byte Code file is safer than sharing an executable file. Viruses do not corrupt the byte code files.

1. How to Generate Python Byte Code file .pyc

We use the Python built-in module "py_compile" to create the byte code file.

Follow the below steps to create a Python Byte code file ".pyc". In the end, we try to run the .pyc file using the same PYTHON command.

Step 1: Create a sample Python program with the name "generate_bytecode.py". Type the following code.

print('Generating Byte Code File.. .pyc from .py file')

Step 2: In the DOS command prompt, type the below command to generate the Byte-Code file.

C:\Python> python -m py_compile generate_bytecode.py 

Step 3: Check the current directory for the folder "__pycache__". Python creates a byte-code file with the name PROGNAME.cpython-xx.pyc. Here, xx is the Python version. For the above example, the name of the Byte code file is generate_bytecode.cpython-39.pyc. You can distribute this .pyc file for readymade use.

Step 4: To run the .pyc file, simply use the PYTHON command in the command prompt as shown below.

C:\Python\__pycache__> python generate_bytecode.cpyton-39.pyc 
Generating Byte Code File.. .pyc from .py file

C:\Python\__pycache__>

Note that the end-user may put this .pyc file in any directory and run to get the same output as the developer intended.

2. How to View the Python Byte Code file on the Console

Practically there is no use of viewing a Python Byte Code. It is only for knowledge purpose. You should use the Python built-in module "dis" to accomplish this task. The module DIS represents Disassembler. Let us view the bytecode using the below command.

C:\Python> python -m dis generate_bytecode.py

  1           0 LOAD_NAME                0 (print)
              2 LOAD_CONST               0 ('Generating Byte Code File.. .pyc from .py file')
              4 CALL_FUNCTION            1
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

Observe that you are supplying a .py file to the python command. You can find byte-code instructions like LOAD_NAME, LOAD_CONST, CALL_FUNCTION, POP_TOP, BINARY_ADD, RETURN_VALUE etc inside the code.

This is how you can generate (convert) and view a Python Byte-code file.

Share this Last Minute Python tutorial with your friends and colleagues to encourage authors.