development

How to run a Python file in terminal: a step-by-step guide

To run a Python file in terminal, first ensure Python is installed and on your system PATH, then navigate to the script’s directory and execute it with the interpreter. This e...

Mara Ellison
How to run a Python file in terminal: a step-by-step guide

How to run a Python file in terminal: a clear, evergreen workflow

To run a Python file in terminal, first ensure Python is installed and on your system PATH, then navigate to the script’s directory and execute it with the interpreter. This evergreen explainer covers verification, execution patterns, common errors, and debugging steps so you can run Python scripts reliably on any major platform.

Verify your Python installation and PATH

Before you run scripts, confirm Python is installed and accessible from the terminal. Use version checks to validate availability and inspect the executable path to avoid surprises later.

Check Python and pip versions

  • python --version or python3 --version
  • pip --version

Confirm paths with which/where

  • which python or where python
  • which python3 or where python3
ActionCommandExpected useful output
Check Pythonpython3 --versionPython 3.x.y
Check pippip --versionpip x.z.z from <path>
Find python locationwhich python3/usr/bin/python3 or C:\Python3x\python.exe

The terminal’s current working directory determines where Python looks for files and relative imports. Use standard directory navigation commands to move to the folder containing your script before execution.

Common shell navigation commands

  • cd path/to/folder
  • cd .. to go up one level
  • cd ~ or cd to go to your home directory
  • pwd (macOS/Linux) or cd (Windows) to show full path

Run a Python file with the interpreter

The simplest way to run Python file in terminal is to call the interpreter with the script name. Use python or python3 depending on your platform and installation, and always type the filename exactly as it appears.

Execution syntax

  • python script.py
  • python3 script.py

Use the executable-bang (shebang) approach

If your script includes a shebang and has executable permissions, you can run it directly. This pattern is common for Unix-like systems but also works on Windows when file associations are set correctly.

Shebang and execution checklist

  1. Add #!/usr/bin/env python3 as the first line
  2. Set executable permission (chmod +x script.py on macOS/Linux)
  3. Run ./script.py from the directory containing the file

Handle arguments and environment considerations

Scripts often rely on correct working directories, virtual environments, and system paths. Understanding how Python resolves modules and how environment variables affect imports helps prevent common runtime errors.

Practical tips

  • Use absolute imports when scripts live in nested packages.
  • Activate virtual environments before running if dependencies are involved.
  • Quote paths and arguments that contain spaces to avoid parsing issues.

Common errors and fixes

When execution fails, these frequent issues and remedies can help you quickly restore flow. Matching error patterns to causes reduces troubleshooting time and supports reproducible runs.

Error symptomLikely causeQuick fix
‘python’ is not recognizedPython not in PATHReinstall Python with Add to PATH or use the full interpreter path
ModuleNotFoundErrorMissing dependencies or wrong cwdpip install -r requirements.txt and check import paths
SyntaxError on older PythonVersion mismatchRun with the correct python3 version or update the script
Permission denied (Unix)Run chmod +x script.py

Related Reading

More pages in this topic cluster.

For i in range 4: A Practical Guide to Python’s Range-Based Loop

In Python, the expression for i in range(4): iterates four times, with i taking the values 0, 1, 2, and 3. This sequence starts at 0 by default and stops before the stop value,...

Read next
Mermaid Recipe: A Technical Guide to Diagram-as-Code Syntax and Usage

Mermaid is a diagramming and charting tool that uses text-based definitions to generate flowcharts, sequence diagrams, class diagrams, Gantt charts, and more directly in the bro...

Read next
How to View a Website's Code

To view a website's code is to inspect the technologies, rules, and structure that define its layout, behavior, and content in a web browser. Most modern browsers ship with deve...

Read next