Master How to Run Python in CMD: A Step-by-Step Guide
Running Python from the command line is a fundamental skill that unlocks the true power of the language. While Integrated Development Environments (IDEs) offer convenience, the Command Line Interface (CLI) provides direct control, allows for automation through scripting, and is essential for working on remote servers or deploying applications. Understanding how to execute your scripts and interact with the Python interpreter directly via CMD or Terminal is a critical step for any developer.
Verifying Your Python Installation
Before you can run Python commands, you need to ensure your system is correctly configured. The first step is to check if Python is installed and accessible from your command prompt. This process verifies that the Python executable is in your system's PATH, allowing the CMD to locate it.
Checking the Python Version
To confirm your installation, open your command prompt and type a simple command to query the version. This not only confirms the presence of Python but also tells you which major version you are working with, which is important for compatibility.
| Operating System | Command | Purpose |
|---|---|---|
| Windows (CMD/PowerShell) | python --version or py --version | Displays the installed Python version. |
| macOS/Linux (Terminal) | python3 --version | Displays the installed Python 3 version. |
If the command returns a version number (such as Python 3.9.1), your installation is successful. If you encounter an error like "'python' is not recognized," you will need to reinstall Python and ensure the option "Add Python to PATH" is checked during the setup process.
Executing Python Scripts
The most common task is running a Python file containing your code. This is usually done with a `.py` extension. You need to navigate to the directory containing your script and instruct the interpreter to execute it.
Navigating the File System
Use standard directory commands to move to the folder where your script is located. cd (Change Directory) is your primary tool for this. You must ensure you are in the correct folder before attempting to run the file.
Running the File
Once in the correct directory, type python followed by the space and the name of your script. Press enter, and the interpreter will process the lines of code sequentially.
python my_script.pypython data_processor.py
The output of your script will appear directly in the terminal window immediately following execution.
Interactive Mode: Talking to Python
Beyond running files, CMD allows you to enter an interactive environment, often called the REPL (Read-EEval-Print Loop). This mode is excellent for testing small snippets of code, experimenting with libraries, or debugging logic in real-time without creating a new file.
Entering the REPL
Simply typing python or py into the command prompt without specifying a script file will launch the interactive mode. You will see a prompt (usually >>>) indicating that Python is ready to accept your commands.
Exiting the REPL
To return to the standard command line, you need to exit the interpreter. The most reliable method is to call the built-in exit() function or press Ctrl+Z followed by Enter on Windows.
>>> print(2 + 3)5>>> exit()
Utilizing Command Line Arguments
Powerful scripts often need to receive data at runtime. Python provides access to command line arguments through the sys module, allowing your programs to be dynamic and flexible based on user input.
Importing the Module
To handle arguments, you must first import the sys module. The sys.argv list object captures the command line inputs, where the first item is the script name and subsequent items are the arguments you pass.
A Practical Example
Imagine a script named greet.py designed to personalize a message. You can run python greet.py Alice and the code inside the script can read that "Alice" was passed and print "Hello, Alice!".
import sys
print("Hello, " + sys.argv[1])
Troubleshooting Common Issues
Encountering errors is a normal part of the learning process. The two most frequent issues involve path configuration and syntax mistakes. Knowing how to diagnose these problems saves significant time.
The 'Not Recognized' Error
If CMD responds with "'python' is not recognized as the name of a cmdlet," it means the system cannot find the Python executable. The solution is to reinstall Python and carefully check the "Add Python to PATH" checkbox on the initial installation screen.
Syntax and Logical Errors
If your script stops with a SyntaxError, the terminal will usually point to the line number where the issue occurred. Carefully check for missing colons, unmatched parentheses, or incorrect indentation. The command line does not hide these errors; it reports them clearly so you can fix them.