FILE FORMAT GUIDE

PY File: Read, Edit and Run Python Source

A PY file contains Python source code. Open it in a text editor to read or edit it; running the script requires a Python interpreter and can execute commands on your computer.

Quick facts

Extension
.py
Format
Python Source File
Category
system
MIME type
Unknown
Standard / developer
Python Software Foundation
Open with
Visual Studio Code

What is PY?

Python source is readable text whose indentation, statements and encoding follow Python’s language rules. Reading a .py file in an editor is different from passing it to an interpreter. This guide covers Python source, not unrelated files that happen to use the same suffix.

How to open PY

Use an editor to read unfamiliar scripts. Invoking Python executes the file.

Visual Studio CodeWindows / macOS / Linux

Open the .py file to read and edit source. Python language support helps with editing; running or debugging also requires a separately installed interpreter.

Common uses and compatibility

  • Store Python scripts that the interpreter can execute.
  • Read and edit program source with syntax-aware editor support.

Things to check

  • An editor alone does not provide the Python interpreter required to run code.
  • An encoding or indentation error can prevent Python from parsing otherwise readable source.

Technical details

Signature
Unknown
Encoding
UTF-8 by default in Python 3; a source-encoding declaration can specify another encoding
Container
Unknown
Compression
Unknown

Python 3.13 Language Reference

How to open a PY file without running it

Open the file from inside a text editor. Avoid Run, Debug and terminal execution while you are only reading code from an unfamiliar source.

  1. Keep the original file and open a copy through Visual Studio Code’s File > Open File command.
  2. Read the source before using any Run or Debug action. Select Python language support if you want syntax highlighting.
  3. Save edits as plain text with the .py extension; preserve indentation and check the source encoding.

Sources: Visual Studio Code: Python editing and interpreter selection · Python 3.13: using the interpreter

How do I run a Python script?

For code you trust, select an installed interpreter and run the script through your development environment or terminal. Python’s documentation describes passing a script filename to the interpreter. This executes the source; it is not a preview.

Trusted script, with a Python 3 interpreter already installed

python3 hello.py

Sources: Python 3.13: using the interpreter · Visual Studio Code: Python editing and interpreter selection

Why does a PY file open as text or close immediately?

Opening it as text is appropriate when you want to inspect or edit source. To see program output and errors, run trusted code in an existing terminal or the editor’s terminal rather than relying on a filename association.

Sources: Visual Studio Code: Python editing and interpreter selection · Python 3.13: using the interpreter

What encoding does a Python source file use?

Python 3 defaults to UTF-8 unless a valid source-encoding declaration specifies another encoding. Preserve the declaration and indentation when editing; changing whitespace can change program structure.

Sources: Python 3.13: lexical analysis and source encoding

Can I rename a PY file to EXE?

Renaming leaves the Python source unchanged. An interpreter executes source text; a Windows executable has a different structure. Ask the software author for a supported application package when you need a standalone program.

Sources: Python 3.13: using the interpreter · Python 3.13: lexical analysis and source encoding

Related formats

  • VBScript File

    Both contain program source, but Python and VBScript require different runtimes.

  • JavaScript Object Notation

    Python programs may use JSON data; JSON data is not Python program source.

  • TOML

    TOML stores configuration rather than executable Python source.

Sources & review

Sources reviewed 2026-11-05.