.PY

How to Open a PY File

To read a PY file, open it in a text editor. To run it, use a Python interpreter from a terminal or the browser runner below for a compatible script. Desktop scripts may need packages, files or system features that the browser runtime cannot provide. Choose reading or execution before troubleshooting.

Category: Source code / script Developer: Python Software Foundation MIME type: text/x-python Native support: None — needs a Python interpreter

Choose what you need

Open & Run a PY File Online

Drop a .py file below to see its source with syntax highlighting and a quick structural overview (imports, functions, classes) — and actually run it using a real Python interpreter compiled to WebAssembly, right in this page.

Before you use this tool: The browser runner uses Pyodide. It does not provide unrestricted desktop files, processes or every native Python package. Reading the code and running it are separate actions; use local Python for scripts that depend on desktop features.

🔒 Nothing is uploaded — your file never leaves this device

What is a PY file?

A .py file is a plain-text source file written in Python, one of the world's most widely used programming languages, created by Guido van Rossum and first released in 1991, now maintained by the Python Software Foundation. Being plain text, a .py file opens in literally any text editor — there's no special binary structure to it, the same way there isn't for a .txt file.

What makes a .py file useful is that it can be executed: a Python interpreter reads the file top to bottom and carries out the instructions it contains, whether that's printing text, doing calculations, processing data, or running a full application. That's the real reason "opening" a .py file usually means something different from opening a .pdf or a .jpg — you're not just viewing content, you're choosing whether to read it or run it.

How to run a PY file on Windows

  1. Download and install Python from python.org, making sure to check "Add Python to PATH" during setup — this step trips up most first-time installs.
  2. Open a terminal in the script's folder (Shift + right-click the folder, choose "Open PowerShell window here" or similar).
  3. Run: python filename.py
  4. The script's output appears directly in the terminal.

Double-clicking a .py file in File Explorer will also run it if Python is installed and associated with the extension, but the terminal method above lets you actually see any output or errors, which double-clicking usually doesn't.

How to run a PY file on Mac

  1. Open Terminal (Applications > Utilities > Terminal).
  2. Navigate to the script's folder: cd path/to/folder
  3. Run: python3 filename.py

Modern macOS doesn't ship a full Python installation by default — if python3 isn't found, install it from python.org or via Homebrew: brew install python.

How to run a PY file on Linux

Most Linux distributions come with Python pre-installed. Open a terminal and run:

python3 filename.py

If it's not installed, get it with your package manager, e.g. sudo apt install python3 on Debian/Ubuntu, or sudo dnf install python3 on Fedora.

Double-clicked a .py file and the window flashed and closed?

That's normal behaviour, not a crash. On Windows, double-clicking a .py file starts it in a console window, and that window closes the instant the script finishes — or the instant it fails with an error, so you never get to read the message. Three ways to see what happened:

  1. Run it from a terminal instead. In File Explorer, click the folder's address bar, type cmd and press Enter; a Command Prompt opens in that folder. Run py script.py (or python script.py). The window stays open and any error traceback stays on screen.
  2. Make the script wait. If it's your own script, add input("Press Enter to exit...") as the last line so the console stays open until you press a key.
  3. Open it in IDLE. Python for Windows includes IDLE, a simple editor. Right-click the file → Show more options (Windows 11) → Edit with IDLE, then press F5 to run it. Output and errors appear in IDLE's shell window, which doesn't close.
The classic Windows right-click menu for a .py file (Windows 11: Show more options). "Edit with IDLE" opens the script in Python's own editor, where F5 runs it.

Double-clicking opens the code in Notepad or VS Code instead of running it

Another app has taken over the .py association. That's actually the safer default, but if you want double-click to run scripts: right-click a .py file → Open with → Choose another app, select Python (the py.exe launcher), and choose Always. On a Mac, double-clicking a .py usually opens it in a text editor or Xcode; run scripts from Terminal as shown above.

Running a script without a console window: .pyw

Scripts with a graphical window (Tkinter, PyQt) are often saved as .pyw. Windows runs those with pythonw.exe, which opens no console at all — so if a .pyw does nothing visible when double-clicked, run it as py script.pyw in a terminal to see its errors.

Fix "python is not recognized" and "Python was not found" on Windows

Windows has three different ways of finding Python, and these two errors mean different things:

  • "'python' is not recognized as an internal or external command, operable program or batch file." — no Python on your PATH at all.
  • "Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases." — you're hitting Windows' built-in placeholder python.exe, which only exists to send you to the Store.
When python isn't on the PATH, the py launcher installed by python.org's Windows installer usually still works.
  1. Try the launcher first: py script.py. Python installed from python.org includes the py launcher, which is found even when PATH wasn't set. py --list shows every installed version and py -3.12 script.py picks one.
  2. Turn off the Store placeholder if you installed Python from python.org: Windows 11 Settings > Apps > Advanced app settings > App execution aliases (Windows 10: Settings > Apps > Apps & features > App execution aliases) and switch off python.exe and python3.exe ("App Installer").
  3. Or fix PATH: re-run the python.org installer, choose Modify, and tick Add Python to environment variables; or reinstall with Add python.exe to PATH checked on the first screen. Close and reopen the terminal afterwards — an open window keeps the old PATH.
  4. Or install from the Microsoft Store (Python Software Foundation's own listing, or the newer Python Install Manager). Both make python and py work without touching PATH.

On Mac and Linux the command is python3, not python. On a Mac without Python, typing python3 may offer to install Apple's Command Line Developer Tools, which include a Python 3 — fine for running scripts, though python.org or Homebrew give you a newer version.

Install the modules a script needs (pip, requirements.txt, venv)

Most downloaded scripts import third-party packages. If a module is missing, Python stops with ModuleNotFoundError and names it. Install it with the same Python you run the script with — using -m pip guarantees that:

  • Windows: py -m pip install requests
  • Mac / Linux: python3 -m pip install requests
  • If the project has a requirements.txt: py -m pip install -r requirements.txt installs everything at once.
A missing-module error and the pip command that fixes it. Install with the same python3 you use to run the script.

The pip name isn't always the import name

Error saysInstall with
No module named 'cv2'pip install opencv-python
No module named 'PIL'pip install pillow
No module named 'sklearn'pip install scikit-learn
No module named 'yaml'pip install pyyaml
No module named 'bs4'pip install beautifulsoup4
No module named 'dotenv'pip install python-dotenv

"error: externally-managed-environment"

Recent Ubuntu, Debian and Fedora releases and Homebrew's Python refuse system-wide pip installs so they don't break the OS's own packages. Create a virtual environment for the project instead:

  1. python3 -m venv .venv in the script's folder.
  2. Activate it: source .venv/bin/activate (Mac/Linux) or .venv\Scripts\activate (Windows).
  3. Now pip install -r requirements.txt and python script.py use that private environment.

Only the standard library is available in the browser tool on this page; scripts that need packages like these have to run on a local Python install.

How to open or run a PY file on Android, iPhone or iPad

Phones don't include Python, but you can always read a .py file in any text or code viewer, and you can run standard-library scripts in the tool at the top of this page from your phone's browser. To run scripts properly on the device:

  • Android — Pydroid 3: a Python 3 IDE from Google Play with an editor, console and a built-in pip for many popular packages. Open the .py from your Downloads folder and tap the run button.
  • Android — Termux: a Linux terminal app (get it from F-Droid or GitHub for the up-to-date build); pkg install python, then python script.py like on Linux.
  • iPhone / iPad — Pythonista 3 (paid) or Pyto: full editors with run buttons and iOS-specific modules. a-Shell (free) is a terminal with python3 built in — open the script's folder and run python3 script.py.

Scripts that rely on desktop features (Tkinter windows, Windows paths, packages with compiled code that hasn't been ported) may not run on mobile even when the app itself works. A Python project you received as a ZIP has to be extracted first so the app can see all its files.

How to convert a .py file to an .exe

Python scripts aren't compiled, so there's no true "converter" — but PyInstaller bundles your script, the Python interpreter and every imported package into a single program you can run on PCs without Python:

  1. Install it: py -m pip install pyinstaller
  2. In the script's folder run pyinstaller --onefile myscript.py. Add --noconsole for a GUI app that shouldn't open a console window, and --icon app.ico for a custom icon.
  3. Your program appears in the new dist folder as myscript.exe; the build folder and .spec file can be deleted (or kept to rebuild the same way).
  • Build on the target OS: PyInstaller makes a Windows .exe only on Windows, a Mac app only on macOS.
  • Expect large files (often tens of MB) and occasional antivirus false positives on one-file builds; signing the .exe or using the default one-folder mode helps.
  • Alternatives: auto-py-to-exe is a point-and-click front end for PyInstaller; Nuitka compiles Python to C for faster, harder-to-unpack executables.

The reverse — turning an .exe back into .py — only works for programs that were built with tools like PyInstaller in the first place, and usually yields .pyc bytecode rather than clean source.

.py, .pyw, .pyc, .pyd and other Python files

ExtensionWhat it isCan you open it as text?
.pyPython source codeYes
.pywPython source meant to run without a console window on WindowsYes — identical to .py inside
.pycCompiled bytecode cached in __pycache__ folders when a module is importedNo. Safe to delete — Python recreates it.
.pydA compiled extension module for Windows (a DLL in disguise — see DLL files)No
.pyiType-hint "stub" file used by editors and type checkersYes
.pyzA zipped, runnable Python application (zipapp)No — run with py app.pyz, or open as a ZIP
.ipynbJupyter notebook (JSON with code cells and outputs)Readable, but best opened in Jupyter or VS Code

Which editor should I use?

For quick viewing, any text editor works. For actually writing or editing Python code, a dedicated editor makes a big difference:

  • Visual Studio Code (free) — the most popular general-purpose editor, with an excellent Python extension.
  • PyCharm (free Community edition available) — a full Python-specific IDE with deep debugging and refactoring tools.
  • Jupyter Notebook — ideal for data science and step-by-step, cell-by-cell exploration rather than running a whole script at once.

Is it safe to run a .py file?

Only if you trust where it came from. Unlike opening a document or image, running a Python script means letting arbitrary code execute on your computer — it can read, modify or delete files, make network requests, or install other software, exactly like any other program. Read through the code first (or use the viewer above) if you're at all unsure, and never run a script downloaded from an untrusted source without understanding what it does.

Common PY file errors and how to fix them

"'python' is not recognized as an internal or external command" (Windows)

Python isn't in your system PATH. Reinstall from python.org with "Add Python to PATH" checked, or add the install folder to PATH manually.

"ModuleNotFoundError: No module named '...'"

The script imports a package that isn't installed. Install it with pip install packagename (matching the name shown in the error, which is sometimes different from the import name).

"SyntaxError" pointing at code that looks fine

Usually a Python version mismatch — the script was written for Python 2 (which uses different syntax for things like print) but you're running Python 3, or vice versa. Check which version the script expects.

"IndentationError"

Python uses indentation to define code blocks, so mixing tabs and spaces, or an inconsistent indent level, causes this. Most editors can auto-convert tabs to spaces to fix it.

Technical references

A .py file is plain Python source code, so the definitive reference for what's actually inside one is the official Python documentation maintained by the Python Software Foundation.

Frequently asked questions

What program opens a .py file?

A .py file is plain text, so any text editor (Notepad, TextEdit, VS Code, Sublime Text, PyCharm) can open and display it. To actually run the code as a program, you need a Python interpreter installed (from python.org, or already present on most Mac and Linux systems), or you can run it directly in your browser using the online tool on this page without installing anything.

Can I run a Python file without installing Python?

Yes. The online tool on this page runs a real Python interpreter (compiled to WebAssembly) directly in your browser, so you can execute a .py script's standard-library code with no installation. For scripts that need third-party packages like NumPy or Pandas, or that need a full desktop/GUI environment, you'll still want a local Python installation.

Why do I get "python is not recognized" on Windows?

This means Python isn't in your system's PATH, usually because the "Add Python to PATH" option wasn't checked during installation. Reinstall Python from python.org and make sure that checkbox is selected, or manually add Python's install folder to your PATH environment variable.

Is it safe to run a .py file from the internet?

Only if you trust its source. A Python script can do anything a program can do — read, modify or delete files, access the network, or worse — so treat an unfamiliar .py file with the same caution as any executable. Reading the code first (or using this page's viewer) before running it is good practice whenever you're unsure.

Why does my Python script open and close immediately?

When you double-click a .py file on Windows, it runs in a console window that closes as soon as the script ends or crashes. Run it from a terminal instead (type cmd in the folder's address bar, then py script.py) so the output and any error stay visible, open it with Edit with IDLE and press F5, or add input("Press Enter to exit...") as the last line of your own script.

How do I convert a .py file to an .exe?

Install PyInstaller with py -m pip install pyinstaller, then run pyinstaller --onefile yourscript.py in the script's folder. The finished program appears in the dist folder and runs on Windows PCs without Python installed. Add --noconsole for GUI apps. Build on the same operating system you want the program to run on.

Can I run a .py file on my iPhone or Android phone?

Yes. On Android, Pydroid 3 or Termux (pkg install python) run Python scripts on the device. On iPhone and iPad, Pythonista 3, Pyto or the free a-Shell app run them. For standard-library scripts you can also use the online tool on this page in your phone's browser without installing anything.

How do I fix ModuleNotFoundError: No module named ...?

Install the missing package with the same Python you use to run the script: py -m pip install name on Windows, or python3 -m pip install name on Mac and Linux. The pip name sometimes differs from the import name, for example cv2 is opencv-python, PIL is pillow and sklearn is scikit-learn. If a requirements.txt file came with the script, install everything with pip install -r requirements.txt.

What is a .pyc file and can I delete it?

A .pyc file is compiled Python bytecode that Python caches in __pycache__ folders to start modules faster. It isn't human-readable and you can safely delete it, or the whole __pycache__ folder; Python regenerates it the next time the code runs.

How do I open a .py file in IDLE?

On Windows, right-click the file, choose Show more options on Windows 11, then Edit with IDLE. On any system you can also open IDLE from the Start menu or Applications and use File > Open. Press F5 to run the script; its output appears in IDLE's shell window.

Last updated October 1, 2026.