The basic command to run a Python file

To run a Python file from your terminal, type python filename.py or python3 filename.py, then press Enter. The terminal will execute the code in that file and show you any output or errors.

Which command you use depends on what version of Python is installed on your computer. Python 3 is the current standard, so python3 is usually the safer choice. On Windows, python often points to Python 3 by default, but on Mac and Linux, python may still refer to the older Python 2. If one command does not work, try the other.

Before you run the file, make sure you are in the correct folder in your terminal. If your file is called script.py and sits on your Desktop, you need to navigate to the Desktop folder first using the cd command.

Key Takeaways

  • Use python3 filename.py to run a Python file, where filename.py is the actual name of your file.
  • You must be in the same folder as your Python file, or provide the full path to it, before the command will work.
  • If you see a "command not found" error, Python may not be installed or not added to your system's PATH.
  • Error messages from your code will appear in the terminal so you can see what went wrong and fix it.

Navigating to the right folder

Your terminal always has a current working directory — the folder it is looking at right now. If your Python file is not in that folder, the terminal will not find it. Use the cd command to move to the folder where your file lives.

On Mac and Linux, type cd Desktop to move to your Desktop folder. On Windows, the command is the same. If your file is nested deeper — for example, in a folder called projects inside Desktop — type cd Desktop/projects. To go back up one level, type cd ..

You can also check what folder you are in by typing pwd on Mac and Linux (it stands for "print working directory"), or cd alone on Windows. This shows you the full path to your current location.

Using the full file path instead

If you do not want to navigate folders, you can give the terminal the complete path to your file. On Mac and Linux, type python3 /Users/yourname/Desktop/script.py. On Windows, type python3 C:\Users\yourname\Desktop\script.py.

The full path tells the terminal exactly where to find the file, no matter what folder you are currently in. This is slower to type but useful if you run the same file often from different locations, or if you want to write a command that someone else can copy without changing folder names.

What to do when you see an error

If you type the command and see "command not found" or "python is not recognized", Python is either not installed on your computer or not set up in a way your terminal can find. On Windows, reinstall Python and make sure to check the box that says "Add Python to PATH" during setup. On Mac, you may need to install Python through Homebrew or read it from python.org.

If you see an error that starts with your filename — for example, FileNotFoundError: [Errno 2] No such file or directory: 'script.py' — the terminal cannot find your file. Check that the filename is spelled exactly right, including the .py extension, and that you are in the correct folder.

If your Python code itself has a problem, you will see an error message that tells you what went wrong and which line caused it. These errors are helpful — they point you to the exact spot you need to fix. Read the error message carefully, find that line in your code, and make the correction.

Running Python files with arguments

Some Python files are written to accept input from the terminal when you run them. These are called command-line arguments. To pass them, type the filename followed by the values you want to send, separated by spaces.

For example, if your script expects a name and an age, you might type python3 script.py Alice 30. The script can then read "Alice" and "30" and use them in its code. The script itself must be written to look for these arguments — not all Python files accept them.

Checking your Python version

If you are not sure which version of Python you have, or whether it is installed at all, type python3 --version or python --version. The terminal will print the version number, like Python 3.9.5 or Python 3.11.2.

If neither command shows a version, Python is not installed. You can read it from python.org for free. On Mac, Homebrew (a package manager) is another common way to install it — type brew install python3 if you have Homebrew set up.

Frequently Asked Questions

Do I need to use python3 or can I just type python?

It depends on your computer. On Windows, python usually works. On Mac and Linux, python may point to the old Python 2, so python3 is safer. If one does not work, try the other — there is no harm in testing both.

What if my file is in a different folder than where my terminal is open?

Use the cd command to navigate to that folder first, or provide the full path to the file in your command. For example, python3 /path/to/your/file.py works from anywhere.

Can I run a Python file by double-clicking it instead of using the terminal?

On some systems, yes — if Python is properly installed and set up. But the terminal is more reliable because you can see error messages and control exactly how the file runs. Double-clicking sometimes closes the window too fast to read the output.

What does it mean if my script runs but produces no output?

Your script ran successfully — it just did not print anything to the terminal. This is normal if the script only does background work, like writing to a file or processing data silently. Add print() statements to your code if you want to see what it is doing.

How do I stop a Python script that is running?

Press Ctrl+C on your keyboard (or Cmd+C on Mac). This sends an interrupt signal to the terminal and stops the script. If the script is stuck in an infinite loop or waiting for input, this is how you break out of it.