What are .pyc files in Python?

We usually write programs in Python and save the file with .py extension. However, there is another file type called .pyc, which is automatically generated by the Python interpreter while executing the source code.

What is a .pyc File?

When you execute a Python program, the Python interpreter doesn't directly execute the .py file; instead, it parses the source code, compiles it into bytecode (a low-level representation of the Python source code), and stores it as the .pyc file. Further, this bytecode is executed with the Python Virtual Machine (PVM).

A .pyc file is usually created when a Python program is executed for the first time or when a module is imported. Some of the benefits of storing bytecodes in .pyc files are −

  • By storing bytecode in a .pyc file, Python programs can run faster as the interpreter doesn't have to repeatedly recompile the source code.
  • Once a .pyc file is generated, Python can load and execute this bytecode multiple times until the source code remains the same.

The name of the .pyc files includes the version of Python that generated them. For example, module.cpython-39.pyc for Python 3.9.

How do .pyc Files Work?

Python stores the .pyc files in a special directory called __pycache__, which is located in the same directory as the source code. When you import a module in Python, the interpreter checks if a .pyc file exists in the __pycache__ directory. If it exists, Python loads the bytecode from the .pyc file directly by skipping the compilation step. If it doesn't exist, the interpreter recompiles the .py file.

Example: Observing .pyc File Creation

Let's create a simple Python module and observe how .pyc files are generated ?

# Create a file called mymodule.py
def greet(name):
    return f"Hello, {name}!"

def calculate_sum(a, b):
    return a + b

Now, let's import this module from another Python file ?

import mymodule

result = mymodule.greet("World")
print(result)

sum_result = mymodule.calculate_sum(10, 20)
print(f"Sum: {sum_result}")
Hello, World!
Sum: 30

After running this code, Python automatically creates a __pycache__ directory containing mymodule.cpython-311.pyc (or similar, depending on your Python version).

Managing and Deleting .pyc Files

We cannot run .pyc files directly from the command line like we execute the .py files. If the corresponding source code exists, the .pyc file will automatically be executed when the Python interpreter runs or when a module is imported.

You will not need to manually manage or delete the .pyc files because Python will regenerate them when needed. However, if you are troubleshooting issues or cleaning up unnecessary files, you can delete the .pyc files using the following methods ?

Using Command Line

The find method is a command-line utility for searching files in a directory. To find and delete all .pyc files ?

# To find all .pyc files in current directory and subdirectories
find . -name '*.pyc'

# To delete all .pyc files
find . -name '*.pyc' -delete

Using Python Script

You can also delete .pyc files programmatically using Python ?

import os
import glob

# Find all .pyc files recursively
pyc_files = glob.glob('**/*.pyc', recursive=True)

# Delete each .pyc file
for file in pyc_files:
    try:
        os.remove(file)
        print(f"Deleted: {file}")
    except OSError as e:
        print(f"Error deleting {file}: {e}")

print(f"Total .pyc files processed: {len(pyc_files)}")

Key Points

Aspect Description
Purpose Store compiled bytecode for faster execution
Location __pycache__ directory
Creation Automatically when module is imported
Naming Includes Python version (e.g., .cpython-39.pyc)

Conclusion

.pyc files are compiled bytecode versions of Python source files that improve program execution speed by avoiding recompilation. Python automatically manages these files in the __pycache__ directory, and they are regenerated when the source code changes.

Updated on: 2026-03-24T18:13:50+05:30

41K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements