How to open .PY files on Windows

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.

Step-by-step instructions

  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.

Common 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.

Security note

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.

Back to .PY extension page