Python is widely used on Windows 11 for programming, automation, data analysis, web development, and many other tasks. After creating a Python script, you can run it directly from Command Prompt instead of opening an IDE every time.
Running a Python file from CMD is simple once Python is installed correctly and Windows can find the Python executable. The basic command is python filename.py, but there are several useful ways to run scripts, including using the Python launcher, providing the complete file path, changing folders first, and passing arguments to a script.
If you are getting errors such as “python is not recognized,” you may need to check the Python installation or PATH configuration before running the file.
Method 1: Check Whether Python Is Installed
Before running a Python file, check whether Python is available from Command Prompt.
- Press Windows + R.
- Type:
cmd
- Press Enter.
- Type:
python --version
- Press Enter.
If Python is installed and available, CMD will display the installed Python version.
You can also try:
py --version
The py command is the Python launcher commonly available with Windows Python installations.
If one of these commands works, you can use it to run your Python script.
Method 2: Navigate to the Python File in CMD
The simplest way to run a Python file is to first navigate to the folder containing the script.
For example, suppose your file is stored in:
C:\Users\User\Documents\Python
Open Command Prompt and enter:
cd C:\Users\User\Documents\Python
Press Enter.
Then use the dir command to see the files in the folder:
dir
Find your Python file, for example:
hello.py
Run it with:
python hello.py
If the Python launcher is required, use:
py hello.py
The script will execute in the same Command Prompt window.
Method 3: Run a Python File Using Its Full Path
You do not have to change the current CMD folder before running a script.
You can provide the complete path to the .py file.
For example:
python "C:\Users\User\Documents\Python\hello.py"
Or:
py "C:\Users\User\Documents\Python\hello.py"
Using quotation marks is especially important when the file path contains spaces.
For example:
python "C:\Users\User\My Python Projects\test.py"
Without quotation marks, CMD may interpret the spaces as separate arguments.
This method is useful when you want to run a script from any directory without first using the cd command.
Method 4: Run the Python File From Its Folder
Windows File Explorer can make it easier to open CMD directly in the folder containing your Python script.
- Open File Explorer.
- Navigate to the folder containing the
.pyfile. - Click the address bar.
- Type:
cmd
- Press Enter.
Command Prompt will open with that folder as the current directory.
You can then run:
python filename.py
Replace filename.py with the actual name of your Python file.
This method is convenient because you do not need to manually type a long folder path.
Method 5: Run a Python File With the Python Launcher
Windows Python installations commonly include the Python launcher, which can be accessed with the py command.
First check it:
py --version
Then run your script:
py script.py
The launcher can be useful when multiple Python versions are installed.
You can view installed Python versions with:
py --list
If your installation supports the command, Windows will show the Python versions available through the launcher.
You can then specify a particular version when running a script. For example:
py -3 script.py
This can be useful when a project requires a particular Python version.
Method 6: Pass Arguments to a Python Script
Python scripts can accept information from CMD when they are started.
For example, create a file named greet.py:
import sys
print("Hello,", sys.argv[1])
Run it from CMD:
python greet.py John
The script receives John as a command-line argument.
You can provide multiple arguments as well:
python greet.py John 25
Python programs can use command-line arguments for filenames, options, search terms, configuration values, and other information.
For larger applications, modules such as argparse provide a more structured way to handle command-line options.
Method 7: Run a Python File With a Virtual Environment
Python projects sometimes use virtual environments to keep project dependencies separate.
First, navigate to the project folder:
cd C:\Users\User\Documents\MyProject
Create a virtual environment:
python -m venv .venv
Activate it:
.venv\Scripts\activate
After activation, the command prompt will normally show the environment name.
You can then run the Python file:
python main.py
When you finish, you can leave the virtual environment with:
deactivate
Virtual environments are particularly useful when different projects require different Python packages or versions of dependencies.
Method 8: Fix “Python Is Not Recognized” in CMD
If you enter:
python --version
and Windows says that Python is not recognized, Python may not be installed or its location may not be available through PATH.
First try:
py --version
If py works but python does not, you can run scripts using:
py script.py
If neither command works, check your Python installation.
You can also use:
where python
and:
where py
If Python is installed but the commands cannot be found, check the installation’s PATH configuration.
When reinstalling Python, the installer normally provides an option to add Python to the system PATH. Enabling that option can make the python command available from CMD.
After changing PATH, close and reopen Command Prompt before testing again.
How to Run a Python File From Any Folder
If the Python executable is correctly configured in PATH, you can run a script by providing its complete path:
python "C:\Path\To\Your\script.py"
You can also navigate to the script’s folder first:
cd "C:\Path\To\Your"
Then:
python script.py
The second approach is often easier when working with several files in the same project.
How to Stop a Running Python Script
If a Python program is running in Command Prompt and you need to stop it, press:
Ctrl + C
This sends an interrupt to the running program.
For scripts that are stuck or continuously running, Ctrl + C is usually the first method to try.
If the program does not respond, you can close the Command Prompt window or use Task Manager to end the associated Python process.
How to Run a Python Script as Administrator
Some Python scripts need administrator privileges to access protected files or perform system-level operations.
To run CMD as administrator:
- Open Start.
- Search for Command Prompt.
- Select Run as administrator.
- Navigate to the script’s folder.
- Run the Python file normally.
For example:
python script.py
Only run scripts that you trust with administrator privileges. A Python program executed with elevated permissions can make changes to the Windows system.
Conclusion
Running a Python file in CMD on Windows 11 is straightforward once Python is installed and available to the command line. The basic command is python filename.py, but you can also use the Python launcher with py filename.py.
If your script is located in another folder, use cd to navigate to its directory or provide the complete path to the .py file. Quotation marks are useful when the path contains spaces.
The Python launcher is especially helpful when multiple Python versions are installed, while virtual environments provide a convenient way to isolate project dependencies.
If Windows reports that Python is not recognized, check whether the py command works and verify the Python installation and PATH configuration. After making changes to PATH, reopen Command Prompt before testing again.
Once Python is correctly configured, CMD provides a quick way to run scripts, pass arguments, work with virtual environments, and manage Python programs without opening a separate development environment.
FAQs
1. What command runs a Python file in Windows 11 CMD?
Use:
python filename.py
You can also use:
py filename.py
2. Why does CMD say Python is not recognized?
Python may not be installed, or its installation directory may not be available through the Windows PATH. Try py --version first and check the Python installation if that command also fails.
3. Can I run a Python file without changing folders?
Yes. Use the complete path to the script:
python "C:\Path\To\script.py"
4. How do I stop a Python program running in CMD?
Press Ctrl + C in the Command Prompt window. This normally interrupts the running Python program.
Quick Summary
- Check Python with
python --versionorpy --version. - Navigate to the script folder with
cdand runpython filename.py. - Use the complete file path when the script is stored elsewhere.
- Use
pyfor the Python launcher andCtrl + Cto stop a running script.





