If Python is interpreted, what are .pyc files?

.pyc files are compiled bytecode files that are generated by the Python interpreter when a .py file is imported. They contain the compiled version of the Python code in the .py file, which can be executed faster by the interpreter than interpreting the code directly from the .py file.

When a .py file is imported, the Python interpreter checks if a corresponding .pyc file exists in the same directory. If it does, the interpreter uses the .pyc file to execute the code, rather than interpreting the code from the .py file. If the .py file is modified, the .pyc file is automatically regenerated.

Here is an example of how a .pyc file is generated:

# my_module.py
def my_function():
    print("Hello, world!")
import my_module
# A my_module.pyc file is generated in the same directory as my_module.py

Here is an example of how a .pyc file is used:

# my_module.py
def my_function():
    print("Hello, world!")
import my_module
my_module.my_function()
# Hello, world!
# The interpreter uses the my_module.pyc file to execute the code in my_module.my_function()

Note: The .pyc files are not human-readable, they are only intended to be used by the Python interpreter.