Python guide

How to Run a Python Script

Open a terminal in the folder that holds your file and type python script.py. On Windows you can also use py script.py, and on macOS and Linux it is usually python3 script.py. This guide covers each system, arguments, VS Code, common errors, and how to run Python online when Python is not installed.

Run a Python script in 4 steps

  1. Save your code in a file that ends in .py, for example hello.py.
  2. Open a terminal: Command Prompt or PowerShell on Windows, Terminal on macOS and Linux.
  3. Go to the folder that holds the file, for example cd Documents/python.
  4. Type python hello.py (py hello.py on Windows, python3 hello.py on macOS and Linux) and press Enter. The output appears in the terminal.

The command on Windows, macOS and Linux

SystemRun a scriptCheck the installed version
Windowspy hello.py or python hello.pypy --version
macOSpython3 hello.pypython3 --version
Linuxpython3 hello.pypython3 --version
Any system, onlinePaste the code in the Python editor and select RunShown above the editor

On Windows, py is the Python launcher that the installer from python.org adds. It finds the newest Python on the computer, so it works even when python is not on the PATH. On macOS and many Linux systems, python either does not exist or starts an old Python 2, which is why python3 is the safe choice there.

If the file is in another folder, give its path instead of changing folders: python3 ~/projects/hello.py on macOS and Linux, or py C:\Users\you\projects\hello.py on Windows. Put the path in quotes if it contains spaces.

Run a Python script online, without installing Python

On a school computer, a Chromebook, a tablet or any machine where you cannot install software, run the script in the browser. The editor runs real Python 3 with NumPy, pandas, Matplotlib and other common packages already installed, and asks for keyboard input when the script calls input().

hello.py
hello.py
import platform
import sys

print("Hello from Python", platform.python_version())
print("Script arguments:", sys.argv[1:])

name = input("What is your name? ")
print(f"Nice to meet you, {name}!")

numbers = [4, 8, 15, 16, 23, 42]
print("Sum:", sum(numbers), "Average:", sum(numbers) / len(numbers))

Run a Python script with arguments

Words after the file name are passed to the script in the list sys.argv. The first item is the script's own name, so the arguments start at sys.argv[1]. Each argument is a string; convert it with int() or float() when you need a number.

python3 greet.py Ada 3
greet.py
greet.py
import sys

# python greet.py Ada 3  ->  sys.argv == ["greet.py", "Ada", "3"]
name = sys.argv[1] if len(sys.argv) > 1 else "world"
times = int(sys.argv[2]) if len(sys.argv) > 2 else 1

for _ in range(times):
    print(f"Hello, {name}!")

For scripts with many options, the standard argparse module adds named options such as --count 3 and a --help message.

What if __name__ == "__main__": does

Python runs a file from top to bottom. Code inside if __name__ == "__main__": runs only when you start the file itself, not when another file imports it. Putting the script's work in a main() function and calling it there keeps the file usable both as a script and as a module.

app.py
app.py
def main():
    total = sum(range(1, 101))
    print("1 + 2 + ... + 100 =", total)


# This block runs when you start the file directly (python app.py),
# but not when another file imports it.
if __name__ == "__main__":
    main()

Run a Python script in VS Code, IDLE or PyCharm

  • VS CodeInstall the Python extension, open the file and select the Run button (the triangle) in the top right, or press Ctrl+F5. The output appears in the terminal panel.
  • IDLEComes with Python on Windows and macOS. Open the file with File, Open, then press F5 or choose Run, Run Module.
  • PyCharmRight-click in the editor and choose Run, or press Shift+F10 after the first run.
  • BrowserThe online Python editor runs the file with Run, steps through it with Debug, and shows variables in the Python visualizer.

Other ways to run Python code

CommandWhat it does
python3 -i hello.pyRuns the script, then leaves you at the >>> prompt with its variables, which is handy for checking results.
python3 -m http.serverRuns a module from the standard library or an installed package by name, here a small web server.
python3 -c "print(2 ** 10)"Runs a short piece of code without a file.
python3Starts the interactive interpreter. Type code line by line and exit with exit().
./hello.pymacOS and Linux: runs the file like a program once its first line is #!/usr/bin/env python3 and you have run chmod +x hello.py.

Common errors when running a Python script

MessageCause and fix
'python' is not recognized as an internal or external commandWindows cannot find Python. Use py instead, or reinstall Python from python.org with "Add python.exe to PATH" checked.
python: command not foundmacOS and Linux name it python3. If python3 is missing too, install Python with your package manager or from python.org.
can't open file 'hello.py': [Errno 2] No such file or directoryThe terminal is in a different folder. Run cd to the file's folder (dir on Windows or ls elsewhere shows the files there) or give the full path.
SyntaxError: invalid syntax after typing python hello.pyYou typed the command inside the Python prompt (>>>). Leave it with exit() and type the command in the system terminal.
ModuleNotFoundError: No module named 'requests'The package is not installed for this Python. Run python3 -m pip install requests (py -m pip install requests on Windows).
Permission denied when running ./hello.pyThe file is not executable. Run chmod +x hello.py, or start it with python3 hello.py.

Next steps

Short lessons with code you can run, starting from the first program.

Frequently asked questions

How do I run a Python script from the command line?

Open a terminal, go to the folder with cd, and type python script.py. On Windows you can use py script.py, and on macOS and Linux python3 script.py.

Why does Windows say 'python' is not recognized?

Python is not on the PATH. Use the py launcher that the python.org installer adds, or reinstall Python with the option to add python.exe to PATH.

Can I run a Python script without installing Python?

Yes. Paste it in the online Python editor and select Run. It runs Python 3 with common packages such as NumPy, pandas and Matplotlib, and it works on Chromebooks, tablets and school computers.

How do I pass arguments to a Python script?

Write them after the file name, for example python3 greet.py Ada 3. The script reads them from sys.argv, where sys.argv[1] is the first argument. Each one is a string.

How do I keep the window open after the script ends on Windows?

Run the script from Command Prompt or PowerShell instead of double-clicking it, or add input("Press Enter to exit") as the last line.

What is the difference between python and python3?

On macOS and Linux, python3 starts Python 3, while python may be missing or start an old Python 2. On Windows, python and py both start Python 3 when it is installed.