Table of Contents >> Show >> Hide
- Quick Start (If You’re in a Hurry)
- Step 1: Confirm Python Is Installed (and CMD Can See It)
- Step 2: Open Command Prompt the Easy Way
- Step 3: Navigate to Your Python File in CMD
- Step 4: Run the Python File from CMD
- Running the “Right” Python (Avoiding Version Mix-Ups)
- How to Run Python in a Virtual Environment (CMD-Friendly)
- Bonus: Running Python Modules (Not Just .py Files)
- Troubleshooting: Fix the Most Common CMD Errors
- Error: 'python' is not recognized as an internal or external command
- CMD opens the Microsoft Store when you type python
- Error: “No such file or directory” / Python can’t find your script
- Error: ModuleNotFoundError (but you “installed it already”)
- My script runs, then the window disappears
- How do I see which python CMD is using?
- Best Practices for Running Python Files in CMD
- Conclusion
- of Real-World “Experience” (aka Things People Learn the Hard Way)
The Windows Command Prompt (aka CMD) looks like it time-traveled here from 1997, but don’t let the retro vibe fool you:
it’s still one of the fastest ways to run a Python script, test a quick fix, or figure out why your code works in an editor but “mysteriously”
fails everywhere else. (Spoiler: it’s usually the path. Or the Python version. Or both. CMD is innocent. Probably.)
In this guide, you’ll learn how to run a Python file from CMD step-by-step, how to pass arguments, how to use virtual environments,
and how to troubleshoot the most common Windows-specific “why is my computer like this” momentswithout turning your system into a science experiment.
Quick Start (If You’re in a Hurry)
- Open CMD
- Go to your script’s folder with
cd - Run it with
py your_script.py(orpython your_script.py)
Example:
Step 1: Confirm Python Is Installed (and CMD Can See It)
Before running any Python file, make sure Python is actually installed and available from CMD.
On Windows, you’ll commonly see python and/or the py launcher. Either works, but py is often the smoothest
option on Windowsespecially if you ever install multiple Python versions.
Check your Python version
If one of these prints a version number (like Python 3.12.x), you’re good. If you see an error like:
'python' is not recognized... or Windows tries to open the Microsoft Store, don’t panicjump to the troubleshooting section below.
Pro tip: Use the Windows Python Launcher (py) when you can
The py launcher can automatically find installed Python versions and run the one you want. It’s especially helpful when your machine
has more than one Python installed (which happens more often than people admit in polite company).
List installed Python versions (handy for debugging):
Step 2: Open Command Prompt the Easy Way
Any of these methods work:
- Start Menu: Press the Windows key, type cmd, press Enter.
- Run dialog: Press
Win + R, typecmd, press Enter. - From a folder: In File Explorer, click the address bar, type
cmd, press Enter (opens CMD in that folder).
That last method is underrated. It saves you from typing long paths and reduces the risk of “Oops, I ran the script from the wrong folder again.”
Step 3: Navigate to Your Python File in CMD
In CMD, your script runs relative to your current directory. That means if your file isn’t in the folder CMD is currently “in,”
Python won’t find it unless you provide a full path.
Common commands you’ll use
dir– list files in the current foldercd– change directorycd ..– go up one foldercd– jump to the root of the current drive
Navigate to a folder on the same drive
Switch drives (important!)
If your project is on another drive, you can either switch drives first:
…or do it in one line using /d:
Paths with spaces need quotes
If you forget the quotes, CMD will interpret the path as multiple pieces and act confused. (To be fair, you did ask it to.)
Step 4: Run the Python File from CMD
Once you’re in the folder that contains your script, running it is straightforward.
Option A: Run with py (recommended on Windows)
Option B: Run with python
Example with a real file name
If hello.py prints something, congratsyou just ran a Python file from CMD like it’s your day job.
Run a script using its full path (from anywhere)
You don’t always have to cd into the folder. You can also run a script by providing the full path:
Pass arguments to your Python script
CMD arguments after the file name become values in sys.argv.
If your script doesn’t handle arguments yet, you can add a simple starter pattern:
Run it and you’ll see CMD passing your inputs into Python.
Running the “Right” Python (Avoiding Version Mix-Ups)
One of the most common Windows headaches is having multiple Python installationsmaybe one from python.org, one from the Microsoft Store,
and possibly one bundled with an app you didn’t even know was installing Python. The result? You run your script and it can’t find a package
you swear you installed yesterday.
Pick a specific Python version with py
Install packages for the same Python you run
Instead of typing pip install ... and hoping it targets the right interpreter, use:
That tells Python: “Run pip as a module using this interpreter.” It’s one of the cleanest ways to avoid “installed but not found.”
How to Run Python in a Virtual Environment (CMD-Friendly)
If you’re building anything more serious than a one-file experiment, a virtual environment (venv) is your best friend.
It keeps each project’s dependencies separate, so one project doesn’t wreck another. Think of it as a “bubble wrap layer” around your Python packages.
Create a virtual environment
From your project folder:
Activate the virtual environment in CMD
After activation, your prompt usually changes (you might see (.venv)). Now when you run python or py,
you’re using the virtual environment’s Python.
Install packages inside the venv
Run your file inside the venv
Deactivate when you’re done
Virtual environments are the difference between “my project works” and “my project worked until I sneezed near my keyboard.”
Bonus: Running Python Modules (Not Just .py Files)
Sometimes you don’t want to run a file directly. You want to run a module. That’s what -m is for.
It’s useful for built-in tools, package entry points, and consistent behavior across projects.
Start a quick local web server
Run pip reliably
Run a package module
If your script lives inside a package, -m often solves import issues that appear when you run a file “bare.”
Troubleshooting: Fix the Most Common CMD Errors
Error: ‘python’ is not recognized as an internal or external command
This usually means Python isn’t installed, or it’s installed but not added to your PATH (so CMD can’t find it).
Options that commonly fix it:
- Use
pyinstead ofpython(often works even when PATH is messy). - Re-run the Python installer and enable “Add Python to PATH.”
- Open a new CMD window after installation or PATH changes (old windows don’t always refresh).
CMD opens the Microsoft Store when you type python
This is typically caused by Windows “App execution aliases” pointing python.exe to the Store installer instead of your actual Python.
The fix is usually:
- Open Windows Settings
- Go to Apps → Advanced app settings → App execution aliases
- Turn off
python.exeandpython3.exealiases (if they’re hijacking your command)
After that, try python --version again, or just use py --version.
Error: “No such file or directory” / Python can’t find your script
This happens when you’re running a file that isn’t in your current directory. Fix it by:
- Using
dirto confirm the file is actually there cdinto the correct folder- Or running the script with its full path in quotes
Error: ModuleNotFoundError (but you “installed it already”)
This is almost always a mismatch between:
(1) the Python you used to install packages and (2) the Python running your script.
Fix it by installing packages like this:
Or, if you’re using a virtual environment, activate it and then:
My script runs, then the window disappears
That usually happens when you double-click a file (not when you run it from CMD). Running from CMD keeps the output visible.
If you absolutely must keep a window open, add something like:
How do I see which python CMD is using?
Windows can tell you where it found an executable using where:
If you’re in a virtual environment, you’ll typically see the venv path listed first (or exclusively).
Best Practices for Running Python Files in CMD
- Prefer
pyon Windows for consistent version selection. - Use quotes around any path with spaces.
- Use
python -m pip(orpy -m pip) to avoid pip confusion. - Use a virtual environment for real projects.
- Keep your project structure clean so imports behave predictably.
Once you’re comfortable in CMD, running a Python file becomes less of a “special event” and more like breathingjust with more typing.
(And fewer celebratory snacks, unfortunately.)
Conclusion
Running a Python file from the Command Prompt is simple once you understand three core ideas:
(1) CMD runs things relative to your current folder, (2) Windows may have multiple Pythons installed,
and (3) virtual environments save you from dependency chaos. Use py when possible, quote paths with spaces,
and lean on python -m ... for tools like pip. With those habits, you’ll spend less time wrestling with setup and more time
actually running your code.
of Real-World “Experience” (aka Things People Learn the Hard Way)
If you hang around Python learners long enough, you start seeing the same CMD stories repeat like a sitcom. Someone writes a script, it works in an editor,
they open CMD, type python main.py, andbamWindows responds with a message that sounds like it was written by a confused robot.
The most common “experience” is discovering that the problem isn’t Python at all; it’s where you’re running it from.
CMD is extremely literal. If your prompt says C:UsersYou and your script is on the Desktop, CMD will not magically guess your intentions.
It will simply shrug (metaphorically) and tell you the file doesn’t exist.
Another classic experience: paths with spaces. People will swear they typed the path correctlyand they didexcept they didn’t wrap it in quotes.
CMD then treats the path like it’s a sentence and stops reading at the first space. The fix feels almost insulting (“just add quotes”), but it works.
This is why so many developers create folders like C:code and call it a day. Not because it’s elegant, but because it’s peaceful.
Then there’s the “installed package but ModuleNotFoundError” experiencean emotional rite of passage. The root cause is usually that pip installed the package
into one Python, while CMD is running a different Python. On Windows, this can happen if you installed Python from different places over time.
The practical lesson people learn: stop trusting plain pip. Use python -m pip (or py -m pip) so the installer and the runner
are the same interpreter. It’s not flashy, but it prevents hours of confusion.
A very Windows-specific experience is typing python and getting launched into the Microsoft Store like your computer is trying to sell you something.
That’s the App Execution Alias situation. People often think they “broke Python,” but it’s really Windows intercepting the command. Once you disable those aliases
(or just use py), everything suddenly behaves like a normal programming environment again.
Finally, virtual environments: many folks avoid them until their system packages become a tangled mess. The “experience” here is simpleonce someone uses a venv
and realizes it keeps projects from stepping on each other, they usually don’t go back. It’s like labeling your pantry: you can live without it, but life gets
dramatically easier when you do it.