What is the PY file format?
.py is the file extension used by script files written in the Python programming language. A .py file contains source code written in Python - stored as plain text in UTF-8 encoding by default, so it can be read and modified without any special tooling.
.py file versatility
The .py format has many applications and is extremely popular, driven by Python's widely-used interpreter and its vast ecosystem of libraries. This format is supported on many operating systems, including Windows, macOS, and Unix/Linux. Python's reach now extends from web development and data science to machine learning and automation, making .py files present across virtually every area of modern software development.
Managing .py files
Viewing, opening, and editing .py files is possible using any text editor. Running a .py script, however, requires a Python interpreter installed on the system - the standard commands are python script.py or python3 script.py. .py script files are commonly used for system administration, task automation, and data processing. When Python imports a module it may create a compiled .pyc cache alongside the source. Related variants include .pyw (Windows GUI scripts with no console window) and .pyi stub files used for type hints.
Security & safety
RISK: HIGHA .py file is a program - running it executes whatever code it contains, with your user permissions. NEVER run a Python script from an untrusted source without reading it first; malicious scripts can delete files, install malware, or steal data, and Python is a common malware-delivery language. Just OPENING a .py in a text editor is safe (it's only text). Be cautious of "pip install" of unknown packages a script requires - typosquatted PyPI packages are a real attack vector. When in doubt, read the source; .py is human-readable by design.
Format details
in a nutshellPrograms that open PY files
Technical details
deep spec| File type | Plain-text Python source code |
| MIME type | text/x-python |
| Default encoding | UTF-8 (PEP 3120); overridden by a `# -*- coding: <name> -*-` declaration on line 1 or 2 |
| Shebang support | Unix/Linux scripts place `#!/usr/bin/env python3` on line 1 to allow direct execution without invoking the interpreter explicitly |
| Indentation syntax | Significant whitespace - indentation is part of the grammar; PEP 8 recommends 4 spaces per level |
| Module entry point | `if __name__ == "__main__":` guard separates importable code from code that runs only when the file is executed directly |
| Compiled cache | CPython compiles `.py` source to `.pyc` bytecode and stores it in `__pycache__/` to speed up subsequent imports |
| Type hint support | Optional inline annotations per PEP 484 (Python 3.5+); separate stub definitions live in `.pyi` files |
| Comment syntax | `#` introduces a single-line comment; triple-quoted strings (`"""..."""`) serve as docstrings for modules, classes, and functions |
| Runtimes | CPython (reference implementation), PyPy, Jython, MicroPython, GraalPy |
| Line endings | LF preferred across platforms; CRLF is accepted on Windows |
| Import system | `import` and `from ... import` load modules; a directory containing `__init__.py` is treated as a package |
| Magic bytes | None - `.py` is a plain-text file with no binary signature; identified by content (shebang, `import`/`def`/`class` keywords, or indentation pattern) |
| Released | 1991 (Python 0.9.0); language steward: Python Software Foundation since 2001 |
| Open standard | Yes · royalty-free |
| Specification | docs.python.org |