To run Python from the Windows Command Prompt, you must first ensure the Python executable is added to your system's PATH environment variable. Once configured, you can execute scripts directly by typing
python script.py in any terminal window. This setup allows for seamless integration of Python into batch workflows and automated tasks without needing to navigate to specific installation directories every time.
Verifying Python Installation
Before attempting to run scripts, confirm that Python is installed on your machine and accessible via the command line. Open a new Command Prompt window by pressing
Win + R, typing
cmd, and hitting Enter. Type
python --version or
py --version and press Enter. If Python is installed and properly configured, the terminal will display the version number, such as
Python 3.10.5. If you receive an error stating that the command is not recognized, it indicates that Python is either not installed or its location is not included in your system's PATH. In many cases, simply reinstalling Python and ensuring the "Add Python to PATH" checkbox is selected during installation resolves this issue immediately.
Configuring the Environment Path
The most common reason users cannot run Python from CMD is that the directory containing
python.exe is missing from the System PATH. To fix this, open the Start menu, search for "Environment Variables," and select "Edit the system environment variables." Click the "Environment Variables..." button at the bottom of the System Properties window. Under the "System variables" section, find the variable named
Path, select it, and click "Edit." Click "New" and add the path to your Python installation folder, typically something like
C:\Users\YourUsername\AppData\Local\Programs\Python\Python310. You should also add the
Scripts subdirectory (
...\Python310\Scripts) if you plan to use pip packages that install command-line tools. After adding these paths, click OK on all dialogs to save changes. It is crucial to open a new Command Prompt window after making these changes, as existing windows will not reflect the updated PATH until restarted.
Executing Python Scripts Directly
Once the PATH is correctly configured, running a Python script is straightforward. Navigate to the directory containing your script using the
cd command. For example, if your script is on the Desktop, you might type
cd C:\Users\YourUsername\Desktop. Then, simply type
python your_script.py and press Enter. The Command Prompt will execute the script and display any output or errors generated by the code. If you have multiple versions of Python installed, you can specify the exact version using the
py launcher, such as
py -3.9 script.py. This method ensures that the correct interpreter is used for your specific project requirements.
Running Python with Batch Files
For more complex automation, you may want to wrap Python execution within a batch file (.bat). Create a new text file, rename it to
run_python.bat, and open it in a text editor. Inside the file, you can include commands to change directories, set environment variables, and call the Python interpreter. A typical batch file might look like this:
@echo off
cd /d "C:\Path\To\Your\Script"
python main.py
pause
The
@echo off command prevents the commands themselves from being displayed in the console, providing a cleaner output. The
cd /d command changes the drive and directory, which is necessary if your script resides on a different drive than your current working directory. The
pause command keeps the window open after the script finishes, allowing you to read any error messages before the window closes automatically. This approach is particularly useful for scheduling tasks or creating shortcuts for non-technical users who need to trigger Python processes.
Troubleshooting Common Issues
If you encounter issues while running Python from CMD, consider the following troubleshooting steps. First, verify that the file extension association is correct. Sometimes, Windows may associate
.py files with a text editor instead of the Python interpreter. You can check this by right-clicking a Python file, selecting "Open with," and choosing Python. Second, ensure there are no spaces in the file path that are not properly quoted. If your script path contains spaces, enclose it in quotes:
python "C:\My Scripts\script.py". Third, check for permission issues. If you are trying to write to a directory outside your user profile, you may need to run the Command Prompt as an administrator. Finally, if you recently changed environment variables but still see errors, restart your computer to ensure all system services reload the updated configuration. For related issues with variable handling in CMD, you might find
Windows CMD Variable Not Set on First Run helpful.
Frequently Asked Questions
Why does my CMD say 'python' is not recognized?
This error almost always means Python is not in your system's PATH environment variable. You need to add the Python installation directory to the Path variable in your system settings and restart your terminal session.
Can I run Python scripts without opening CMD manually?
Yes, you can create a shortcut on your desktop that points to the Python executable and includes the script path as an argument. Alternatively, you can double-click a .py file if the file association is correctly set to open with Python.
How do I specify a virtual environment in CMD?
Activate your virtual environment by running the activation script located in the
Scripts folder of your virtual environment directory. Once activated, any
python command will use the isolated environment's packages and interpreter.
What is the difference between python and py commands?
python calls the executable directly found in your PATH, while
py invokes the Python Launcher for Windows, which helps manage multiple installed versions. The launcher allows you to easily switch between versions using flags like
-3.8 or
-3.10.
How can I pass arguments to a Python script from CMD?
Simply append the arguments after the script name in the command line. For example,
python script.py arg1 arg2. Your Python script can then access these values using the
sys.argv list or a library like
argparse.