Last modified: Feb 06, 2026 By Alexander Williams
Run Python File in Terminal Guide
Running a Python script is a fundamental skill. The terminal is a powerful tool for this task. This guide will walk you through the process step by step.
You will learn how to check your Python installation, navigate to your file, and execute your code. We will also cover common issues and their solutions.
Prerequisites: Check Your Python Installation
First, ensure Python is installed on your system. Open your terminal or command prompt.
Type the following command and press Enter:
python --version
Or, on some systems, you might need to use python3:
python3 --version
You should see a version number like "Python 3.11.4". If you see an error, you need to install Python first.
Step 1: Create a Simple Python File
You need a Python file to run. Let's create a basic one named hello.py.
Use a text editor like Notepad, VS Code, or even the terminal itself. The file should contain this code:
# This is a simple Python script
print("Hello, World from the terminal!")
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")
Save this file in a location you can easily find, like your Desktop or a dedicated project folder. For more on creating files, see our Python File Create: A Beginner's Guide.
Step 2: Open Your Terminal or Command Prompt
On Windows, search for "Command Prompt" or "PowerShell". On macOS, open "Terminal" from Applications > Utilities. On Linux, use your system's application menu.
Step 3: Navigate to Your File's Directory
The terminal starts in a default directory (like your home folder). You must navigate to where your Python file is saved.
Use the cd command (Change Directory).
For example, if your file is on the Desktop:
cd Desktop
If it's in a folder inside the Desktop:
cd Desktop/my_python_scripts
Use ls (macOS/Linux) or dir (Windows) to list files and confirm your hello.py is there.
Step 4: Run the Python File
Now, execute the script. Use the python command followed by the filename.
python hello.py
If that doesn't work, try python3:
python3 hello.py
You should see the program's output immediately:
Hello, World from the terminal!
What is your name?
Type your name and press Enter. The script will finish:
Nice to meet you, Alex!
Congratulations! You have successfully run a Python file from the terminal.
Running Files with Command Line Arguments
You can pass data to your script when you run it. Modify your hello.py file:
import sys
# sys.argv is a list of command-line arguments
print("Number of arguments:", len(sys.argv))
print("Argument List:", str(sys.argv))
if len(sys.argv) > 1:
print(f"Hello, {sys.argv[1]}!")
else:
print("Hello, World!")
Now run it with an extra argument:
python hello.py Alice
The output will be:
Number of arguments: 2
Argument List: ['hello.py', 'Alice']
Hello, Alice!
Common Errors and How to Fix Them
Beginners often encounter a few common errors. Here’s how to solve them.
'python' is not recognized as a command
This means Python is not in your system's PATH. Reinstall Python and check the "Add Python to PATH" option during installation. Or, use the full path to the Python executable.
File Not Found Error
This is the most common issue. The terminal cannot find your file. You are likely in the wrong directory.
Double-check your location with pwd (macOS/Linux) or cd (Windows). Ensure you have navigated to the exact folder containing your .py file. Our detailed Fix Python File Not Found Error Guide can help you troubleshoot further.
Permission Denied Error (macOS/Linux)
Your script might not have execute permissions. You can run it with the Python interpreter directly (as shown above), which doesn't require file execution permissions. The python hello.py command is the solution.
Syntax Error in Script
The terminal will show the error message from Python. Read it carefully. It will tell you the line number and the problem. Go back to your code editor and fix the typo or syntax mistake.
Why Run Python in the Terminal?
You might wonder why not just use a "Run" button in an editor. The terminal offers key advantages.
It gives you more control. You can easily pass arguments, chain commands, and automate tasks.
It's essential for server environments. Most web servers and cloud machines don't have a graphical interface. You manage everything through a terminal.
It helps with debugging. Error messages and program output are displayed directly in the terminal, providing a clear log of what happened.
Mastering the terminal is a crucial step in becoming a proficient developer. For a deeper dive into execution methods, you can explore our other resource on How to Run Python File in Terminal.
Conclusion
Running a Python file in the terminal is simple once you know the steps. First, check your Python installation. Then, navigate to the correct directory using cd. Finally, execute the script with python filename.py.
This skill unlocks the true power of Python for automation, scripting, and backend development. Practice by creating different scripts and running them. Soon, it will become second nature.
Remember, if you get a "File Not Found" error, stop and check your current directory. That is almost always the cause. Happy coding!