Programming & Coding How-TosTechnology

How to Run a Python File From the Windows Terminal, Step by Step

Admin
How to Run a Python File From the Windows Terminal, Step by Step

Running a Python file from the Windows terminal is the single most useful skill a beginner can learn. It replaces the unreliable double-click, shows you every error in full, and works the same way on every machine. Yet the first attempt often stumbles on small things: the terminal opens in the wrong folder, the file name has a space in it, or Windows answers with a message about Python not being found. This guide takes you from a closed terminal to a running script, one careful step at a time, and explains what to do when each common hiccup appears.

Quick Answer

Open Command Prompt or Windows Terminal, move into the folder containing your file with cd, and run py filename.py (or python filename.py). A fast way to land in the right folder is to open the folder in File Explorer, click the address bar, type cmd, and press Enter. If Windows says Python is not found, install it from python.org with the PATH option ticked, or use the py launcher, which usually works even then.

Step 1: Know Your File’s Name and Location

  1. Find your script in File Explorer, for example C:\Users\YourName\Projects\hello.py.
  2. Turn on file name extensions if they are hidden: in Explorer choose View → Show → File name extensions. This matters because a file Windows displays as hello.py might secretly be hello.py.txt, which will not run as Python.
  3. Note the folder path. You will need the terminal to point at the same folder, or you will have to type the full path to the file.

Step 2: Open a Terminal in That Folder

You have three easy options:

  • The address bar trick (fastest): in the Explorer window showing your file, click the empty part of the address bar, type cmd, and press Enter. Command Prompt opens already set to that folder.
  • Right-click menu: in the folder, right-click empty space and choose Open in Terminal on Windows 11, which opens Windows Terminal or PowerShell there.
  • Open first, navigate second: launch Command Prompt from the Start menu, then use the cd command shown in the next step.

Step 3: Navigate With cd if Needed

The prompt shows your current folder, something like C:\Users\YourName>. Change folder with cd followed by the path:

cd C:\Users\YourName\Projects

If the path contains spaces, wrap it in quotes: cd "C:\Users\YourName\My Projects". Useful companions: cd .. moves up one level, dir lists the files in the current folder so you can confirm your script is really there, and typing the first letters of a name then pressing Tab lets the terminal complete it for you. The official Python guide to using Python on Windows describes the launcher and command options in full.

Step 4: Run the File

  1. Type py hello.py, using your file’s real name, and press Enter.
  2. If py is not available, try python hello.py. On a correctly set-up machine both work; py is the launcher installed with Python from python.org and is the most reliable on Windows.
  3. Watch the output appear in the same window. When the program ends, the prompt returns, and the window stays open so you can read everything, including full error tracebacks.
  4. To run the script again, press the Up arrow key to recall the command and press Enter. This tiny habit will save you thousands of keystrokes.

Step 5: Pass Arguments and Stop a Runaway Script

Many scripts accept extra information after the file name, called arguments: py report.py march.csv runs report.py and hands it the text march.csv, which the program reads from sys.argv. If a script ever runs too long or loops forever, click the terminal and press Ctrl+C to interrupt it; our guide on stopping an infinite loop safely explains the details. If your script asks the user questions with input(), simply type your answers in the terminal when prompted.

Troubleshooting the Usual First-Run Problems

  • “Python was not found” or the Microsoft Store opens: Python is not installed properly or the Store alias is intercepting. Follow the repair steps in our guide to the “Python was not found” error, which also applies outside VS Code.
  • “can’t open file … No such file or directory”: the terminal is in the wrong folder or the name is misspelled. Run dir to see the real names in front of you.
  • “‘py’ is not recognized”: the launcher was not installed, usually because Python came from the Microsoft Store. Reinstall from python.org, or use the full path to python.exe as a temporary workaround.
  • The window closes the instant the script ends: you double-clicked instead of using a terminal. That behaviour is expected for double-clicks; running from an open terminal, as in this guide, is the cure.

Common Mistakes to Avoid

  • Typing commands into the Python interpreter. If your prompt shows >>>, you are inside Python, and commands like cd or py file.py will fail. Type exit() to leave, then run your command at the normal prompt.
  • Forgetting quotes around paths with spaces. cd My Projects is read as two separate arguments. Quote the whole path.
  • Running the wrong file among similar names. Use dir and Tab completion to be certain, especially with names like test.py and test2.py.

Knowing Whether the Script Actually Succeeded

When a script finishes, the prompt returns with no verdict — so how do you tell success from silent failure? First, read the output: a traceback, even a short one, means the program ended early with an error, and the last lines of the traceback name the problem. Clean output ending with your program’s own final message usually means a clean finish. Second, Windows keeps a record you can ask about: immediately after running a program, type echo %ERRORLEVEL% in Command Prompt. A value of 0 means the program reported success; any other number, most often 1, means it ended in error, whether or not text was printed. Scripts you write can control this deliberately with sys.exit(), but for now the habit matters more than the mechanism: after each run, glance for a traceback and, when in doubt, check the error level. Within a week this two-second check becomes automatic, and “it ran” will always mean “it ran and I verified it.”

Conclusion

The reliable routine is always the same: open a terminal in the script’s folder, confirm the file is there with dir, run it with py filename.py, and read the output in the window that stays open. Master these few commands — cd, dir, py, and Ctrl+C — and you have the foundation for every Python workflow that follows.

Frequently Asked Questions

What is the difference between py and python on Windows?

python runs the interpreter that your PATH finds first. py is the Python Launcher, installed with the python.org version, which locates installed Pythons itself and even lets you pick a version, for example py -3.11 script.py. When both exist, py is the more predictable choice on Windows.

How do I run a Python file that is on my Desktop?

The Desktop is a folder like any other: cd %USERPROFILE%\Desktop takes you there in Command Prompt. Or open the Desktop in Explorer and use the address bar trick to open the terminal in place, then run the file by name.

Why does the terminal say the file is not found when I can see it?

Almost always a hidden extension or a spelling mismatch. With extensions visible you may discover the file is really named script.py.txt. Rename it in Explorer so it ends in a single .py, or type the full real name in the terminal.

Can I run a Python file by giving its full path?

Yes: py "C:\Users\YourName\Projects\hello.py" works from any folder. One caution: the script’s “current folder” for opening its own data files is still the terminal’s folder, not the script’s folder, which surprises many beginners when relative file paths fail.

How do I run a script with a specific Python version?

Use the launcher’s version flag: py -3.12 script.py or py -3.11 script.py, provided those versions are installed. To see every version the launcher can find, run py --list. This is handy when a project needs an older Python than your default.

Admin

Passionate about the intersection of style and storytelling. I simplify fashion trends and industry news into easy-to-follow advice, helping you stay stylish and informed in the fast-paced world of fashion.

Leave a Reply

Your email address will not be published. Required fields are marked *