.py File Extension
.py file contains, how to open and run Python source safely, how Poly supports it, and when to use another format. File extension details for .py
Full Support| File type | Python source file |
|---|---|
| MIME type | |
| Encoding | Unicode source text, UTF-8 by default; a source encoding declaration can select another codec |
| Operating systems | Windows, macOS, Linux |
| Opens with | Visual Studio Code, PyCharm, Python, Any text editor |
| Poly support | Yes. Poly recognizes .py files as Python source and provides code-aware text features. |
| Indexed by Poly | Yes. Poly indexes readable source for exact and semantic search without running it. |
| Preview in Poly | Yes. .py files open in Poly's code editor with Python syntax highlighting. |
| Poly agent | Yes. The Poly agent can read and reason about the available source without executing it. |
What kind of file is a .py?
A .py file contains Python source code. It may be a script you run directly, an importable module, or one piece of a larger package. The widely used text/x-python MIME type describes these files, although it is not registered with IANA and some systems label them text/plain instead.1
Python source has no dependable magic number. An optional first line such as #!/usr/bin/env python3 can identify an interpreter on Unix-like systems, but the filename, media type, and readable contents are stronger clues when considered together.
Python 3 treats source as UTF-8 by default. A specially formatted comment on the first or second line can declare another character encoding for the whole file.2
.py suffix means source text. A .pyc file contains cached bytecode and is not interchangeable with it. Renaming either file does not convert its contents.How do I open a .py on my computer?
Any text editor can open a .py file on Windows, macOS, or Linux. A Python-aware editor is more useful because Python indentation affects program structure.
- Visual Studio Code provides completion, navigation, linting, testing, and debugging when its Python extension and a Python interpreter are installed.3
- PyCharm provides Python-focused code analysis, interpreter setup, testing, and debugging.4
- Python can run a trusted file from a terminal with
python path/to/script.py.5
Opening a file for inspection is different from executing it. Review unfamiliar scripts first. Python code can modify files, read credentials, start processes, access networks, and import more code.
Poly support for .py
Poly opens .py source in a code-friendly editor with Python syntax highlighting, word wrap, and in-file search. It indexes readable source for exact and semantic search, so you can find a known identifier or search by what the code does.
The Poly agent can summarize functions and classes, explain visible control flow, and help reason about changes. Poly does not execute the program. Runtime state, environment variables, generated code, network responses, and dependencies are available only when you provide the relevant material.
.py file, even if you never run it.History of .py
Guido van Rossum started work on Python in December 1989 as a successor to ABC. He chose the name as a reference to Monty Python's Flying Circus, not the snake.6 Python's first public release followed in 1991.
The .py suffix became the conventional marker for Python source as the language spread across Unix, Windows, and macOS tools. The freedesktop.org shared MIME database associates both .py and .wsgi with text/x-python and also uses interpreter lines as supporting evidence.1
Working with .py and alternatives
Keep .py when people need portable, reviewable source. Save it as UTF-8 unless a project has a specific legacy requirement, and preserve the encoding declaration when one is necessary.
Choose another format only when the workflow calls for it:
| Goal | Better fit | Tradeoff |
|---|---|---|
| Mix executable cells, prose, and saved output | Jupyter .ipynb notebook | More metadata and execution state than a plain script |
| Distribute an installable project | Python package or wheel | Requires package metadata and a build workflow |
| Cache imported code locally | .pyc bytecode | Interpreter-specific and not a source replacement |
| Publish an explanation | HTML or PDF documentation | Readable, but not a runnable substitute |
Changing .py to another suffix does not translate the program. Conversion must preserve valid Python syntax, source encoding, dependencies, and any required interpreter version.
Footnotes
- freedesktop.org. Shared MIME Info Database: Python Source Definitions. The database defines
text/x-python,.pyand.wsgifilename patterns, and Python interpreter-line matches. ↩ ↩2 - Python Software Foundation. Lexical Analysis: Encoding Declarations. ↩
- Microsoft. Python in Visual Studio Code. ↩
- JetBrains. Python in PyCharm. ↩
- Python Software Foundation. The Python Command Line and Environment. ↩
- van Rossum, G. Foreword for Programming Python, First Edition. Python.org, May 1996. See also the Python Software Foundation's General Python FAQ, which dates the first public posting to February 1991. ↩