How to Run a Python File in CMD on Windows 11

Python is one of the most popular programming languages for beginners and professionals alike. Whether you’re creating automation scripts, web applications, data analysis programs, or machine learning projects, you’ll often need to run Python files from the Command Prompt (CMD). Running Python scripts from CMD is fast, efficient, and useful for testing code, automating tasks, and executing programs without opening an Integrated Development Environment (IDE).

Before you can run a Python file in Command Prompt, Python must be installed correctly and added to the system’s PATH environment variable. Once configured, you can execute any Python script with a simple command.

Update Windows Drivers

This guide explains several ways to run a Python file in CMD on Windows 11, along with solutions to common errors you may encounter.

Prerequisites

PC running slow or unstable? Do you want to update drivers?

Before running Python scripts, make sure you have:

  • Windows 11 installed
  • Python installed on your computer
  • A Python (.py) file
  • Administrator access (optional for some tasks)

If Python isn’t installed, download and install the latest version from the official Python website before continuing.

Method 1: Verify That Python Is Installed

First, check whether Python is installed.

  1. Press Windows + S and search for Command Prompt.
  2. Open Command Prompt.
  3. Type the following command:
python --version
  1. Press Enter.

If Python is installed, you’ll see output similar to:

Python 3.13.0

You can also check using:

py --version

If neither command works, Python may not be installed or added to the PATH.

Method 2: Open Command Prompt in the Script Folder

Repair PC

Navigate to the folder containing your Python file.

Option 1

  1. Open File Explorer.
  2. Browse to your Python file.
  3. Click the address bar.
  4. Type:
cmd
  1. Press Enter.

Command Prompt opens directly in that folder.

Option 2

PC running slow or unstable? Do you want to update drivers?

Open Command Prompt normally and use the cd command.

Example:

cd C:\Users\YourName\Documents\Python

Replace the path with your actual folder location.

Method 3: Run the Python File

Once Command Prompt is in the correct folder, run the script using:

python filename.py

For example:

python hello.py

Press Enter.

PC running slow or unstable? Do you want to update drivers?

The Python script executes immediately.

Method 4: Use the Python Launcher

Windows installs the Python Launcher (py) with most Python installations.

Run your script using:

py filename.py

Example:

py hello.py

This is especially useful if multiple Python versions are installed.

Method 5: Run a Python File Using the Full Path

If your script is located in another folder, specify the full path.

Example:

python "C:\Users\YourName\Documents\Scripts\hello.py"

Quotation marks are recommended if the path contains spaces.

Method 6: Run a Python Script with a Specific Python Version

If multiple versions of Python are installed, specify the version.

For Python 3.11:

py -3.11 filename.py

For Python 3.13:

py -3.13 filename.py

This ensures the script runs with the correct interpreter.

Method 7: Pass Command-Line Arguments

Some Python programs accept arguments.

Example:

python program.py file.txt

Or:

python calculator.py 10 20

The script can read these arguments using Python’s sys.argv module.

Method 8: Run Python in Interactive Mode

You can also launch the Python interpreter directly.

Type:

python

or

py

You’ll see the Python prompt:

>>>

Enter Python commands interactively.

Exit by typing:

exit()

or pressing Ctrl + Z followed by Enter.

Method 9: Add Python to PATH

If you receive:

'python' is not recognized as an internal or external command

Python isn’t available in the system PATH.

To fix it:

  1. Search for Environment Variables.
  2. Open Edit the system environment variables.
  3. Click Environment Variables.
  4. Edit the Path variable.
  5. Add the Python installation folder.
  6. Add the Scripts folder.
  7. Save the changes.
  8. Restart Command Prompt.

Now the python command should work from any folder.

Method 10: Run Python from Windows Terminal

Windows Terminal works just like Command Prompt.

  1. Right-click the Start button.
  2. Select Terminal.
  3. Navigate to your script folder:
cd C:\Path\To\Folder
  1. Run:
python filename.py

Windows Terminal supports Command Prompt, PowerShell, and other command-line environments.

Common Errors and Solutions

Python Is Not Recognized

Cause:

  • Python isn’t installed.
  • PATH isn’t configured correctly.

Solution:

Install Python and ensure Add Python to PATH is selected during installation.

File Not Found

Cause:

The script isn’t in the current directory.

Solution:

Use the cd command or specify the full file path.

Permission Denied

Run Command Prompt as an administrator if your script requires elevated privileges.

Module Not Found

Install the missing package using:

pip install package_name

Replace package_name with the required module.

Tips for Running Python Files

  • Keep Python updated.
  • Add Python to the PATH during installation.
  • Use descriptive file names.
  • Store projects in dedicated folders.
  • Use virtual environments for larger projects.
  • Test scripts from Command Prompt before deploying them.

Conclusion

Running a Python file in Command Prompt on Windows 11 is a simple and efficient way to execute scripts, test programs, and automate tasks. Once Python is installed and configured correctly, you can run any .py file using the python or py command from Command Prompt or Windows Terminal. Understanding how to navigate directories, use command-line arguments, and resolve common errors ensures a smooth development experience. Whether you’re a beginner learning Python or an experienced developer, mastering the command line is an essential skill for working with Python on Windows 11.

Frequently Asked Questions

How do I check if Python is installed on Windows 11?

Open Command Prompt and run:

python --version

or

py --version

Why does CMD say “Python is not recognized”?

This usually means Python isn’t installed or its installation folder hasn’t been added to the system PATH.

Can I run Python files from any folder?

Yes. If Python is added to the PATH, you can run scripts from any location by navigating to the folder or specifying the full file path.

What’s the difference between python and py?

python directly calls the Python interpreter, while py uses the Windows Python Launcher, which can automatically select the appropriate Python version if multiple versions are installed.

PC running slow or unstable? Do you want to update drivers?

GeeksDigit.Com
Logo