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.
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.
On macOS, search for the Terminal.app using the Launchpad or Spotlight Search by pressing Command and Spacebar (⌘ + ␣) on your keyboard.
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.
Before going any further, make sure you have downloaded and installed Python from the official website.
Let’s say your Python filename is my_script.py and it’s located in the Users → my_user → Documents → scripts folder. Typically, the terminal opens a session already in your home directory (Users → my_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:
This also works when you provide the full path to your file, for instance:
python /Users/my_user/Documents/scripts/my_script.py
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
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
For Linux users, Python 3 is often invoked using python3 instead of python:
python3 Documents/scripts/my_script.py
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:
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.
Click the Open button and open an empty folder where you’ll create a Python project.
Initialize an environment by typing > in the search bar at the top. Then, select the following options: Python: Create Environment → Venv or Conda (whichever you prefer) → choose the Python interpreter version.
Let’s set up a test Python file by clicking the file icon, as shown below.
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.
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:
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.
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.
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.
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 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.
Vytenis Kaubrė
2025-02-27
Enrika Pavlovskytė
2025-02-27
Get the latest news from data gathering world
Scale up your business with Oxylabs®