How To Execute A Python Script In Windows | Proven Methods

Run py script.py in Command Prompt, PowerShell, or Windows Terminal after navigating to the script’s folder to execute a Python script on Windows.

The fastest, most reliable way to execute a Python script on Windows is through a terminal session. Open Command Prompt, PowerShell, or Windows Terminal, use cd to reach the folder containing your .py file, and type py script.py (or python script.py if the Python executable is in your PATH). If you prefer a graphical editor, Visual Studio Code includes a dedicated “Run Python File in Terminal” button. This guide covers all major methods—terminal, editor, and double‑click—along with solutions to the most common execution problems.

How Do You Run A Python Script From The Command Line?

Command‑line execution is the standard, version‑stable method. Open a terminal—Command Prompt, PowerShell, or Windows Terminal—and change to the directory containing your script using the cd command. Then run one of these commands:

  • py script.py – Uses the Python Launcher for Windows, which is installed with Python and is the recommended command on Windows per the official Windows documentation.
  • python script.py – Works if Python was added to your PATH during installation. Verify with python --version.

Replace script.py with your actual file name. If the path contains spaces, quote the full path: py "C:\My Scripts\hello.py". After pressing Enter, you’ll see the script’s output in the same terminal window.

Running A Python Script In Visual Studio Code

Visual Studio Code (VS Code) provides a convenient button for running scripts without leaving the editor. Open your .py file in VS Code, then click the triangular Run Python File in Terminal button in the top‑right corner of the editor, or right‑click anywhere in the file and choose Run > Python File in Terminal. A terminal panel opens at the bottom, activates the correct Python interpreter, and runs your script. The output appears in that panel. The VS Code Python docs note that this method uses the interpreter selected in your editor settings, so ensure it matches the version you expect.

Can You Execute A Python Script By Double‑Clicking It?

Yes, if the .py file extension is associated with the Python interpreter. After installing Python, double‑clicking a .py file typically opens it in a terminal window briefly, then closes it—which is fine for scripts that produce quick output. For scripts that need to stay open (e.g., a GUI), use the .pyw extension instead; it runs without a console window. If double‑clicking doesn’t work, set the default program for .py files to python.exe (located in your Python installation folder) and check “Always use this app to open .py files.”

The table below compares the most common execution methods side by side.

Method How To Run Best For
Command Prompt + py cd folder && py script.py Most Windows users; uses the official launcher
Command Prompt + python cd folder && python script.py When Python is in PATH
PowerShell + py cd folder; py script.py PowerShell fans; same command works
PowerShell + python cd folder; python script.py If py is not recognized
Windows Terminal Open any shell in Terminal, then same as above Users who want the latest terminal features
VS Code Run button Click the play button or right‑click → Run Python File Editor‑first workflow; immediate feedback
Double‑click .py file Double‑click in File Explorer Quick runs after file association is set

Common Execution Problems And How To Fix Them

Even with the right command, things can go wrong. The most frequent issue is that python or py is not recognized by the terminal. This usually means Python wasn’t added to your system PATH, or the Python Launcher isn’t installed. Re‑run the Python installer and ensure Add Python to PATH is checked, or manually add the Python installation folder to the Path environment variable. Another common mistake is forgetting to use cd to navigate to the script’s folder—running a bare script name only works if the terminal is already in that directory. The table below covers these and other frequent pitfalls.

Problem Symptom Fix
Python not recognized 'python' is not recognized Add Python to PATH (re‑installer or edit environment variables)
py not recognized 'py' is not recognized Re‑install Python and ensure “Python Launcher” is selected
File association missing Double‑click opens Notepad or nothing Right‑click .py file → Open with → Choose another app → python.exe → Always
Wrong directory Script runs but can’t find its files, or “No such file or directory” Use cd to the correct folder, or provide full path
Extension omitted python script fails (without .py) Include the .py extension or add .PY to PATHEXT

A Reliable Reference For Executing Python Scripts On Windows

To make sure your scripts run every time, follow this short sequence:

  1. Install Python from python.org, checking Add Python to PATH and Install Python Launcher.
  2. Save your script with a .py extension (e.g., hello.py).
  3. Open Command Prompt, PowerShell, or Windows Terminal.
  4. Change to the script’s directory: cd C:\path\to\your\script.
  5. Run py hello.py or python hello.py.
  6. Verify the output. If nothing appears or an error shows, check the tables above for likely fixes.

Whether you prefer the terminal or an editor like VS Code, these methods cover every scenario. With the PATH set correctly and the Python Launcher installed, executing a Python script on Windows becomes a single command.

References & Sources

Please use a real email you check. If it's fake or mistyped, your message won't reach us and we can't reply — wrong addresses are rejected automatically.