How to Run a Python File in CMD: A Complete Guide
Running a Python file from the Windows Command Prompt is one of the first real skills every developer picks up — and it unlocks a lot of flexibility that clicking "Run" inside an IDE simply doesn't offer. Whether you're automating tasks, testing scripts, or deploying tools, understanding how CMD executes Python gives you genuine control over what's happening under the hood.
What Actually Happens When You Run Python From CMD
When you type a command into CMD and press Enter, Windows looks for an executable that matches what you typed. If Python is installed correctly and added to your system's PATH environment variable, CMD can find the Python interpreter and use it to execute your .py file.
The Python interpreter reads your script line by line, compiles it to bytecode internally, and executes it. You never see the intermediate steps — the result either runs successfully or throws an error in the terminal window.
Step-by-Step: Running a Python File in CMD 🖥️
1. Open Command Prompt
Press Windows + R, type cmd, and hit Enter. Alternatively, search "Command Prompt" in the Start menu.
2. Verify Python Is Installed
Type the following and press Enter:
python --version If Python is installed and on your PATH, you'll see something like Python 3.11.4. If you get an error like 'python' is not recognized, Python either isn't installed or isn't added to PATH.
3. Navigate to Your Script's Directory
Use the cd (change directory) command to move to the folder containing your .py file:
cd C:UsersYourNameProjects You can also check the contents of a folder with dir to confirm your file is there.
4. Run the Python File
Once you're in the right directory:
python your_script.py Replace your_script.py with your actual filename. The script runs immediately, and any output or errors print directly in the terminal.
Common Variations You'll Encounter
Using python3 Instead of python
On some Windows setups — especially if you've installed multiple Python versions — the command python may default to Python 2, or may not work at all. Try:
python3 your_script.py This explicitly calls the Python 3 interpreter.
Running a File Without Navigating to Its Directory
You can pass the full file path directly without using cd:
python C:UsersYourNameProjectsyour_script.py Useful when you're working across multiple folders in one session.
Passing Arguments to Your Script
If your Python script is built to accept command-line arguments, you pass them after the filename:
python your_script.py argument1 argument2 Inside the script, these are accessed via the sys.argv list from the sys module.
Why PATH Matters So Much
PATH is a system variable that tells Windows where to look for executables. If Python's installation directory isn't included in PATH, CMD has no idea where the Python interpreter lives.
During Python installation on Windows, there's a checkbox that reads "Add Python to PATH" — checking this at install time handles everything automatically. If you missed it, you can add Python to PATH manually through System Properties → Environment Variables, or by reinstalling Python with that option enabled.
| Symptom | Likely Cause |
|---|---|
'python' is not recognized | Python not on PATH |
| Wrong Python version runs | Multiple installs, PATH order issue |
ModuleNotFoundError | Script depends on package not installed |
| Script runs but no output shows | Script may have errors or missing print() |
Variables That Affect Your Experience
Not everyone's setup produces the same result when running Python from CMD, and a few key factors determine what works smoothly versus what needs troubleshooting:
Python version installed — Python 2 and Python 3 use different syntax. Scripts written for Python 3 will fail on a Python 2 interpreter and vice versa. Most modern scripts assume Python 3.
Single vs. multiple Python installations — Having both Python 2 and Python 3 installed, or multiple Python 3 versions alongside tools like Anaconda, can create conflicts where python resolves to an unexpected interpreter.
Virtual environments — If your project uses a virtual environment (a self-contained Python environment with its own packages), you need to activate it before running the script. Inside an active virtual environment, python points to that environment's interpreter, not the system-wide one.
.venvScriptsactivate python your_script.py Script dependencies — If your script imports third-party libraries (like requests, pandas, or numpy), those libraries must be installed in the Python environment you're using. Running the script against a different environment — one that lacks those packages — will throw import errors even if the command syntax is perfect. 🔧
Working directory context — Some scripts reference files using relative paths, meaning they assume the script is run from a specific directory. Running the same script from a different location in CMD can cause file-not-found errors that have nothing to do with Python itself.
The Difference Between Running in CMD vs. an IDE
IDEs like VS Code, PyCharm, or IDLE manage a lot of this automatically — they set working directories, detect your interpreter, and handle virtual environments behind the scenes. CMD gives you none of that automation, which is why understanding these variables matters more when working directly in the terminal.
Running from CMD is often closer to how scripts run in production environments, on servers, or through scheduled tasks. That fidelity is exactly why developers learn it — but it also means your local setup, your PATH configuration, and your environment choices all factor into whether a script runs exactly as expected.