.PY file extension

To open .PY files on Windows, view/edit: open the folder in Visual Studio Code (or another editor) and select the .py file. Use Python extensions for linting, formatting, and debugging as needed.

Open .py files in a code editor to read or change them. To run, use a terminal: py yourfile.py on Windows (Python Launcher) or python3 yourfile.py on macOS/Linux. Activate the project’s virtual environment first when one exists; check requirements.txt or pyproject.toml before pip installing.

Last updated: June 11, 2026 · Reviewed by Julian Stricker

What “reviewed” means on this page
  • Format fit: whether the “what is this file?” description matches common real-world use of the extension, including category, typical MIME types, and aliases.
  • Opening paths: whether Windows / macOS / Linux steps read plausibly for current OS dialogs and default apps; we remove fantasy menus and unsafe shortcuts.
  • Security framing: whether risk notes match the extension class (for example executables vs plain data) and whether affiliate wording does not contradict the security section.
  • Sources and further reading: whether external links point to vendors, standards bodies, or other primary references where possible; we avoid inventing details we cannot ground.
  • Limits: this is clarity and safety-messaging QA, not a guarantee that every statement is exhaustive or that every binary you download is harmless.

Full methodology in the Imprint The Imprint also states where AI is used and what that does—and does not—replace.

Open on your device

Choose your operating system for a dedicated step-by-step opening guide.

How to open .PY files

Use these platform-specific instructions to open .PY files safely.

Windows

  1. View/edit: open the folder in Visual Studio Code (or another editor) and select the .py file. Use Python extensions for linting, formatting, and debugging as needed.
  2. Run once: PowerShell or Command Prompt → cd to the script directory → py .\yourfile.py. The py launcher selects an installed Python version when several exist.
  3. Project workflow: if you see venv, .venv, or poetry/pipenv metadata, activate that environment first (for example .venv\Scripts\activate), then pip install -r requirements.txt (or the tool the README names) before running the script or pytest.
  4. If double-click flashes a window: run from a terminal so stderr stays visible; set Open with to your editor if Explorer keeps executing instead of editing.
Full Windows guide

Mac

  1. View/edit: open in VS Code or PyCharm; review unknown scripts before running.
  2. Run: Terminal → cd to the directory → python3 ./yourfile.py. With a shebang and chmod +x, ./yourfile.py works the same way.
  3. Virtualenv: python3 -m venv .venv, source .venv/bin/activate, then pip install -r requirements.txt when the project provides it.
Full Mac guide

Linux

  1. View/edit: open in your editor; most distributions ship python3 but not necessarily idle shortcuts.
  2. Run: python3 ./yourfile.py from the project root when imports depend on that cwd, or use python3 -m package.module for modules.
  3. Packaged apps may ship /usr/bin helpers; still read the .py source when troubleshooting.
Full Linux guide

iOS

  1. Use a text or code app to inspect; run real Python projects on macOS, Linux, or Windows with the matching interpreter version.
Full iOS guide

Android

  1. Treat .py as text on-device unless you use a dedicated environment app; serious development and venv workflows belong on a full OS.
Full Android guide

Security notes

  • Running python malware grants file, network, and process access as your user. Inspect source from strangers before execution; never curl | python a remote script blindly.
  • pip install can run arbitrary setup hooks; prefer trusted indexes, hashes, and lockfiles.
  • Jupyter notebooks and automation glue often download .py helpers—verify checksums and repo provenance.
  • Shebang lines do not sanitize code; they only pick which interpreter runs the same bytes.

Before you run downloaded code

These files usually need a runtime (Python, Node, Java, …). They are not classic “file viruses,” but untrusted code can still do serious harm if you execute it. Prefer official packages, verify publishers, and scan archives or sketchy downloads when you are unsure.

We may earn a commission when you use affiliate links. This supports our free file extension guides.

Can't open this file?

These are the most common causes and fixes when .PY files fail to open.

Common reasons

  • Double-click runs the script or closes instantly
  • ModuleNotFoundError or ImportError
  • python3: command not found / py not recognized
  • Wrong Python version for the project

Fix steps

  1. Run from PowerShell/Terminal with py/python3 plus the script path.
  2. Add input("Press Enter…") only for quick debugging—prefer logging for real apps.

What is a .PY file?

Python scripts are UTF-8 (or declared) text interpreted by CPython, PyPy, or another implementation. A file may be a standalone script, a module imported elsewhere, or a package entry (often with if __name__ == "__main__":). The first line can be a shebang (#!/usr/bin/env python3) so Unix shells pick the interpreter when the file is executable. Packages bundle many .py files under a directory with __init__.py (namespace packages may omit it). Understanding that flow matters more than generic “format support.”

Background

Python source lives in .py files—plain text interpreted by CPython or another implementation. Professional workflows pair an editor or IDE with an interpreter, virtual environment, and dependency files (requirements.txt, pyproject.toml, Pipfile) so installs are reproducible.

Windows users typically rely on py.exe to pick among installed versions; macOS and Linux expect python3 in PATH, sometimes managed via pyenv or distro packages. Shebang lines and executable bits matter on Unix; Windows relies on explicit commands.

Because import paths depend on the current working directory and installed packages, advice like “install a compatible application” is insufficient: you align the interpreter version, activate the right venv, and run documented commands (python -m module, pytest, poetry run, etc.).

MIME tables often list text/x-python for .py (for example Debian mime.types), which helps desktops pick editors but is not a substitute for knowing how the script was meant to be launched.

Common MIME types: text/x-python

Further reading

Authoritative resources for more details on the .PY format.

Common .PY issues

Double-click runs the script or closes instantly

File associations may launch python.exe without a persistent console, hiding errors.

  1. Run from PowerShell/Terminal with py/python3 plus the script path.
  2. Add input("Press Enter…") only for quick debugging—prefer logging for real apps.

ModuleNotFoundError or ImportError

Dependencies are missing or you are outside the virtual environment / wrong cwd.

  1. Activate the documented venv, then pip install -r requirements.txt or poetry install.
  2. Run from the repository root or use PYTHONPATH only when you understand why it is needed.

python3: command not found / py not recognized

Python is not installed or not on PATH.

  1. Windows: install from python.org and tick “Add to PATH”, or use py -0 to list managed versions.
  2. macOS/Linux: install via official installer, Homebrew, or distro packages; confirm with python3 --version.

Wrong Python version for the project

Modern projects pin 3.10+ while the OS default may be older.

  1. Read .python-version, runtime.txt, or CI configs.
  2. Use py -3.12 script.py on Windows or a version manager on Unix (pyenv, asdf) to match the team.

FAQ

Editor versus interpreter—what is the difference?

The editor manipulates text. The interpreter (python3/py) executes bytecode compiled from that text. They solve different problems.

What is a virtual environment and when do I need it?

A venv isolates pip packages per project so dependencies do not clash. Activate it before running or installing when the repository documents one.

What does #!/usr/bin/env python3 do?

On Unix-like OSes it selects python3 from PATH when the file has execute permission. On Windows you still typically call py/python3 explicitly.

How do I run tests instead of the script?

Most projects document pytest, python -m unittest, or tox. Use those commands from the repo root after dependencies are installed.

Is .py always plain text?

Yes for normal source. If a file is actually binary despite the extension, it is not standard Python source—verify the download.

Similar file extensions

Compare related formats in the same category to find the right tool faster.