Run Python Scripts from the Command Line

Learn how to save a Python program as a .py file and run it from Windows Command Prompt or a Linux shell using relative and absolute paths.

What the command line does in Python

The command line is a text-based interface for running programs and navigating folders. On Windows, you can use Command Prompt. On Linux, you can use a terminal application connected to a shell.

Python programs do not have to be run only from an editor or an integrated development environment (IDE). You can save Python code in a file and launch that file from the command line.

Two different things are involved:

  • Python interpreter: The executable program that reads and runs Python code. Common commands for it are python, python3, and py.
  • Python script: A saved source-code file, usually ending with the .py file extension.

A command such as python example.py tells the interpreter to open and run the script named example.py.

Create and save a Python script

Use a basic text editor or code editor to create a new file. Enter the following program:

x = 5
y = 10
total = x + y
print(total)

Save the file as example.py. The .py suffix is the file extension that identifies the file as Python source code.

  1. Create a folder that you can find easily, such as a Documents or programming folder.
  2. Open the folder in your editor.
  3. Create a new text file and enter the program.
  4. Save it with the complete name example.py.
  5. Remember the folder containing the file.

The filename and its folder location determine how you refer to the script at the command line. A script in the terminal's current folder can be referenced by its short filename. A script elsewhere needs a relative or absolute path.

Open a terminal and understand the current folder

On Windows, open Command Prompt. On Linux, open a terminal application. The terminal provides access to a shell, which is the program that interprets the commands you type.

Commands run relative to the current working directory. This is the folder from which the command is currently being run. If example.py is in that folder, you can use its filename directly. If it is in another folder, the shell will not find it from a short filename.

You can change the current working directory with cd:

Windows Command Prompt:
cd C:\Users\student\Documents

Linux shell:
cd /home/student/documents

After changing folders, the terminal is ready to run a script located there.

Run your first script

The general command pattern is:

python script-filename.py

For the example file, use:

python example.py

On a Linux system where Python 3 uses the separate python3 command, use:

python3 example.py

Windows systems that provide the Python launcher may use:

py example.py

The expected output is:

15

The interpreter reads the assignments, adds 5 and 10, and sends the value of total to the terminal with print().

Use relative and absolute paths

A relative path describes a file location in relation to the current working directory. Examples include example.py and scripts/example.py.

An absolute path is the complete location of a file. It begins at a Windows drive root, such as C:\, or at the Linux filesystem root, written as /.

Use an absolute script path when the terminal is not in the script's folder, when you want to make the location unambiguous, or when another program starts the command from an unknown folder.

Windows Command Prompt:
python "C:\Users\student\Documents\example.py"

Linux shell:
python3 /home/student/documents/example.py

Windows paths commonly use drive letters and backslashes. Linux paths begin with / and use forward slashes. A path containing spaces must be surrounded by quotation marks:

python "C:\Users\student\My Programs\example.py"
python3 "/home/student/My Programs/example.py"

An absolute path to the Python executable is useful when Python is installed but its command is not available through PATH, or when several installations exist and you need a specific one.

"C:\Program Files\Python\python.exe" "C:\Users\student\Documents\example.py"

Do not assume that this example directory exists on every computer. Python installation directories and versions vary.

Command variations by operating system and installation

SituationWindows Command PromptLinux shellPurpose
Run script from current folderpython example.py or py example.pypython3 example.py or python example.pyRun a script whose path is relative to the current directory.
Run script by full pathpython "C:\path\example.py"python3 /path/example.pyRun a script without first changing folders.
Check Python versionpython --version or py --versionpython3 --version or python --versionShow which interpreter and version a command selects.
Display interpreter helppython --help or python /?python3 --helpList available interpreter options.
Use a specific Python versionpy -3 example.py where the launcher supports itUse the required interpreter command or its full pathSelect a particular installed interpreter when available.

Understand the command components

ComponentExampleMeaning
Interpreter commandpython3The executable that reads and runs Python code.
Script filenameexample.pyThe saved Python source file.
Relative script pathscripts/example.pyA path interpreted from the current working directory.
Absolute Windows pathC:\Users\student\Documents\example.pyA complete path beginning at a drive root.
Absolute Linux path/home/student/documents/example.pyA complete path beginning at the filesystem root.
Quoted path with spaces"My Programs/example.py"A path kept together as one command argument.

Use PATH to find Python

PATH is an environment variable: a named configuration value available to programs and shells. It contains a list of directories that the shell searches when you enter an executable command without its full location.

When Python is installed and its interpreter directory is on PATH, a short command such as python can locate the interpreter. If it is not available, the shell may report that python is not recognized, cannot be found, or is an unknown command.

There are two general solutions:

  • Durable solution: Add the directory containing the Python interpreter to the operating system's PATH environment variable, then open a new terminal session. The new session reads the updated configuration.
  • Direct solution: Run the interpreter by its absolute path, quoting that path if it contains spaces.

Adding a directory to PATH does not change the script itself. It changes how the shell discovers executable programs.

Check the Python version and get help

Before running a script, check which Python command is available:

Windows:
python --version
py --version

Linux:
python3 --version
python --version

You can also use the short version option -V:

python -V
python3 -V
py -V

Request interpreter help with:

Windows:
python --help
python /?

Linux:
python3 --help

The Windows py launcher is available on some installations and can be useful when more than one Python version is installed. For example, py -3 example.py asks the launcher to use a Python 3 interpreter where that option is supported.

Troubleshoot command-line execution

Python cannot be found

If the shell says that python is not recognized or cannot be found, Python may not be installed, its interpreter directory may be missing from PATH, or the system may use a different command.

  • Check whether Python is installed.
  • Try python3 on Linux.
  • Try py on Windows when the launcher is available.
  • Run the interpreter using its absolute path.
  • Add the interpreter directory to PATH, open a new terminal, and check again.

Python cannot open the script

If Python starts but cannot open the script file, the interpreter is working; the problem is probably the script location or name.

  • Use cd to enter the folder containing the script.
  • Verify the exact filename, including .py.
  • Pass the script's full path.
  • Put quotation marks around paths containing spaces.

The wrong Python version runs

Multiple Python installations can cause different commands to select different versions. PATH order can also determine which interpreter is found first.

  • Check the version using the exact interpreter command that runs the script.
  • Use py with an explicit version on Windows when supported.
  • Use the desired interpreter's full path or a virtual environment.

The file has an unexpected extension or location

If a file appears to be named example.py but does not run correctly, the editor may have added .txt, or the file may have been saved in a different folder.

  • Enable file extensions in the file manager and confirm the complete name.
  • Locate the file and use its actual path in the command.

The script launches but reports a Python error

This means the interpreter found and started the file, but the program contains a syntax error or another code error. Read the traceback from bottom to top to identify the error type and line number. Correct the indicated line in the editor, save the file, and run the command again.

Exam-relevant notes

  • The interpreter is the executable program; the script is the saved .py source file.
  • The current working directory controls how a relative script path is interpreted.
  • An absolute path identifies a file independently of the current working directory.
  • Windows paths commonly use drive letters and backslashes; Linux paths begin with / and use forward slashes.
  • Quote any path containing spaces.
  • PATH tells the shell where to search for executable commands, not where to search for every script automatically.
  • python, python3, and py are command variations, not guaranteed names on every system.

Once you can save example.py, identify its folder, select the available interpreter command, and run it from the terminal, you can execute Python programs independently of an editor or IDE.