Fundamentals 7 min read

Mastering Python File Extensions and Their Use Cases

This article explains the purpose and typical usage of various Python file extensions—including .py, .ipynb, .pyi, .pyc, .pyw, and .pyx—provides code examples, demonstrates how to write type‑hint files, and compares pure Python with Cython for performance‑critical tasks.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Mastering Python File Extensions and Their Use Cases

Today a colleague gave me a .pyd file and asked me to run some data, which left me puzzled.

.py

The most common Python source file extension, officially called a Python source code file.

.ipynb

This is the extension for Jupyter Notebook files, representing an IPython Notebook.

Those who have studied data analysis, machine learning, or deep learning will be familiar with this format.

.pyi

A .pyi file is a Python type‑hint file that provides static type information to help developers with type checking and static analysis.

# hellp.pyi

def hello(name: str) -> None:
    print(f"hello {name}")
.pyi files are usually named to match their corresponding .py files so they can be automatically associated.

.pyc

.pyc

is the Python bytecode file extension, storing the compiled intermediate representation of Python source code; as a binary file it cannot be read directly.

.pyc files contain compiled bytecode, allowing the Python interpreter to load and execute them faster because recompilation of the source code is not needed.

.pyw

.pyw

is the extension for windowed Python script files, used to create applications without a console window, such as GUI programs.

Running a normal Python script opens a console window, but for GUI applications the .pyw extension prevents the console from appearing.

.pyx

.pyx

is the source file extension for Cython, a compiled static‑type extension language that lets Python code use C syntax and libraries for better performance.

I compared the execution speed of Cython with plain Python on a compute‑intensive task (calculating the 300th Fibonacci number).

import fb
import timeit

def fibonacci(n):
    if n <= 0:
        raise ValueError("n必须是正整数")
    if n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        a, b = 0, 1
        for _ in range(3, n + 1):
            a, b = b, a + b
        return b

# Pure Python version
timeit.timeit("fibonacci(300)", setup="from __main__ import fibonacci", number=1000000)
# Cython version
timeit.timeit("fb.fibonacci(300)", setup="import fb", number=1000000)
print("纯Python版本执行时间:", python_time)
print("Cython版本执行时间:", cython_time)
# run.py (same code as above for pure Python and Cython timing)
纯Python版本执行时间: 12.391942400000516
Cython版本执行时间: 6.574918199999956

In this compute‑intensive scenario, Cython runs almost twice as fast as pure Python, demonstrating the performance benefit of using compiled extensions for heavy calculations.

Performancetype hintsCythonfile extensionspycpyi
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.