.PY

PY File

Python Source Code File
Ask a question
QUICK ANSWER

A .py file is a Python source code file - plain text containing a program or script written in the Python language. To read the code, open it in any text editor. To run it, install Python from python.org, then type "python file.py" in a terminal. Never run a .py from an untrusted source without reading it first, since it executes with your full user permissions.

Developer: Python Software Foundation (language created by Guido van Rossum) Category: Developer Files Open standard MIME: text/x-python
OPENS ON Windows macOS Linux
Related: .DO · .MD · .HEX · .XSD

On this page

19k+ extensions indexed
Last reviewed Jul 1, 2026

Not sure what your file is?

Drop any file into our identifier - we read just the first bytes to name the format.

Identify a file

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: HIGH

A .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 nutshell
FULL NAMEPython Source Code File
DEVELOPERPython Software Foundation (language created by Guido van Rossum)since 1991 (Python 0.9.0); language steward: Python Software Foundation since 2001
MIME TYPEtext/x-python
TYPEPlain-text source code (UTF-8 by default)
STANDARDOpen · royalty-free

Programs that open PY files

Windows4 apps
Notepad Open-source Open the .py to read or edit the code with syntax highlighting (does not run it - that needs Python).
Python (run it) Open-source Install Python from python.org (check 'Add to PATH' / py launcher). Then in a terminal: 'python file.py'. To keep the window open after a crash, run from an already-open terminal rather than double-clicking.
Visual Studio Code Free Open the .py, install the Microsoft Python extension, and press Run (F5) to edit, debug, and execute.
PyCharm Community Free Full Python IDE (free Community edition) - open the project, run and debug .py files with one click.
macOS2 apps
Python (run it) Open-source macOS ships an old/limited Python; install the current one from python.org or Homebrew, then 'python3 file.py' in Terminal.
Visual Studio Code Free Open the .py with the Python extension to edit, debug and run.
Linux3 apps
GNU Emacs Open-source Open the .py to edit with python-mode; run with the built-in inferior-python shell.
Python (run it) Open-source Pre-installed on most distros: 'python3 file.py'. Use the distro package manager or pyenv for a specific version.
Visual Studio Code Free Edit, debug and run .py files with the Python extension.

Technical details

deep spec
File typePlain-text Python source code
MIME typetext/x-python
Default encodingUTF-8 (PEP 3120); overridden by a `# -*- coding: <name> -*-` declaration on line 1 or 2
Shebang supportUnix/Linux scripts place `#!/usr/bin/env python3` on line 1 to allow direct execution without invoking the interpreter explicitly
Indentation syntaxSignificant 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 cacheCPython compiles `.py` source to `.pyc` bytecode and stores it in `__pycache__/` to speed up subsequent imports
Type hint supportOptional 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
RuntimesCPython (reference implementation), PyPy, Jython, MicroPython, GraalPy
Line endingsLF 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 bytesNone - `.py` is a plain-text file with no binary signature; identified by content (shebang, `import`/`def`/`class` keywords, or indentation pattern)
Released1991 (Python 0.9.0); language steward: Python Software Foundation since 2001
Open standardYes · royalty-free
Specificationdocs.python.org

PY conversions

Community Q&A

asked by users
Ask a quick question
Get help from people who work with PY files. Be specific - include your system and software version.
No account needed · answers usually within a day
R
Ryan11 d agoUNANSWERED
import random class Creature: def __init__(self, name, hp, attack): self.name = name self.hp = hp self.attack = attack def is_alive(self): return self.hp > 0 def attack_target(self, other): damage = random.randint(self.attack - 2, self.attack + 2) other.hp -= damage print(f"{self.name} attacks {other.name} for {damage} damage!") # Create creatures player = Creature("Flameling", 30, 8) enemy = Creature("Aquabeast", 30, 7) print("🔥 A wild Aquabeast appeared!") print("Go, Flameling!\n") # Game loop while player.is_alive() and enemy.is_alive(): print(f"\n{player.name} HP: {player.hp}") print(f"{enemy.name} HP: {enemy.hp}") action = input("Choose action (attack/heal): ").lower() if action == "attack": player.attack_target(enemy) elif action == "heal": heal = random.randint(4, 8) player.hp += heal print(f"{player.name} heals for {heal} HP!") else: print("Invalid action!") continue if enemy.is_alive(): enemy.attack_target(player) # End result if player.is_alive(): print("\n🎉 You won the battle!") else: print("\n💀 You lost...")
Waiting for an answer
B
Ben11 d agoUNANSWERED
import random class Creature: def __init__(self, name, hp, attack): self.name = name self.hp = hp self.attack = attack def is_alive(self): return self.hp > 0 def attack_target(self, other): damage = random.randint(self.attack - 2, self.attack + 2) other.hp -= damage print(f"{self.name} attacks {other.name} for {damage} damage!") # Create creatures player = Creature("Flameling", 30, 8) enemy = Creature("Aquabeast", 30, 7) print("🔥 A wild Aquabeast appeared!") print("Go, Flameling!\n") # Game loop while player.is_alive() and enemy.is_alive(): print(f"\n{player.name} HP: {player.hp}") print(f"{enemy.name} HP: {enemy.hp}") action = input("Choose action (attack/heal): ").lower() if action == "attack": player.attack_target(enemy) elif action == "heal": heal = random.randint(4, 8) player.hp += heal print(f"{player.name} heals for {heal} HP!") else: print("Invalid action!") continue if enemy.is_alive(): enemy.attack_target(player) # End result if player.is_alive(): print("\n🎉 You won the battle!") else: print("\n💀 You lost...")
Waiting for an answer

Frequently asked questions

How do I run a .py file?
Install Python from python.org, open a terminal in the file's folder, and type 'python file.py' (Windows) or 'python3 file.py' (Mac/Linux). On Windows with the py launcher you can also double-click it, but running from a terminal lets you see errors before the window closes.
How do I open a .py file to see the code?
It's plain text - open it in any editor. Use VS Code or Notepad++ for syntax highlighting (Notepad works but is bare). Opening it to read is completely safe; only RUNNING it executes the code.
Can I convert a .py file to an .exe?
Not as a file conversion, but you can package it into a standalone Windows .exe with PyInstaller ('pyinstaller --onefile file.py'), cx_Freeze, or Nuitka. The result runs without Python installed.
Why won't my Python script run?
Common causes: Python isn't installed or not on PATH; the script targets Python 2 (EOL 2020) but you have Python 3, or vice versa (print syntax differs); or a required library is missing ('ModuleNotFoundError' - fix with 'pip install <name>').
What's the difference between .py and .pyc?
.py is the human-readable source you write and edit; .pyc is the compiled bytecode Python caches (in __pycache__) to start faster. You edit .py; Python regenerates .pyc automatically. The .pyc is tied to a specific Python version.
I double-clicked my .py and a black window flashed and vanished - what happened?
The script ran and finished (or crashed) and closed the console instantly. Run it from an already-open terminal to read the output/error, or add 'input()' at the end to pause. Use a .pyw extension for GUI scripts that shouldn't show a console.

References

1Python.org - official downloads and documentationwww.python.org
2Python 3 Language Referencedocs.python.org

Keep exploring

across the database

Top extensions this week

1.AQQAQQ Instant Messenger File
2.CRDOWNLOADChrome Partial Download File
3.BINCD/DVD Disc Image (BIN/CUE)
4.DATProgram Data File (generic)
5.PARTPartial Download File
6.EXEWindows Executable (Portable Executable)
7.RPMSGRestricted Permission Message
8.NOMEDIAAndroid No-Media Marker File
9.MDMarkdown Document
10.TXTPlain Text File

Related extensions

.DOStata Do-File
.MDMarkdown Document
.HEXIntel HEX File
.XSDXML Schema Definition
.MAPSource Map (JavaScript / CSS)
.CONFIGConfiguration File

Free file tools

An in-browser file identifier and image converter - everything runs on your device.

Open the toolbox

Browse file extensions A-Z