Last modified: Feb 06, 2026 By Alexander Williams
How to Run Python File in Terminal
Running a Python script is a fundamental skill. You need to know how to do it. This guide will show you the steps. We cover Windows, macOS, and Linux.
Prerequisites: Python and a Text Editor
First, you must have Python installed. Check by opening your terminal. Type python --version or python3 --version. You should see a version number.
If you get an error, download Python from python.org. Install it following the instructions. Next, you need a Python file. Create a simple script using any text editor.
# This is a simple Python script
# Save this as hello.py
print("Hello, World!")
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")
Save the file with a .py extension. For example, hello.py. Remember where you save it. The file path is important.
Opening Your Terminal or Command Prompt
The terminal is a text-based interface. You use it to give commands to your computer. How you open it depends on your operating system.
On macOS, open the Applications folder. Then go to Utilities and launch Terminal. On Linux, you can use a shortcut like Ctrl+Alt+T.
On Windows, search for "Command Prompt" or "PowerShell". For a better experience, consider using the new Windows Terminal.
Navigating to Your Python File's Directory
Your terminal starts in a default directory. You must navigate to where your Python file is saved. Use the cd command, which means "change directory".
For example, if your file is on the Desktop, you would type:
cd Desktop
If your file is in a folder inside the Desktop, type the full path. Use the ls command (or dir on Windows) to list files. This confirms you are in the right place.
# On macOS/Linux
ls
# Output should show your hello.py file
# On Windows
dir
The Basic Command: Running Your Script
Once in the correct directory, run the file. Use the python command followed by the filename. On many systems, you must use python3.
# Common command
python hello.py
# If python doesn't work, try python3
python3 hello.py
Press Enter. The script will execute line by line. You should see the output in the terminal.
Hello, World!
What is your name? Alex
Nice to meet you, Alex!
Handling Common Errors and Issues
Beginners often face a few common errors. Knowing how to fix them is key.
'python' is not recognized: This means the system can't find Python. On Windows, you might need to add Python to your PATH during installation. Reinstall Python and check the "Add Python to PATH" box.
File Not Found Error: This means the terminal is not in the same directory as your script. Double-check your location with ls or dir. Use the full path to the file if needed.
python /Users/YourName/Desktop/hello.py
Syntax Error in Script: The error will point to a line in your code. Go back to your editor and fix the typo. Common issues are missing colons or quotation marks.
Using Command-Line Arguments
You can pass information to your script when you run it. These are called command-line arguments. Use the sys.argv list in your Python code.
# Save as greet.py
import sys
# sys.argv[0] is the script name
# sys.argv[1] is the first argument
if len(sys.argv) > 1:
name = sys.argv[1]
print(f"Hello, {name}!")
else:
print("Hello, World! Please provide a name.")
Run it with an extra argument after the filename.
python greet.py Sarah
Hello, Sarah!
Running Python Interactively and Exiting
Sometimes you want to test code quickly. You can launch the Python interpreter directly. Type python or python3 with no filename.
python3
You will see the Python prompt >>>. You can type code here and see results immediately. To exit, type exit() or press Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows).
Conclusion
Running a Python file in the terminal is simple. Ensure Python is installed. Navigate to the correct folder. Use the python filename.py command. This is the core of Python development.
Master this basic workflow. It is the first step to building more complex projects. Practice with different scripts. Soon it will become second nature.