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 display any output it produces. Which command you use depends on which version of Python is installed on your computer and how your system is configured.
On most modern systems, python3 is the standard command for Python 3.x versions, while python alone may point to Python 2.7 (which is no longer supported). If you are unsure which version you have, type python --version or python3 --version to check.
Key Takeaways
- Use python3 filename.py to run a Python file, where filename.py is the actual name of your file in the current directory.
- You must be in the same folder as your Python file, or provide the full path to the file if it is stored elsewhere.
- On Windows, you may need to use py filename.py instead, depending on how Python was installed.
- If you get a "command not found" error, Python is not installed or not in your system's PATH variable.
- The terminal will show any print statements or errors your code produces, and return you to the prompt when the script finishes.
Making sure you are in the right folder
Before you run a Python file, your terminal must be in the same directory (folder) where that file is stored. Use the cd command to change directories. For example, if your file is on your Desktop, type cd Desktop and press Enter.
To see what files are in your current folder, type ls on Mac or Linux, or dir on Windows. This will show you whether your Python file is actually there. If you do not see it, you are in the wrong folder or the file name is spelled differently than you think.
If your Python file is nested inside multiple folders, you can provide the full path instead of navigating there. For example, python3 /Users/yourname/Documents/myscript.py will run the file no matter which folder your terminal is currently in.
Differences between Windows, Mac, and Linux
On Windows, the command is often py filename.py instead of python3. This works if Python was installed through the official installer or the Microsoft Store. If that does not work, try python filename.py or python3 filename.py.
On Mac and Linux, use python3 filename.py. These systems usually come with Python 2 pre-installed, so typing just python will run the older version. Always use python3 to ensure you are running Python 3.x.
If none of these commands work on your system, Python may not be installed, or it may not be in your system's PATH. You can download Python from python.org and reinstall it, making sure to check the box that says "Add Python to PATH" during installation.
What to do if you get an error
A "command not found" error means your terminal does not recognize the python or python3 command. This usually means Python is not installed or not in your PATH. Check your Python installation and reinstall if needed, selecting the option to add Python to your system PATH.
A "No such file or directory" error means the terminal cannot find your Python file. Check that you spelled the filename correctly (including the .py extension) and that you are in the correct folder. Use ls or dir to list the files in your current location.
If your script runs but produces an error message, that is a problem in your Python code itself, not with how you ran it. Read the error message carefully — it will tell you which line has the problem and what went wrong. These errors are normal and part of writing code.
Running Python files with arguments
Some Python scripts are written to accept arguments — extra information you provide when you run the file. For example, python3 script.py myname 25 passes "myname" and "25" to the script, which can then use those values in its code.
Inside the Python file, you access these arguments using the sys module. At the top of your file, write import sys, then use sys.argv to read the arguments. The first item in sys.argv is always the script name itself, so the first argument you passed will be at sys.argv[1].
Running Python in interactive mode
If you type just python3 or python without a filename, your terminal enters interactive mode. This lets you type Python code one line at a time and see results immediately. This is useful for testing small pieces of code or learning how Python works.
In interactive mode, you will see a prompt that looks like >>>. Type any Python code and press Enter to run it. To exit interactive mode and return to your normal terminal prompt, type exit() or press Ctrl+D on Mac and Linux, or Ctrl+Z then Enter on Windows.
Frequently Asked Questions
Do I need to install anything before running a Python file?
You need Python installed on your computer. Download it from python.org and run the installer. On Windows, make sure to check "Add Python to PATH" during installation so the terminal can find it. On Mac, you may need to install the Xcode Command Line Tools as well.
What if my Python file uses libraries that are not built in?
You must install those libraries first using pip, the Python package manager. For example, pip3 install requests installs the requests library. Run this command before running your script. If your script imports a library that is not installed, you will get an ImportError when you try to run it.
Can I run a Python file by double-clicking it?
On some systems, yes. On Windows, you can right-click a .py file and select "Open with Python". On Mac, you may need to set up a script or use an IDE. However, using the terminal gives you more control and lets you see any output or errors your script produces.
How do I stop a Python script that is running?
Press Ctrl+C while the script is running. This sends an interrupt signal and stops the program. Your terminal prompt will return. If Ctrl+C does not work, try Ctrl+Z on Windows or Command+C on Mac.
What is the difference between python and python3?
Python 2 is an older version that is no longer supported. Python 3 is the current version with new features and fixes. The python command often points to Python 2, while python3 points to Python 3. Always use python3 unless you have a specific reason to use Python 2.