How to run Python file using Windows Command Prompt

Table of contents:

How to run Python file using Windows Command Prompt
How to run Python file using Windows Command Prompt
Anonim

This article will show you how to open a Python file using the built-in Command Prompt on a Windows computer. In most cases, this can be done without any problems - as long as you have Python installed on your computer. If you have installed an older version of Python or used custom settings during installation that prevented the "python" command from being added to the "Path" variable list on the computer, you will have to add Python to the "Path" variable list to be able to run the file. Python via command line.

Steps

Part 1 of 3: Find the path to your Python file

Use Windows Command Prompt to Run a Python File Step 1
Use Windows Command Prompt to Run a Python File Step 1

Step 1. Navigate to the folder with the Python file

Find the Python file you want to open on the Command Prompt.

If you already know the path to the Python file you want to open, skip to the section on opening a file on the Command Line

Use Windows Command Prompt to Run a Python File Step 2
Use Windows Command Prompt to Run a Python File Step 2

Step 2. Select the Python file

Click once on the Python file you want to find the path to.

Use Windows Command Prompt to Run a Python File Step 3
Use Windows Command Prompt to Run a Python File Step 3

Step 3. Right click on the Python file

A dropdown menu will appear.

Use Windows Command Prompt to Run a Python File Step 4
Use Windows Command Prompt to Run a Python File Step 4

Step 4. Select Properties from the drop-down menu

The properties window will open.

Use Windows Command Prompt to Run a Python File Step 5
Use Windows Command Prompt to Run a Python File Step 5

Step 5. Pay attention to the value in the "Location" line

The folder address (or "path") to the right of Location is exactly what you need to enter on the Command Prompt to navigate to the directory where the Python file is stored.

To copy a location, select it (hold down and drag the mouse pointer over the value in the “Location” line), and then press Ctrl + C

Part 2 of 3: Run the Python File

Use Windows Command Prompt to Run a Python File Step 6
Use Windows Command Prompt to Run a Python File Step 6

Step 1. Open the Start Menu

Windowsstart
Windowsstart

Click on the Windows logo in the lower left corner of the screen. The Start menu appears.

Use Windows Command Prompt to Run a Python File Step 7
Use Windows Command Prompt to Run a Python File Step 7

Step 2. Find Command Prompt by entering cmd

Use Windows Command Prompt to Run a Python File Step 8
Use Windows Command Prompt to Run a Python File Step 8

Step 3. Click on

Windowscmd1
Windowscmd1

Command Prompt at the top of the Start menu to open Command Prompt.

Use Windows Command Prompt to Run a Python File Step 9
Use Windows Command Prompt to Run a Python File Step 9

Step 4. Browse to the location of the Python file

Type cd and press the spacebar, then type in the Location for the Python file and press ↵ Enter.

  • For example, to open a Python file in a folder named Files on your desktop, you would type cd desktop / Files.
  • If you copied the path to the file, enter cd and press the spacebar, then press Ctrl + V to paste the path.
Use Windows Command Prompt to Run a Python File Step 10
Use Windows Command Prompt to Run a Python File Step 10

Step 5. Enter the "python" command and the filename

Enter python file.py where file is the name of the Python file.

  • For example, if the Python file is named “script,” enter python script.py.
  • If the Python file name contains one or more spaces, surround the file name and extension with quotation marks (for example, python "my script.py").
Use Windows Command Prompt to Run a Python File Step 11
Use Windows Command Prompt to Run a Python File Step 11

Step 6. Press ↵ Enter to run the command and open the Python file through the Python program installed on your computer

  • If after pressing the Enter key you encounter an error stating that

    'python' is not recognized as an internal or external command

    , you will need to add Python to your PATH before returning to this part.

Part 3 of 3: Add Python to PATH

Use Windows Command Prompt to Run a Python File Step 12
Use Windows Command Prompt to Run a Python File Step 12

Step 1. Turn on the display of hidden folders

Since one of the folders that contains the Python installation directory is likely hidden, you'll have to display it before continuing:

  • Open File Explorer
  • File_Explorer_Icon
    File_Explorer_Icon
  • Click on the tab View.

  • Check the box next to Hidden Items.
Use Windows Command Prompt to Run a Python File Step 13
Use Windows Command Prompt to Run a Python File Step 13

Step 2. Go to the folder where Python is installed

Python is sometimes placed in the "C: \ Python27" folder, but if you have installed the most recent version of Python using the default settings, the program will be hidden in a hidden folder. Copy the correct file path by following these steps:

  • Press This computer on the left side of File Explorer.
  • Double click on your hard drive under Devices and Drives.
  • Scroll down and double-click the Users folder.
  • Double click on the folder with your username.
  • Scroll down and double click on the "AppData" folder.
  • Double click on the "Local" folder.
  • Scroll down and double click on the Programs folder.
  • Double click on the "Python" folder.
  • Double click on the version number "Python" folder (for example, "Python36").
Use Windows Command Prompt to Run a Python File Step 14
Use Windows Command Prompt to Run a Python File Step 14

Step 3. Copy the path to the Python folder

Click on the address bar at the top of File Explorer to highlight its contents, and then press Ctrl + C to copy the highlighted address.

Use Windows Command Prompt to Run a Python File Step 15
Use Windows Command Prompt to Run a Python File Step 15

Step 4. Open the context menu

To do this, right-click on the "Start" icon

Windowsstart
Windowsstart

… Then a pop-up menu will appear.

You can also open the pop-up context menu by pressing ⊞ Win + X

Use Windows Command Prompt to Run a Python File Step 16
Use Windows Command Prompt to Run a Python File Step 16

Step 5. Click on System in the pop-up menu

A new window will open.

Use Windows Command Prompt to Run a Python File Step 17
Use Windows Command Prompt to Run a Python File Step 17

Step 6. Click on System Information

It's a link in the upper right corner of the window. The "System" window will open.

Use Windows Command Prompt to Run a Python File Step 18
Use Windows Command Prompt to Run a Python File Step 18

Step 7. Click on the link Advanced system settings in the upper left part of the "System" window

Another window will appear.

Use Windows Command Prompt to Run a Python File Step 19
Use Windows Command Prompt to Run a Python File Step 19

Step 8. Click on Environment Variables in the lower right corner of the pop-up window

Use Windows Command Prompt to Run a Python File Step 20
Use Windows Command Prompt to Run a Python File Step 20

Step 9. Find the "Path" heading in the "User Environment Variables" panel

This window is at the top of the Environment Variables window.

You may have to scroll up or down above the User Environment Variables panel to find the Path variable

Use Windows Command Prompt to Run a Python File Step 21
Use Windows Command Prompt to Run a Python File Step 21

Step 10. Double click on the "Path" heading

A pop-up window will open.

Use Windows Command Prompt to Run a Python File Step 22
Use Windows Command Prompt to Run a Python File Step 22

Step 11. Click on Create in the right part of the window

A text box will open in the middle of the window.

Use Windows Command Prompt to Run a Python File Step 23
Use Windows Command Prompt to Run a Python File Step 23

Step 12. Paste the copied path

To do this, press Ctrl + V. The copied path will appear in the text box in the middle of the window.

Use Windows Command Prompt to Run a Python File Step 24
Use Windows Command Prompt to Run a Python File Step 24

Step 13. Click OK on the three open windows

This will save your changes and close the Path window, Environment Variables window, and System Properties window.

Popular by topic