How to Run Python File in Terminal?

Introduction 

 

Running Python files in the terminal is one of the most fundamental skills for anyone learning Python or working in a development environment. Whether you’re automating tasks, debugging scripts, or deploying code on remote servers, using the terminal provides direct control and speed.

In modern development workflows, the terminal isn’t just for professionals — it’s a core tool for data analysts, backend developers, DevOps engineers, and even non-technical users who need to run automation scripts. Learning to run .py files through the terminal also helps in understanding how Python works behind the scenes and how to manage virtual environments and system-level integrations.

Why Run Python Files in the Terminal? 

Using the terminal to run Python files provides several practical and technical benefits:

  1. Command-line control
    The terminal offers direct interaction with the operating system. This allows developers to run scripts, activate environments, and manage file systems with more control compared to GUI-based tools.
  2. Faster testing and iteration
    For small scripts or experiments, running code in the terminal is quicker than setting up a full IDE workflow. You can make edits, re-run the file instantly, and check results without context switching.
  3. Environment management
    It’s easier to manage Python environments (especially virtualenv, pipenv, poetry) from the terminal. You can isolate dependencies, run different Python versions, and control your development setup more precisely.
  4. Server and cloud compatibility
    Many cloud services (AWS EC2, Google Cloud, etc.) and Linux-based production environments do not have GUIs. Knowing how to run Python in the terminal is a necessity for deployment and automation.
  5. Script automation and scheduling
    Scripts that are run via terminal can be automated using task schedulers like cron (Linux/macOS) or Task Scheduler (Windows), making them ideal for periodic jobs.

 

Step 1: Preparing Your Python File

 

Before executing your Python script in the terminal, you need to make sure the file is ready and saved correctly.

Save your file with a .py extension, for example: script.py.

Here’s an example of a basic Python script:

# script.py
print(“Hello from Python!”)

Step 2: Opening the Terminal

Use one of the following methods to open the terminal on your system:

  • Windows: Press Win + R, type cmd, press Enter

  • macOS: Press Cmd + Space, type Terminal, press Enter

  • Linux: Use Ctrl + Alt + T or search “Terminal” in the applications menu

Common Terminal Commands by OS

Operating System Open Terminal Navigate Directories List Files
Windows Win + R → cmd cd dir
macOS Cmd + Space → Terminal cd ls
Linux Ctrl + Alt + T cd ls

Step 3: Navigating to the File Location

Use the cd command (change directory) to go to the folder where your .py file is saved.

cd Documents/python-projects

Once you’re in the correct folder, confirm the file is there:

ls # for macOS/Linux
dir # for Windows

Step 4: Running the Python File

Now that you’re in the right directory, you can run the script.

➤ Using Python 3:

python3 script.py

➤ On Windows (Python 3 installed as python):

python script.py

Step 5: Running Inside a Virtual Environment

Before running the script, activate your virtual environment:

➤ macOS/Linux:

source env/bin/activate

➤ Windows:

.\env\Scripts\activate

Then run your file as usual:

python script.py

Step 6: Troubleshooting Common Issues

If Python isn’t recognized in the terminal, check your version and installation:

python –version
python3 –version

If it says “Command not found” or “Python is not recognized”, reinstall Python and ensure the PATH variable is set during installation.

Running Python Scripts Automatically

 

In many cases, it’s useful to automate Python scripts to run without manual intervention. This can be done using built-in tools available in most operating systems.

1. Using Task Scheduler on Windows

Windows users can schedule Python scripts to run at specific times using Task Scheduler.

➤ Sample command to use in Task Scheduler:

C:\Path\To\Python\python.exe C:\Path\To\Your\script.py

You can create a .bat file to simplify it:

@echo off
“C:\Path\To\Python\python.exe” “C:\Path\To\Your\script.py”

2. Using Cron Jobs on macOS and Linux

Cron is a time-based job scheduler on Unix-like systems. It allows Python scripts to run automatically at fixed intervals.

➤ Open the crontab editor:

crontab -e

➤ Schedule a Python script to run every day at 8 AM:

0 8 * * * /usr/bin/python3 /home/user/scripts/script.py

Tip: Use which python3 to find the correct path to your Python interpreter.

3. Using .bashrc or .zshrc for Trigger on Terminal Launch

You can add commands to your .bashrc or .zshrc file to automatically run a script when you open a new terminal session.

python3 ~/scripts/auto_script.py

This is helpful for setting up initialization scripts or development environments.

Key Steps to Run Python File in Terminal

Step Action Example Command
1. Prepare Python File Save as .py file script.py
2. Open Terminal Use system shortcut Ctrl+Alt+T / Win+R → cmd
3. Navigate to File Change directory cd Documents
4. Run File Execute with Python python script.py
5. Troubleshoot Check PATH & Python version python –version

Conclusion

 

Running Python files in the terminal may seem like a basic skill, but it’s one of the most powerful habits to develop early. From rapid development to scalable deployment, understanding terminal-based execution can significantly speed up your workflow and help you work more efficiently across platforms.

By following the steps in this guide, you’ll not only learn to execute Python scripts but also discover how to automate, debug, and integrate them into your daily routine. If you’re managing virtual environments, working on remote servers, or just beginning your Python journey — mastering the terminal is a smart investment.

Need further help with Python development or have questions about terminal commands? Contact us today, and let our experts guide you to success!

FAQ: How to Run Python File in Terminal?

 

How do I run Python in Terminal on Windows?
Use python filename.py in Command Prompt after navigating to your script’s folder.

What if Python isn’t recognized in the terminal?
You may need to install Python or add it to your system’s PATH during installation.

Do I need to use Python 3 separately?
On some systems (like macOS), Python 2 may still be the default, so use python3 to ensure you’re running Python 3.

How do I run scripts inside a virtual environment?
Activate the virtual environment, then use python script.py as usual.

Can I automate this with shell scripts or cron jobs?
Yes! Once your script runs in terminal, it can be embedded into scheduled jobs, batch files, or shell scripts for automation.

TELL US ABOUT YOUR NEEDS

Just fill out the form or contact us via email or phone:

    We will contact you ASAP or you can schedule a call
    By sending this form I confirm that I have read and accept Digis Privacy Policy
    today
    • Sun
    • Mon
    • Tue
    • Wed
    • Thu
    • Fri
    • Sat
      am/pm 24h
        confirm