PDF download Download Article
Easily execute Python scripts at the command line
PDF download Download Article

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.

Section 1 of 2:

Running a Python File in CMD

PDF download Download Article
  1. 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.
  2. 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
  3. 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.
  4. 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.
  5. Advertisement
Section 2 of 2:

Adding Python to the PATH

PDF download Download Article
  1. 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]
  2. 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.
  3. Here's how:
    • Press the Windows key on your keyboard (or open the Start menu).
    • Type sysdm.cpl.
    • Press Enter.
  4. It's at the top of the window.
  5. It's in the bottom-right corner of the pop-up window.
  6. 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.
  7. It's on the right side of the window. A text field will open in the middle of the window.
  8. Press Ctrl + V to do so. Your copied path will appear in the text field in the middle of the window.
  9. This will save your changes and close the "Path" window, the "Environmental Variables" window, and the "System Properties" window.[3]
  10. Once your computer comes back up, you will be able to run Python programs at the command prompt by typing "python <filename>".
  11. Advertisement

Community Q&A

Search
Add New Question
  • Question
    I 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 Answer
    Community Answer
    It 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).
  • Question
    Does this work on Windows 7?
    Arrogance
    Arrogance
    Top Answerer
    Yes. 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).
  • Question
    After 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.
    Arrogance
    Arrogance
    Top Answerer
    That 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).
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Video

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!

You Might Also Like

Install Python on WindowsInstall Python on Windows
Run a Batch File from the Command Line on Windows Run a Windows Batch File from the Command Line (CMD)
Open a Python FileOpen a Python File
Run a Program on Command Prompt Run a Program at the Windows Command Prompt: 2 Ways
Check Python Version on PC or Mac See the Python Version on Mac, Windows, and Linux
Run Visual Basic ScriptRun Visual Basic Script
Run a Program from the Command Line on LinuxRun a Program from the Command Line on Linux
Run an EXE File From Command PromptRun an EXE File From the Command Prompt on Windows 10 & 11
Open a Folder in Cmd Open a Folder in Command Prompt (CMD)
Run a BAT File on Windows3 Ways to Run a Batch (.BAT) file on Windows and More
Change the PATH Environment Variable on Windows Change the PATH Environment Variable on Windows
Open Terminal in Windows Open the Windows Terminal: A Quick Guide
Find a File's Path on Windows Find a File’s Path on Windows
Open the Command Prompt in Windows6 Simple Ways to Open the Command Line in Windows
Advertisement

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.

About This Article

Kevin Burnett
Co-authored by:
Software Developer
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 viewed 1,007,358 times.
How helpful is this?
Co-authors: 14
Updated: October 8, 2025
Views: 1,007,358
Categories: Python | Command Prompt
Thanks to all authors for creating a page that has been read 1,007,358 times.

Is this article up to date?

Advertisement