Fundamentals 6 min read

Understanding Common Python File Extensions and Their Uses

This article introduces the most common Python file extensions—including .py, .ipynb, .pyi, .pyc, .pyd, .pyw, and .pyx—explains their purposes, provides example code, and demonstrates how Cython (.pyx) can significantly improve performance for compute‑intensive tasks.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Common Python File Extensions and Their Uses

The article explains various Python-related file extensions, describing their purpose and typical usage scenarios.

.py is the standard source file for Python code.

.ipynb denotes Jupyter Notebook files, which store interactive notebooks.

.pyi files provide type hint information for static analysis; an example:

<code>hellp.pyi
def hello(name: str) -> None:
    print(f"hello {name}")</code>

.pyc files are compiled byte‑code representations of Python modules, enabling faster loading.

.pyd files are binary extension modules written in C or C++ that can be imported like regular Python modules.

.pyw files are Windows‑specific scripts that run without opening a console window; an example GUI script:

<code># click_button.pyw
import tkinter as tk

def button_click():
    label.config(text="Button Clicked!")

window = tk.Tk()
button = tk.Button(window, text="Click Me", command=button_click)
button.pack()

label = tk.Label(window, text="Hello, World!")
label.pack()
window.mainloop()</code>

.pyx files are Cython source files that allow mixing Python with C for performance gains. The article provides a Fibonacci implementation in Cython and compares its speed to pure Python:

<code>cdef int a, b, i
def fibonacci(n):
    if n <= 0:
        raise ValueError("n必须是正整数")
    if n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        a = 0
        b = 1
        for i in range(3, n + 1):
            a, b = b, a + b
        return b</code>
<code>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

python_time = timeit.timeit("fibonacci(300)", setup="from __main__ import fibonacci", number=1000000)
cython_time = timeit.timeit("fb.fibonacci(300)", setup="import fb", number=1000000)
print("纯Python版本执行时间:", python_time)
print("Cython版本执行时间:", cython_time)</code>
<code>纯Python版本执行时间: 12.391942400000516
Cython版本执行时间: 6.574918199999956</code>

The benchmark shows that Cython can nearly double the execution speed for this compute‑intensive Fibonacci calculation.

PerformancePythonprogramming fundamentalsCythonfile-extensions
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

login 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.