Stay in touch!

Never miss out on the latest articles and get sneak peeks of our favorite classes.

How to Run a Python Script: A Complete Guide

ProgrammingPythonSkillsHow-to

Preparations: Desktop or laptop with Python installed

Total time: 5-10 mins

Difficulty: Intermediate/Advanced

Task Yields: After you complete the task, you will be able to run a Python script to automate tasks on your computer

Scripts are pieces of code that can automate tasks and make your workflow easier. If you are a developer, learning how to run a Python script will allow you to test your code before deployment.

 

Millions of developers worldwide use Python to create applications, websites, and machine-learning tools. You can start learning Python by checking out an online Python course.

 

Learning how to run a Python script is not the same as learning how to code in Python, although if you want to write Python scripts, you will need to learn some basic syntax. 

 

In this article, we’ll learn how to run a simple Python script.

What is a Python Script?  

A Python script is code written to perform a specific task. A script is generally intended to be used directly by a user, whereas a module is code that runs inside another file. 

 

A Python script can be as simple as one line or could take up hundreds of lines within a file. Generally, it’s best to keep your code as short as possible and break tasks into separate modules for readability.

 

Some Python scripts do just one simple thing, while others might do many things or even farm out tasks to separate scripts and modules. Generally, if you are automating a simple task on your machine, all the code you need to run will happen within a single script.

The #1 Reason You Need to Know How to Run a Python Script

These days, it’s not just software developers and engineers who need to know how to run a Python script. 

 

With the prevalence and availability of open-source code on forums and sites like GitHub, anyone can download and run small pieces of code to improve their workflows.

 

Automating basic tasks on your machine increases your productivity. Rather than spending many hours updating a spreadsheet, you could run a script to input all the information. Instead of manually searching through files to find a particular line of text, you could run a script.

quotation marks

A Python script is code written to perform a specific task.

How Do I Run a Python Script in Windows?

The easiest way to run Python code or a Python script in Windows is to use the Command Prompt. Open up the Start Menu and type “cmd” into the toolbar. Once the Command Prompt opens, type “python” and append the name of the file that contains your Python script:

 

python filename.py

 

This command starts the Python interpreter and executes whatever code is inside the filename.py file.

How Do I Run Python Code in Terminal?

To run Python code in Terminal on a Mac machine, first, open the Terminal by opening the search bar (command + spacebar) and typing “terminal.” Type “python” into the terminal to open the interpreter and begin an interactive Python session.


If you type “python” and see the message “Command not found: python” it means you do not have Python installed or are running Python3. Type “python3” to start an interactive python3 session, or download a Python release from the official Python website.

how to run a python script

How Do I Run a Script in Python Script?

When a Python script is run inside another script, it is called a “module.” To run a Python module from inside another Python script or module, import the second module into the file that needs to run it and invoke the method you want to run.

 

It’s easiest if the two files are in the same folder. Once they are, type the following command to import the script:

 

import module_name

 

Then run the code by invoking a method inside the module_name file using dot syntax:

 

module_name.run_method()

 

How to Run a Python Script in 3 Easy Steps

  • Write a Python script
  • Open a terminal, command prompt, or Power Shell
  • Type python to start the Python interpreter
  • Append the file name after the python command to run the script

 

Write a Python Script

In this tutorial, we’ll write a basic Python script that prints out the string “Hello World!” You can write a Python script using an IDE (Interactive Development Environment) like VS Code or Pycharm—or you can open a basic text editor and create a simple text file.

 

Open your text editor and create a file with the following line of code in it:

 

print(“Hello World!”)

 

Save the file as hello_world.py.

 

Open A Terminal, Command Prompt, or Power Shell

Depending on your machine (Mac, Linux, Windows), you will open up either a terminal, command prompt or Power Shell.

Open Terminal (Mac)

The easiest way to open a terminal is to press command + spacebar. This will open up the search bar. Type “terminal” into the search bar and hit Enter. This will open up a terminal window.

Open Terminal (Linux)

The easiest way to open a terminal on a Linux machine is to use the keyboard shortcut Ctrl + Alt + T. This opens a new terminal window.

Open Command Prompt or Power Shell(Windows)

To open the command line prompt on Windows, click on the Start Menu in the bottom left corner of the screen. Into the toolbar that appears there, type “cmd” and hit Enter. This opens a Command Prompt.

 

Follow the same steps to open Power Shell, but replace “cmd” with “shell.”

how to run a python script

Start the Python Interpreter

The Python interpreter is the application responsible for running Python code and scripts. It reads a command, evaluates the command, prints the command, and loops back to find the next command.

 

You can start the Python interpreter inside the terminal or command prompt by simply typing “python.” This opens an interactive session into which you can directly type Python commands. What we want to do, however, is run an existing Python script. Move on to the next step to see how.

 

Append the File Name

In the terminal or command prompt, append the name of the file you want to run after the python command. This tells the Python interpreter to execute the given file. The interpreter will read the file line by line, translate the code into bytecode, and send the bytecode off to be run.

 

To run the script we wrote at the beginning of this tutorial, run the following line in the terminal or command prompt:

 

python hello_world.py

 

You will see the output “Hello World!” printed to the next line in the console. That’s it! You just ran your first Python script.

Tips for Successfully Running Python Scripts

Make sure you keep your Python scripts organized and don’t clutter your computer with them. 

 

Always save them to the same location so you can open and edit them easily. 

 

You could also set up a shortcut within the terminal to run the scripts, or you could use the Automater on a Mac machine to automate running the scripts for you.

 

Check out this course on Python for more!

Related Content:

Share this article
Back to top