What do the python file extensions, .pyc .pyd .pyo stand for?


The .py, .pyc, .pyo and .pyd files have their own significance when it comes to executing python programs. They are used for −

.py: The input source code that you've written.

.pyc: The compiled bytecode. If you import a module, python will build a *.pyc file that contains the bytecode to make importing it again later easier (and faster).

.pyo: A *.pyc file that was created while optimizations (-O) was on.

.pyd: A windows dll file for Python.

In Python, there are several file extensions that are used to indicate different types of files. Here are some of the most common file extensions in Python and their meanings −

.py files

.py: This is the standard file extension for Python source code files. These files contain Python code that can be executed by a Python interpreter. Python source code is written in files with a .py extension. For example, a file named "my_script.py" contains Python code that can be executed by a Python interpreter. −

Example

# hello.py
# my_script.py
def greet(name):
   print("Hello, " + name + "!")
greet("Alice")

Output

Hello, Alice!

This code can be run by executing python hello.py in the command line.

.pyc files

pyc: This is the file extension for compiled Python code files. When a .py file is executed, the Python interpreter compiles the code to bytecode and saves it in a .pyc file to improve performance on subsequent executions.

When you run this script, the interpreter will create a compiled bytecode version of the code and save it to a file named my_script.pyc. For example −

Example

# hello.py
print("Hello, world!")

Output

Hello, world!

Running this code will generate a hello.pyc file in the same directory.

.pyo files

.pyo: This is another file extension for compiled Python code files. The only difference between .pyc and .pyo files is that .pyo files are compiled with optimizations enabled. If you run this script with the -O flag, the interpreter will create an optimized compiled bytecode version of the code and save it to a file named my_script.pyo. For example −

Example

# hello.py
# my_script.py
def greet(name):
   print("Hello, " + name + "!")
greet("Alice")

Output

Hello, Alice!

Running this code will generate a my_script.pyc file, but running the code with the -O flag (python -O my_script.py) will generate a my_script.pyo file instead.

.pyd files

.pyd: This is a file extension used on Windows for binary files that contain compiled Python code. These files are similar to .pyc files, but are designed to be used as dynamic link libraries (DLLs) that can be loaded by other programs. If you have a Python module that contains code written in C or C++, the compiled code is saved to a shared library file with a .pyd file extension. It must be noted that .pyd files are specific to the Windows platform. On other platforms (such as macOS or Linux), the equivalent file extension is .so (shared object) or .dylib (dynamic library).

Example

# mymodule.py
# my_module.py
def add(a, b):
   return a + b
print(add(3,4))
# my_module.pyd
# code implements the `add` function and is compiled to a shared library

Output

7

Compiling this code with cython --embed mymodule.py will generate a mymodule.c file, which can then be compiled into a mymodule.pyd file using a C compiler.

Overall, these file extensions in Python represent different stages of code compilation and execution; different types of files have their corresponding purposes.Understanding their meanings and differences can help you write more efficient and optimized Python code,and choose the appropriate file extensions for your specific needs.

Updated on: 02-May-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements