How to Run a Python File in CMD on Windows 11

If you’ve written your first Python script and want to run it using the Command Prompt (CMD), you’re on the right track. Running Python files through CMD gives you full control, whether you’re testing code, automating tasks, or building real-world projects.

In this guide, we’ll walk you through how to run a Python file in CMD on Windows 11, step-by-step. We’ll also cover how to fix common issues like “Python is not recognized” or “File not found” errors, so you can run scripts smoothly every time.

Why Run Python Files Using CMD?

Running Python scripts in CMD is a skill every developer should know. It’s the simplest way to test and execute code directly without using an IDE (like PyCharm or VS Code).

Here’s why using CMD is useful:

  • Full control: Run, debug, and pass arguments directly.
  • Faster testing: Quickly execute scripts without opening an editor.
  • Automation: Schedule and automate Python tasks using batch commands.
  • Learning benefit: It helps you understand how Python works at the system level.

Step 1: Make Sure Python Is Installed

Before you can run a Python file in CMD, ensure Python is installed correctly on your Windows 11 system.

  1. Press Windows + R, type cmd, and press Enter to open Command Prompt.
  2. In CMD, type: python --version or py --version

If you see output like:

Python 3.14.0

— then you’re good to go!

If you get an error like “Python is not recognized as an internal or external command,” it means Python isn’t added to your PATH.

In that case, reinstall Python and make sure you check the box “Add Python to PATH” during installation.

Step 2: Locate Your Python File

Next, find where your Python script (.py file) is stored.

For example:

C:\Users\John\Desktop\my_script.py

You can run your file from any directory, but you must navigate to the folder containing the file before running it (or provide the full file path).

Step 3: Open CMD in the Script’s Directory

To open Command Prompt in your Python file’s location:

Option 1 — Open manually:

  1. Open File Explorer and navigate to your folder (e.g., Desktop or Documents).
  2. Right-click anywhere in the folder.
  3. Select “Open in Terminal” or “Open Command Prompt here.”

Option 2 — Navigate using CMD:

If you already have CMD open, use the cd command to move to your script’s directory:

cd Desktop

or a full path:

cd C:\Users\John\Desktop

Once inside the folder, type:

dir

to confirm your Python file is listed.

Step 4: Run the Python File

Now that you’re in the correct folder, running your Python script is simple. Use one of the following commands:

python filename.py

or

py filename.py

Example:

python my_script.py

If everything is set up correctly, your Python program will run immediately, and you’ll see its output directly in the Command Prompt window.

Step 5: Run Python File from Any Location

If you’d like to run Python files from anywhere (without navigating to the folder each time), you can provide the full file path.

Example:

python "C:\Users\John\Desktop\my_script.py"

Tip: Always put the path in quotes if it contains spaces — otherwise CMD will throw an error.

Step 6: Use Command Line Arguments (Optional)

You can also pass arguments to your Python scripts directly from CMD. This is useful for automation or when your script needs dynamic input.

Example:

python my_script.py input1 input2

In your Python file, you can handle arguments like this:

import sys
print(sys.argv)

This will print all the arguments passed from CMD.

Step 7: (Optional) Run Python in Interactive Mode

If you want to test small code snippets, you can launch Python directly in interactive mode by typing:

python

You’ll see a prompt like:

>>>

Now you can type and execute commands immediately, such as:

print("Hello, Python!")

To exit interactive mode, type:

exit()

or press Ctrl + Z then Enter.

Step 8: (Optional) Create a Batch File to Run Your Script

If you frequently run the same Python file, you can automate it with a .bat file.

  1. Open Notepad and type: @echo off python "C:\Users\John\Desktop\my_script.py" pause
  2. Save it as run_script.bat.
  3. Double-click the batch file to execute your Python script anytime!

Wrapping Up

Running a Python file in CMD on Windows 11 is simple once you’ve installed Python and added it to your PATH. Whether you’re executing a single script or building full automation workflows, CMD gives you total control over your Python environment.

By following this guide, you can now: run Python files from any folder. pass arguments via command line, and automate execution using batch files.

So go ahead — open CMD, run your first Python script, and start exploring the limitless possibilities of Python programming on Windows 11.

Posted by Arpita

With a background in Computer Science, she is passionate about sharing practical programming tips and tech know-how. From writing clean code to solving everyday tech problems, she breaks down complex topics into approachable guides that help others learn and grow.