How to Use Windows Command Prompt to Run a Python File

Download Article
A quick guide to operating Python scripts in command prompt
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 How.com.vn 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" error.

Easy Way to Run a Python Script

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,” type “Python script.py” instead. Press enter to run the command and open the file.

Part 1
Part 1 of 3:

Finding the Python File Path

Download Article
  1. How.com.vn English: Step 1 Go to the Python file's location.
    Find the Python file that you want to open in Command Prompt.
  2. How.com.vn English: Step 2 Select the Python file.
    Click once the Python file for which you want to see the folder path.
    Advertisement
  3. How.com.vn English: Step 3 Right-click the Python file.
    Doing so prompts a drop-down menu to appear.
  4. How.com.vn English: Step 4 Click Properties.
    It's in the drop-down menu. The properties window will open.
  5. Step 5 Note the "Location" value.
    The folder address (or "path") to the right of the "Location" heading is what you'll need to enter into Command Prompt when switching to the directory in which your Python file is stored.
    • You can copy the location by highlighting it (click and drag your mouse across the "Location" value) and then pressing Ctrl+C.
  6. Advertisement
Part 2
Part 2 of 3:

Running a Python File

Download Article
  1. How.com.vn English: Step 1 Open Start icon.
    Click the Windows logo in the bottom-left corner of the screen. The Start menu will pop up.
  2. How.com.vn English: Step 2 Search for Command Prompt.
    Type in cmd to do so.
  3. How.com.vn English: Step 3 Click icon Command Prompt.
    It's at the top of the Start menu. Doing so will open Command Prompt.
  4. How.com.vn English: Step 4 Switch to your Python file's directory.
    Type cd and a space, then type in the "Location" address for your Python file and press Enter.
    • For example, to open a Python file in a folder named "Files" on your Desktop, you would enter cd desktop/Files here.
    • If you copied the path to the file, you can type in cd and a space and then press Ctrl+V to paste in the path.
  5. How.com.vn English: Step 5 Enter the "python" command and your file's name.
    Type in python file.py where file is your Python file's name.
    • For example, if your Python file is named "script", you would type in python script.py here.
    • 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").
  6. How.com.vn English: Step 6 Press ↵ Enter.
    Doing so runs your command and opens your Python file via your computer's installed Python program.
    • 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 list before retrying this part.
  7. Advertisement
Part 3
Part 3 of 3:

Adding Python to the PATH List

Download Article
  1. How.com.vn English: Step 1 Enable viewing for...
    Enable viewing for hidden folders. Since one of the folders that contains your Python installation folder is most likely hidden, you'll have to unhide it before proceeding:
    • Open File Explorer
      How.com.vn English: Windows File Explorer
      .
    • Click the View tab.
    • Check the "Hidden items" box.
  2. How.com.vn English: Step 2 Navigate to your Python folder.
    In some cases, the Python path is "C:\Python27"; however, if you've installed the most recent version of Python using the default settings, it's tucked away in a hidden folder. You can copy the proper file path by doing the following:
    • Click This PC on the left side of the File Explorer.
    • Double-click your hard drive in the "Devices and drives" section.
    • Scroll down and double-click the "Users" folder.
    • Double-click the folder with your username on it.
    • Scroll down and double-click "AppData".
    • Double-click "Local".
    • Scroll down and double-click "Programs".
    • Double-click the "Python" folder.
    • Double-click the Python folder with your preferred version number (e.g., "Python36").
  3. How.com.vn English: Step 3 Copy the path to the Python folder.
    Click once the address bar at the top of the File Explorer to highlight its contents, then press Ctrl+C to copy the highlighted address.
  4. How.com.vn English: Step 4 Open the Power User menu.
    Right-click the Start
    How.com.vn English: Windows Start
    icon to do so. You should see a pop-up menu appear.
    • You can also press Win+X to open the Power User pop-up menu.
  5. How.com.vn English: Step 5 Click System.
    It's in the pop-up menu. A new window will open.
  6. How.com.vn English: Step 6 Click System info.
    This is a link in the upper-right corner of the window. Doing so opens the System Information window.
  7. How.com.vn English: Step 7 Click the Advanced system settings link.
    You'll see this in the upper-left side of the System Information window. Yet another window will pop up.
  8. How.com.vn English: Step 8 Click Environment Variables….
    It's in the bottom-right corner of the pop-up window.
  9. Step 9 Find the "Path" heading in the "User variables" pane.
    This window is at the top of the Environment Variables window.
    • You may have to scroll up or down with your mouse cursor hovering over the "User variables" pane to find the "Path" variable.
  10. Step 10 Double-click the "Path" heading.
    Doing so opens a pop-up window.
  11. How.com.vn English: Step 11 Click New.
    It's on the right side of the window. A text field will open in the middle of the window.
  12. How.com.vn English: Step 12 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.
  13. How.com.vn English: Step 13 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.
  14. 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.
    How.com.vn English: 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?
    How.com.vn English: 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.
    How.com.vn English: 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

      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.

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      How.com.vn English: Kevin Burnett
      Written by:
      Software Developer
      This article was written by Kevin Burnett and by How.com.vn staff writer, Jack Lloyd. Kevin Burnett is a Software Developer with over 20 years of professional experience. 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 Ruby, Python, and JavaScript. This article has been viewed 882,636 times.
      How helpful is this?
      Co-authors: 14
      Updated: March 22, 2024
      Views: 882,636
      Categories: Python | Command Prompt
      Thanks to all authors for creating a page that has been read 882,636 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement