The basic command to run a Python file

Open Command Prompt, navigate to the folder where your Python file is saved, and type python filename.py (or python3 filename.py on Mac and Linux). Press Enter. If Python is installed correctly and the file has no errors, the program will run and show any output in the command window.

The exact command depends on how Python was installed on your computer and what version you have. Windows often uses python, while Mac and Linux typically use python3. If one doesn't work, try the other.

Key Takeaways

  • Navigate to your file's folder in Command Prompt using cd, then type python filename.py to run it.
  • If you get a "python is not recognized" error, Python either isn't installed or isn't in your system's PATH.
  • You can run a file from any folder by typing the full path: python C:\Users\YourName\Documents\script.py
  • Check your Python version with python --version to confirm it's installed and accessible from Command Prompt.

Finding your file's location in Command Prompt

Before you run a Python file, you need to be in the same folder where it's saved. Open Command Prompt and use the cd command to navigate. For example, if your file is on your Desktop, type cd Desktop and press Enter. If it's in a subfolder, type the full path: cd C:\Users\YourName\Documents\MyPythonFiles

To see what files are in your current folder, type dir (Windows) or ls (Mac and Linux) and press Enter. This shows you all the files and folders in that location, so you can confirm your Python file is there before running it.

What to do if Python isn't recognized

If you type python filename.py and get an error like "python is not recognized as an internal or external command", Python either isn't installed or Command Prompt can't find it. First, check if Python is installed by typing python --version. If that also fails, you need to install Python from python.org.

During installation on Windows, make sure to check the box that says "Add Python to PATH" — this lets Command Prompt find Python from any folder. If you already installed Python without checking that box, you can reinstall it and select that option, or manually add Python to your PATH through your system settings.

Running a file from a different folder

You don't have to navigate to your file's folder if you know its full path. Type python C:\Users\YourName\Documents\script.py (on Windows) or python /Users/YourName/Documents/script.py (on Mac) and press Enter. This runs the file no matter which folder you're currently in.

This method is useful if you run the same file often or if it's buried several folders deep. Just make sure you type the path exactly as it appears on your computer, including all folder names and the .py extension.

Viewing output and handling errors

When you run a Python file, any output the program produces appears directly in the Command Prompt window. If the program prints text, displays numbers, or asks for input, you'll see it there. If the program finishes without printing anything, the window may appear empty — that's normal.

If your program has an error, Command Prompt shows a message describing what went wrong and which line caused it. Read the error message carefully — it usually tells you the problem and where to look in your code. Common errors include typos in variable names, missing colons, or trying to use a library that isn't installed.

Running Python scripts that need input

Some Python programs pause and wait for you to type something. When you run these from Command Prompt, the window stays open and shows a cursor, waiting for your input. Type what the program asks for and press Enter. The program continues and shows the result.

If you run a program and the window closes immediately, the program may have finished before you could see the output. Add input("Press Enter to close") at the end of your Python file to keep the window open until you press a key. This gives you time to read any output before the window closes.

Frequently Asked Questions

Why does my Python file run but the window closes immediately?

The program finished and Command Prompt closed the window. Add input() at the end of your Python file to pause before closing. Alternatively, run the file from an already-open Command Prompt window instead of double-clicking it, so the window stays open after the program ends.

Can I run a Python file by double-clicking it in File Explorer?

Yes, if Python is installed correctly. Double-clicking a .py file runs it, but the output window closes immediately when the program finishes. For longer output or programs that need input, open Command Prompt and run it from there instead.

What does "ModuleNotFoundError" mean?

Your Python file tried to use a library that isn't installed. Install it using Command Prompt by typing pip install libraryname. For example, pip install requests installs the requests library. After installation, run your Python file again.

How do I run a Python file with arguments?

Type python filename.py argument1 argument2. The arguments appear in your Python file as a list called sys.argv. For example, python script.py hello 42 passes "hello" and "42" to the script, which can read them and use them in the program.

Is there a difference between "python" and "python3"?

Yes. python usually means Python 2 (older, no longer supported) or Python 3 depending on your system. python3 always means Python 3 (current version). On Windows, python typically works. On Mac and Linux, use python3 to be sure you're running the right version.