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.
Part 1 of 3: Find the path to your Python file
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
Step 2. Select the Python file
Click once on the Python file you want to find the path to.
Step 3. Right click on the Python file
A dropdown menu will appear.
Step 4. Select Properties from the drop-down menu
The properties window will open.
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
Step 1. Open the Start Menu
Click on the Windows logo in the lower left corner of the screen. The Start menu appears.
Step 2. Find Command Prompt by entering cmd
Step 3. Click on
Command Prompt at the top of the Start menu to open Command Prompt.
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.
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").
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
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
Click on the tab View.
- Check the box next to Hidden Items.
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").
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.
Step 4. Open the context menu
To do this, right-click on the "Start" icon
… Then a pop-up menu will appear.
You can also open the pop-up context menu by pressing ⊞ Win + X
Step 5. Click on System in the pop-up menu
A new window will open.
Step 6. Click on System Information
It's a link in the upper right corner of the window. The "System" window will open.
Step 7. Click on the link Advanced system settings in the upper left part of the "System" window
Another window will appear.
Step 8. Click on Environment Variables in the lower right corner of the pop-up window
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
Step 10. Double click on the "Path" heading
A pop-up window will open.
Step 11. Click on Create in the right part of the window
A text box will open in the middle of the window.
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.
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.