Back to blog

How to Run a Python Script in Terminal

vytenis kaubre avatar

Vytenis Kaubrė

2025-01-233 min read
Share

A computer's terminal is essential for developers, especially in GUI-less server environments. It enables fast and efficient script execution compared to an Integrated Development Environment, accepts custom script arguments for configuration, and displays complete outputs without truncation. Running Python code files in the terminal remains a crucial skill, seamlessly integrating with development workflows like testing, package management, and git operations.

This guide will show you how to navigate directories, run Python scripts in your Windows, macOS, or Linux terminal, and execute Python programs using VS Code.

Tutorial: how to run Python file in terminal

1. Open the terminal

Windows:

On Windows devices, use the Command Prompt or PowerShell. Command Prompt is the simplest one to use, so open Windows Search and search for Command Prompt or cmd.

Opening Windows Command Prompt

macOS:

On macOS, search for the Terminal.app using the Launchpad or Spotlight Search by pressing Command and Spacebar (⌘ + ␣) on your keyboard.

Opening macOS Terminal.app

Linux:

On Linux, search for the Terminal or its synonyms (cmd, command prompt, shell) in the Activities window or by pressing the Show Apps button. This process may be different depending on your Linux distribution and its version.

Opening Linux (Ubuntu) Terminal

2. Run your Python script

Before going any further, make sure you have downloaded and installed Python from the official website.

Method 1:

Let’s say your Python filename is my_script.py and it’s located in the Usersmy_userDocumentsscripts folder. Typically, the terminal opens a session already in your home directory (Usersmy_user). You can use the python command and provide a relative path to the Python script as shown below:

python Documents/scripts/my_script.py

You may need to use the python3 keyword depending on your Python version and installation:

python3 Documents/scripts/my_script.py

Executing this command line will output everything to stdout if your script contains any print statements, for example:

Running a Python file in the terminal

This also works when you provide the full path to your file, for instance:

python /Users/my_user/Documents/scripts/my_script.py

Method 2:

Alternatively, you can first navigate to the Python script's directory using the cd (change directory) command:

cd Documents/scripts

And then run your Python script directly:

python my_script.py

How to run Python file on different OS

Windows and macOS

To run a Python file, open your Command Prompt or Terminal, type python, followed by the path to your file:

python Documents/scripts/my_script.py

Linux

For Linux users, Python 3 is often invoked using python3 instead of python:

python3 Documents/scripts/my_script.py

Should you use python or python3?

You can easily check which Python keyword you should use by checking the version of your Python via the terminal: python -V. For example, the following screenshot implies that python3 should be used since the python command isn’t found:

Checking the Python version

How to run Python file in VS Code

If you haven’t installed VS Code, make sure to download it for your operating system from the official website. Once that’s done, open VS Code and follow the steps outlined below.

1. Open a folder

Click the Open button and open an empty folder where you’ll create a Python project.

Opening a folder in VS Code

2. Create a Python environment

Initialize an environment by typing > in the search bar at the top. Then, select the following options: Python: Create EnvironmentVenv or Conda (whichever you prefer) → choose the Python interpreter version.

Creating a virtual Python environment

3. Create or upload a .py file

Let’s set up a test Python file by clicking the file icon, as shown below.

Creating a Python file for testing

4. Run a .py file in VS Code

After writing and saving the code you want to execute, click the triangle button (▷) on the right side, as shown in the screenshot below. A dedicated terminal window will open up and output any print statements or errors found in your Python file.

Running a Python file in VS Code

Using the terminal for web scraping

Now that you know how to run Python scripts from your terminal, you're ready to tackle more advanced projects and apply these terminal skills to more complex tasks. One common use case for Python scripts is web scraping – automatically collecting public data from websites. 

Let’s say you need to gather the latest news about a specific topic from multiple sources. The fastest and easiest way to achieve this is to develop a scraper that extracts the data you need, for example:

Running a Python scraper via a terminal

However, running scraping scripts successfully often requires proxy servers to overcome IP blocks, CAPTCHAs, rate limits, and geo-restrictions. For beginner projects, start out with Oxylabs free proxies. Once these proxies aren’t enough, you can always acquire paid proxies, such as Residential Proxies for the best anonymity and block-free web access. Choosing the right proxy infrastructure will ensure your Python scripts can collect public data reliably.

Wrap up

Learning to use the terminal and run Python files through it is an essential skill for any developer. You may also find it beneficial to learn how to run Python files in the background, as shown in this Python script service guide, and develop your skillset to fix Python syntax errors. If you’re looking for proxies and want to test them, check out our in-depth guide on how to use a terminal to test proxies.

Frequently asked questions

How do I run a .py file in the terminal?

As shown in this tutorial, the process goes like this:

  • Open the Terminal, or Command Prompt if you’re on Windows

  • Navigate to the folder where your Python script is located using the cd command. For instance, cd Documents/scripts.

  • Then, execute a python command followed by the name of your script. For example, python my_script.py or python3 my_script.py.

How do I run a Python test in the terminal?

Use python -m unittest test_file.py to run your tests. unittest is Python's built-in module, and it doesn’t require extra installation. To run a specific test method within a test class, you can do python -m unittest test_file.TestClass.test_method. If you want a more verbose output, add the -v flag. Note that you may need to use the python3 keyword, depending on your Python installation.

About the author

vytenis kaubre avatar

Vytenis Kaubrė

Technical Copywriter

Vytenis Kaubrė is a Technical Copywriter at Oxylabs. His love for creative writing and a growing interest in technology fuels his daily work, where he crafts technical content and web scrapers with Oxylabs’ solutions. Off duty, you might catch him working on personal projects, coding with Python, or jamming on his electric guitar.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Get the latest news from data gathering world

I’m interested