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
- 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.
- 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.
- 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.
- 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.
- Run from PowerShell/Terminal with py/python3 plus the script path.
- 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.
- Activate the documented venv, then pip install -r requirements.txt or poetry install.
- 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.
- Windows: install from python.org and tick “Add to PATH”, or use py -0 to list managed versions.
- 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.
- Read .python-version, runtime.txt, or CI configs.
- 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.