Difference Between .py and .pyc Files: A Python Beginners Guide

Claudio Sabato
9 min readMar 20, 2022

Are you wondering what the difference is between Python .py files and .pyc files? You are in the right place.

Files with extension .py contain Python code that is human readable. On the other side .pyc files contain bytecode that is not human readable. Files with .py extension are compiled into .pyc files that are then processed by the Python interpreter.

Don’t worry if this doesn’t fully make sense, we will go through a few examples that will make things clear.

And I will also show you when the compilation of .py files into .pyc files happens.

Let’s get into it!

What Are .py and .pyc Files in Python?

Files with .py extension are Python source files, the files in which you write your Python code.

The Python code you write in .py files is not executed in the same format by the machine on which you run your code.

Before being executed, the code in the .py files is compiled into .pyc files.

Imagine the compilation process as a translation from one language to another language.

Files with .pyc extension are the result of compiling files with .py extension. A .pyc file for a given Python module gets automatically created when that module is imported.

Note: as a Python developer you will only make code changes to .py files.

To see the difference between the two types of files let’s create first a Python module in a file called app.py.

For all the examples in this tutorial I’m creating app.py inside the /var/tmp/ directory.

The app.py file contains code for the app module and in this example it contains a single function:

def get_full_name(first_name, last_name):
return "{} {}".format(first_name, last_name)

To show you the format of a .pyc file we will first use Python 2.

You will understand why in the next section…

Open the Python shell and import the app module:

$ python2Python 2.7.16 (default, Dec 21 2020
Claudio Sabato

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!