• March 10, 2025

How to Run Python File in Terminal?

Running a Python file in the terminal is a fundamental skill for developers, whether you’re a beginner or an experienced programmer. This guide will cover multiple ways to run a Python script in the terminal across different operating systems, including Windows, macOS, and Linux.


1. Prerequisites

Before running a Python script, make sure you have:

Python installed on your system (Python 3.x recommended).
A Python script (.py file) ready to execute.
Access to a terminal (Command Prompt, PowerShell, or Bash).

To check if Python is installed, open the terminal and type:

shCopyEditpython --version

or

shCopyEditpython3 --version

If Python is installed, you’ll see the version number, e.g., Python 3.10.5.


2. Writing a Simple Python Script

Create a Python file (script.py) with the following content:

pythonCopyEditprint("Hello, World!")

Save this file in a directory where you can access it easily.


3. Running a Python File in the Terminal

3.1 Running in Windows (Command Prompt & PowerShell)

Steps:

  1. Open Command Prompt (Press Win + R, type cmd, and hit Enter) or PowerShell.
  2. Navigate to the script’s directory using cd (change directory) command: shCopyEditcd C:\Users\YourUsername\Documents\PythonScripts
  3. Run the script using: shCopyEditpython script.py or if python is mapped to Python 2, use: shCopyEditpython3 script.py
  4. Output: CopyEditHello, World!

3.2 Running in macOS/Linux (Terminal)

Steps:

  1. Open Terminal (Shortcut: Ctrl + Alt + T in Linux, Cmd + Space → Search Terminal in macOS).
  2. Navigate to the script’s directory: shCopyEditcd /home/username/Documents/PythonScripts
  3. Run the script: shCopyEditpython script.py or shCopyEditpython3 script.py
  4. Output: CopyEditHello, World!

4. Running a Python File with Different Methods

4.1 Running a Python File with Absolute Path

If you’re not in the script’s directory, provide the full path:

shCopyEditpython "C:\Users\YourUsername\Documents\PythonScripts\script.py"

or

shCopyEditpython3 /home/username/Documents/PythonScripts/script.py

4.2 Running a Python Script as an Executable

You can make a Python script executable in macOS/Linux:

  1. Add the shebang line at the top of your script: pythonCopyEdit#!/usr/bin/env python3 print("Hello, World!")
  2. Make the script executable: shCopyEditchmod +x script.py
  3. Run it using: shCopyEdit./script.py

4.3 Running a Python Script in the Background

Use & at the end to run the script in the background in macOS/Linux:

shCopyEditpython script.py &

4.4 Running a Python Script with Arguments

Pass arguments when executing a script:

shCopyEditpython script.py arg1 arg2

Example (script.py):

pythonCopyEditimport sys
print("Arguments:", sys.argv)

Run:

shCopyEditpython script.py hello world

Output:

bashCopyEditArguments: ['script.py', 'hello', 'world']

5. Running Python Interactive Mode

You can enter the Python interactive shell by typing:

shCopyEditpython

or

shCopyEditpython3

Then, run Python commands directly.


6. Running Python in Virtual Environments

If using virtual environments, activate it first:

shCopyEditsource venv/bin/activate   # macOS/Linux
venv\Scripts\activate      # Windows

Then, run the script normally.


7. Conclusion

Running Python scripts in the terminal is simple and efficient. You can use basic commands, absolute paths, executable scripts, background execution, and argument passing. Mastering these methods will improve your workflow and productivity. 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *