The simplest way to run a Python file from Command Prompt
Open Command Prompt, navigate to the folder where your Python file lives, and type python filename.py — then press Enter. That's the core of it. The computer finds your Python installation, reads the file, and runs the code inside it.
The catch is that this only works if Python is installed on your computer and Command Prompt knows where to find it. If you type the command and get an error saying "python is not recognized", Python either isn't installed or Command Prompt doesn't know the path to it. Both problems have straightforward fixes.
Key Takeaways
- Python must be installed on your computer, and you can read it free from python.org.
- When you install Python on Windows, check the box that says "Add Python to PATH" — this lets Command Prompt find it automatically.
- Navigate to your file's folder in Command Prompt using the cd command, then type python filename.py to run it.
- If Command Prompt says "python is not recognized", either Python isn't installed or the PATH wasn't set during installation — reinstalling with the PATH option checked fixes this.
- You can also run a Python file by typing its full path: python C:\Users\YourName\Documents\filename.py, without changing folders first.
Installing Python so Command Prompt can find it
Go to python.org, click the Downloads button, and read the latest version for Windows. Run the installer. On the first screen, you will see two checkboxes at the bottom — one says "Install launcher for all users" and one says "Add Python 3.x to PATH". Check both of these boxes. This second one is the critical step: it tells Windows where Python lives, so Command Prompt can find it without you typing the full path every time.
Click "Install Now" and let it finish. Once it's done, close the installer and open a new Command Prompt window. Type python --version and press Enter. If you see a version number (like "Python 3.12.1"), Python is installed and Command Prompt can find it. If you still see "python is not recognized", close Command Prompt completely and open a fresh one — Windows sometimes needs a restart of the terminal to pick up the new PATH.
Navigating to your file's folder in Command Prompt
Command Prompt always starts in a default folder, usually something like C:\Users\YourName. To run a Python file, you need to be in the same folder where that file lives. Use the cd command (short for "change directory") to move around.
If your file is on your Desktop, type cd Desktop and press Enter. If it's in Documents, type cd Documents. If it's nested deeper — say, in a folder called "my-projects" inside Documents — type cd Documents\my-projects. After each command, Command Prompt shows you where you are. Once you're in the right folder, type python filename.py (replacing "filename" with your actual file name) and press Enter.
If you get lost, type cd .. to go up one level, or type cd C:\ to jump to the root of your hard drive. Type dir to see all the files and folders in your current location — this helps you confirm you're in the right place before running the Python file.
Running a Python file without changing folders
If you don't want to navigate using cd, you can tell Python exactly where the file is by typing the full path. For example: python C:\Users\YourName\Documents\my-projects\filename.py. Command Prompt will run the file no matter what folder you're currently in.
To find the full path, open File Explorer, right-click on your Python file, and select "Properties". Look for the "Location" field — that's the folder path. Copy it, then type python, a space, the path you copied, a backslash, and the filename. This method is slower to type but useful if you're running files from different locations and don't want to keep changing directories.
What to do when you see an error message
The most common error is "python is not recognized as an internal or external command". This means Command Prompt can't find Python. Either Python isn't installed, or the PATH wasn't set during installation. The fix: go back to python.org, read the installer again, and run it. This time, on the first screen, make absolutely sure you check "Add Python 3.x to PATH" before clicking Install. After it finishes, close Command Prompt completely and open a new one, then try python --version again.
If you see "No such file or directory" or "cannot find the file specified", you're in the wrong folder or you typed the filename wrong. Double-check the spelling of your file (including the .py extension) and use dir to confirm the file is in your current folder. If your file is named "hello.py" but you typed "python hello.txt", it won't work.
If your Python code runs but produces an error (like "NameError" or "SyntaxError"), that's a problem with the code itself, not with how you're running it. Those errors come from inside your Python file and need to be fixed in the code.
Using the full file path from File Explorer
Windows File Explorer has a built-in shortcut: hold Shift, right-click on your Python file, and select "Copy as path". This copies the full path to your clipboard. Then in Command Prompt, type python, a space, and paste (right-click or Ctrl+V). Press Enter and the file runs. This method eliminates typing mistakes and works even if your file is buried several folders deep.
This approach is especially useful if you're new to Command Prompt and navigating with cd feels confusing. It's slower than learning the navigation commands, but it's reliable and gets the job done.
Frequently Asked Questions
Do I need to install anything besides Python?
No. Python comes with everything you need to run .py files from Command Prompt. You don't need a separate code editor or IDE to run a file — Command Prompt and Python are enough. A code editor like Visual Studio Code makes writing and testing easier, but it's not required to run the file.
What if my Python file needs to read files or save output?
Make sure those files are in the same folder as your Python file, or give Python the full path to them inside your code. If your Python script tries to open a file called "data.txt" and that file isn't in the current folder, the script will fail. The safest approach is to keep your Python file and any data files it uses in the same folder.
Can I run a Python file by double-clicking it instead of using Command Prompt?
Yes, but it's not recommended for learning. Double-clicking runs the file, but the Command Prompt window closes when ready after, so you can't see the output or any error messages. Using Command Prompt directly keeps the window open so you can read what happened.
Why does my Python script run but produce no output?
Your script might be working fine — it just doesn't print anything. If your code doesn't include print() statements, you won't see any results. Add print("Hello") or similar to your code to confirm it's running, then run it again from Command Prompt.
Can I run Python files from Command Prompt on Mac or Linux?
Yes, the process is nearly identical. Install Python from python.org or your system's package manager, open Terminal (Mac) or a terminal emulator (Linux), navigate to your file's folder with cd, and type python3 filename.py. On Mac and Linux, you often use python3 instead of python to may support you're running Python 3.