How to Use Windows Command Prompt to Run a Python File
Part 1 of 3:
Finding the Python File Path
- Go to the Python file's location. Find the Python file that you want to open in Command Prompt.
- If you already know the folder path to the Python file you want to open, skip ahead to opening the file in Command Prompt.
- Select the Python file. Click once the Python file for which you want to see the folder path.
- Right-click the Python file. Doing so prompts a drop-down menu to appear.
- Click Properties. It's in the drop-down menu. The properties window will open.
- 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.
Part 2 of 3:
Running a Python File
- Open Start
- Search for Command Prompt. Type in cmd to do so.
- Click
- 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.
- 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").
- 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.
- If you encounter an error that says
Part 3 of 3:
Adding Python to the PATH List
- 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
- Click the View tab.
- Check the "Hidden items" box.
- Open File Explorer
- 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").
- 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.
- Open the Power User menu. Right-click the Start
- You can also press ⊞ Win+X to open the Power User pop-up menu.
- Click System. It's in the pop-up menu. A new window will open.
- Click System info. This is a link in the upper-right corner of the window. Doing so opens the System Information window.
- 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.
- Click Environment Variables…. It's in the bottom-right corner of the pop-up window.
- 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.
- Double-click the "Path" heading. Doing so opens a pop-up window.
- 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.
4 ★ | 1 Vote
You should read it
May be interested
- How to Kill a Process in Command Promptto kill processes on your computer, you would normally use the windows task manager. the windows task manager is a program designed to help you manage the processes running on your computer. although the task manager is sufficient for most...
- How to Remotely Restart a Windows Machine Through Command Linethis wikihow teaches you how to restart another computer on your network from your own computer by using the windows command prompt app. you cannot remotely restart a computer with command prompt if you haven't previously set the computer...
- How to Use Net Sendnet send is a command line tool used in windows xp for sending messages to other users and computers on a local network. with windows vista, net send was replaced with msg.exe, a command line tool that has very similar functionality and...
- How to Create and Delete Files and Directories from Windows Command Promptthis wikihow teaches you how to use the windows command prompt to create and delete files and folders (also known as directories). the files you create at the prompt can be modified and managed with windows apps, including file manager and...
- How to Decipher the Right Application to View an Unknown File Type on Windows Vistawith so many file extensions out there nowadays, its completely possible, that a few might not have a representable program existent on the 'open with' dialog box. if this is the case, this article will be for you. navigate to the proper...
- How to Optimize Windows Vistawindows vista is known for taking up a lot of memory, but here are a few ways to make it run faster. click the start orb and type regedit into the search box, then press enter. if you're prompted to enter the administrator password or...