This article was co-authored by Kevin Burnett and by wikiHow staff writer, Nicole Levine, MFA. Kevin Burnett is a Software Developer with over 20 years of professional experience. He works for SpiffWorkflow, a Python Process Automation tool for organizations. Prior to SpiffWorkflow, he spent the majority of his career at Rosetta Stone, a language-learning software company. He has experience with both front and back-end development and works primarily in Python, Ruby, and JavaScript. He attended the first RailsConf and the most recent PyCon US. He received an MBA and a BA in Computer Science and Spanish from Eastern Mennonite University.
This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 1,007,358 times.
Whether you're writing Python code on your Windows PC or just want to use existing Python scripts, it'll be helpful to learn how to run code from the Command Prompt. Running Python code is easy—you'll just need to have Python installed. This wikiHow article will walk you through opening a Python file from Command Prompt and teach you how to fix the common "python is not recognized as an internal or external command" or "python was not found" errors.
How do I run a Python file in CMD?
To run a Python file, type "python File.py", where "File" is your file’s name. For example, if your Python file is named "script.py", type "python script.py". If you see "Python is not found" or a similar error, you need to add the Python folder to your system path.
Steps
Running a Python File in CMD
-
Find the path of your Python file. To run a Python file at the Windows command prompt, you'll need to know how to navigate to the folder where the file is stored. Here's an easy way to find the full path so you don't have to do a lot of trial and error at the prompt:
- Open File Explorer.
- Open the folder containing the Python file.
- Right-click the file and select Properties.
- Copy the full path next to "Location" by highlighting it with your mouse and pressing Ctrl + C.
- For example, C:\Users\wikiHow\Documents\python\scripts.
-
Open the Command Prompt. You can do this easily by pressing the Windows key, typing cmd, and clicking Command Prompt. It's at the top of the Start menu. Doing so will open Command Prompt.Advertisement
-
Switch to your Python file's directory. Type cd and a space, followed by the full path to the Python script you want to run. If you copied it to your clipboard, press Ctrl + V to paste it. t.
- For example, cd C:\Users\wikiHow\Documents\python\scripts.
-
Type the word python, followed by the name of the Python script. For example, if the script is called script.py, type python script.py, then press Enter to run it.[1]
- If your Python file has one or more spaces in its name, you'll place quotation marks around the file name and extension (e.g., python "my script.py").
- If you encounter an error that says 'python' is not recognized as an internal or external command after pressing Enter, you'll need to add Python to the PATH before retrying this part.
Adding Python to the PATH
-
Enable viewing for hidden folders in File Explorer. Since one of the folders that contains your Python installation folder is most likely hidden, you'll have to unhide hidden folders before proceeding:[2]
- Open File Explorer .
- Click the View tab.
- On Windows 11, select Show > Hidden items.
- On Windows 10, check the "Hidden items" box.
-
Copy the path to your Python folder. The location of your Python installation depends on how you installed it. By default, it will be in C:\Users\yourusername\AppData\Local\Programs\Python\Python38. "Yourusername" will be your actual username, and the digits at the end of "Python" will likely be different. You can copy the proper file path by doing the following:
- In File Explorer, click This PC.
- Double-click your hard drive in the "Devices and drives" section.
- Scroll down and double-click the Users folder.
- Double-click your username folder.
- Double-click AppData.
- Double-click Local.
- Double-click Programs.
- Double-click the Python folder.
- Double-click the Python folder ending with a number (e.g., "Python36").
- Click the address at the top of File Explorer to highlight its contents.
- Press Ctrl + C to copy the highlighted address.
-
Open the System Properties window. Here's how:
- Press the Windows key on your keyboard (or open the Start menu).
- Type sysdm.cpl.
- Press Enter.
-
Click the Advanced tab. It's at the top of the window.
-
Click Environment Variables…. It's in the bottom-right corner of the pop-up window.
-
Double-click the "Path" heading under "User variables". Doing so opens a pop-up window.
- You may have to scroll up or down with your mouse cursor hovering over the "User variables" pane to find the "Path" variable.
-
Click New. It's on the right side of the window. A text field will open in the middle of the window.
-
Paste in your copied path. Press Ctrl + V to do so. Your copied path will appear in the text field in the middle of the window.
-
Click OK on the three open windows. This will save your changes and close the "Path" window, the "Environmental Variables" window, and the "System Properties" window.[3]
-
Restart Windows. Once your computer comes back up, you will be able to run Python programs at the command prompt by typing "python <filename>".
Community Q&A
-
QuestionI want to create a shortcut that executes the utility "ptpython," running in the cmd prompt. The shortcut I have points to the directory containing "ptpython.exe" file but it does not execute it.
Community AnswerIt sounds like ptpython.exe is a command-line utility, meaning it will only start if you execute it from a DOS window - you can't create a shortcut for it directly. You can probably create a shortcut to cmd.exe, though (the DOS window) and pass it the ptpython.exe file as a parameter. Something like "cmd.exe /c ptpython.exe" should work, or if this disappears in the end, try with /k (instead of /c). -
QuestionDoes this work on Windows 7?
ArroganceTop AnswererYes. The directions to access the environment variables would be slightly different, as there is no "Power User" menu in Windows 7. Instead: 1. Press the Windows key and R to open the Run dialog. 2. Enter "sysdm.cpl". 3. Click the "Advanced" tab of the System Properties Window. 4. Click the "Environmental variables". Most everything else would work as described even on Windows 95 (if there's a version of Python for Windows 95). -
QuestionAfter opening the Command Prompt and navigation to the directory in which the py file exists and opening Python, not able to run the file using python file_name.py. It says that the syntax is wrong.
ArroganceTop AnswererThat sounds like a problem with the file you're trying to run. Make sure you are using the right version of Python for it (version 2 or 3, usually).
Video
Tips
Expert Interview
Thanks for reading our article! If you’d like to learn more about programming languages, check out our in-depth interview with Kevin Burnett.
















