The basic command to run a Python file

To run a Python file from your terminal, type python filename.py or python3 filename.py and 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's installed on your computer. Most modern systems use python3 because Python 2 is no longer supported. If you're unsure which one to use, try python3 first — if that doesn't work, try python.

The file must be saved with a .py extension for this to work. If your file is named something like script.txt or has no extension, rename it to script.py before running it.

Key Takeaways

  • Use python3 filename.py to run a Python file, replacing "filename" with your actual file name.
  • Your terminal must be in the same folder as the Python file, or you need to type the full path to the file.
  • If you get 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 — read them carefully because they tell you exactly what went wrong and where.

Making sure you're in the right folder

The terminal runs commands from a specific folder on your computer. If your Python file is in a different folder, the terminal won't find it. Before you run your file, navigate to the folder that contains it.

On Windows, use cd followed by the folder path. For example, if your file is on your Desktop, type cd Desktop and press Enter. On Mac or Linux, the command is the same — cd Desktop works there too.

To see what folder you're currently in, type pwd (on Mac or Linux) or cd by itself (on Windows). The terminal will show you the current path. Once you're in the right folder, you can run your Python file with python3 filename.py.

Using the full file path if the file is elsewhere

If you don't want to navigate to the file's folder first, you can type the full path to the file instead. This tells the terminal exactly where to find it, no matter what folder you're currently in.

On Windows, a full path looks like python3 C:\Users\YourName\Desktop\script.py. On Mac or Linux, it looks like python3 /Users/YourName/Desktop/script.py. Replace the path with the actual location of your file.

If the path contains spaces, wrap it in quotes: python3 "C:\Users\Your Name\Desktop\script.py". Without the quotes, the terminal will misread the path and fail to find your file.

What to do when you see an error

If your Python file doesn't run, the terminal will show an error message. Read it carefully — it tells you what went wrong and which line of code caused the problem. Common errors include typos in your code, missing libraries, or incorrect indentation.

If you see "command not found" or "python is not recognized", Python is either not installed on your computer or the terminal can't find it. On Windows, reinstall Python and make sure to check the box that says "Add Python to PATH" during installation. On Mac, you may need to install Python through Homebrew or download it from python.org.

If you see an error from inside your code — like NameError, SyntaxError, or ModuleNotFoundError — the problem is in your Python script itself, not with running it. The error message will point you to the line number where the problem is.

Checking if Python is installed

Before you try to run a file, confirm that Python is actually on your computer. Open your terminal and type python3 --version. If Python is installed, the terminal will show you the version number, like Python 3.11.5.

If you see "command not found" or "python3 is not recognized as an internal or external command", Python is not installed or not set up correctly. On Windows, download the installer from python.org and run it. On Mac, you can install Python through Homebrew by typing brew install python3 (if you have Homebrew installed). On Linux, use your package manager — for Ubuntu or Debian, type sudo apt install python3.

Running files with command-line 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 argument after the filename: python3 script.py argument1 argument2.

Your Python code reads these arguments using the sys module. For example, if you run python3 script.py hello world, your code can access "hello" and "world" as separate inputs. The script itself must be written to use these arguments — if it's not, typing them won't do anything.

Frequently Asked Questions

Why does my file run but produce no output?

Your code may have run successfully but didn't print anything to the terminal. Add print() statements to your code to see what's happening. For example, print("Hello") will display "Hello" in the terminal when that line runs.

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

On Windows, yes — if Python is installed correctly, you can double-click a .py file to run it. On Mac and Linux, you usually need to use the terminal. Double-clicking may open the file in a text editor instead of running it. The terminal method works on all systems.

What does "ModuleNotFoundError" mean?

Your code is trying to use a library that isn't installed. For example, if your code says import requests but requests isn't installed, you'll see this error. Install the missing library using pip install requests (or whatever the library name is), then run your file again.

How do I stop a Python file that's running?

Press Ctrl+C on Windows or Linux, or Cmd+C on Mac. This sends an interrupt signal to the terminal and stops the running program. If the program doesn't stop, press it again.

Do I need to save my file before running it?

Yes. The terminal runs the file as it exists on your hard drive, not as it appears in your text editor. If you make changes but don't save, the terminal will run the old version. Always save your file before running it.