Press "Enter" to skip to content

How to Run a Python Script

Having a solid grasp of the various tricks to executing your Python script helps you code faster by anticipating and avoiding common pitfalls.

Running a Python script is pretty easy, and there are many ways you can go about it. We’ll show you the various ways to do so in this article.

What Do You Need to Run a Python Script?

To run a Python script successfully on your computer, have a look at the following checklist and ensure that you’re ready to go:

  • Make sure that you have Python installed on your computer. If not, head over to the Python website to download and install the latest version of Python.
  • Have a code editor or IDE installed on your PC.
  • Ensure that you add Python to your system variable path so you can call it from the command line.

To check if Python is installed and added to the path already, type python –version in your command line and hit Enter. If you see the Python version displayed, then it’s added to your system path.

However, as we mentioned earlier, there are many ways to run your Python script. Let’s take a look at the various ways below.

How to Run a Python Script Interactively

The interactive Python mode lets you run your script instantly via the command line without using any code editor or IDE.

To run a Python script interactively, open up your command line and type python. Then hit Enter.

You can then go ahead and write any Python code within the interactive mode. When you press Enter, the output of your code appears right away.

Python is indent-sensitive. So this can make writing methods like functions, loops, conditions, or classes a bit confusing while in interactive mode.

To avoid indentation errors while writing methods that require them in interactive mode, ensure that you use the space bar consistently each time you get to a new line.

For instance, you can use a single space for any code directly under a function. Then change to two spaces for the subset that follows it, and so on.

Take a look at the example below for a clearer picture:

An advantage of using the interactive mode is that you can test your code with it. However, you can’t use it to run a project, and when you make mistakes, you might have to write your code again from scratch.

Code written in interactive mode is also volatile. So your code clears off, and you can’t recover it once you close the command prompt.

Type exit() or quit(), then hit Enter to leave interactive mode. You can also exit interactive mode on Windows by pressing Ctrl + Z.

How to Run Python File With Python Command

You can write Python code with any text editor of your choice and run it from the command line using the python command.

Unlike the interactive mode, your code sits in a dedicated Python file with a .py extension.

To run a Python file with the python command:

  1. Create a new file in any directory on your PC. Ensure that you name your file with a .py extension. For instance, you can have myFile.py.
  2. Open that file using any code editor of your choice.
  3. Write your code in the file you just created. Then save it again by hitting Ctrl + S.
  4. Open up the command line and cd into the root directory of the Python file.
  5. Type python myFile.py to execute the code in that Python file, replacing myFile.py with the name of your Python file.

You can save the output of a script you run via the command line as a text file. To do so, use:

python myFile.py > output.txt

This method is ideal for executing real-life Python projects. For instance, running a Flask server.py file like this launches a local server for you.

Run a Python File by Its Name

If you’re using a recent version of Windows, you can now run a Python script by typing the name of the file without adding the python command:

myFile.py

Run Your Python Script With an IDE

Integrated Development Environments or IDEs offer advanced file and folder management systems. So they let you build projects faster by organizing your files in different folders under one directory.

Ultimately, IDEs run Python scripts easily in a virtual environment. They’re ideal for managing projects that run on specific dependencies.

With an IDE, you can write, read, edit, and execute your Python code. As you would a simple code editor, you can run Python scripts written in IDEs from the command prompt using the python command.

Additionally, IDEs like Pycharm or Spyder allow you to run your script with a single click.

Use Browser-Based IDEs

Jupyter Notebook and Google Colaboratory are popular browser-based IDEs that allow you to write and execute Python code quickly. They’re also cell-based and ideal for handling data science projects.

To run a Python script with Google Colaboratory, click File. Then select New notebook to open a notebook where you can write and execute your Python code. You can click + Code to start a new cell.

You can register and start using Google Colaboratory right away without any installatio

Executing a script is similar on both platforms. Once you write a code in a cell, hit Ctrl + Enter to execute that cell.

Run Your Python Script Using the Built-in Python IDLE

Python IDLE is one of the most basic ways you can run any Python script without having to save the file before it works.

To access the Python IDLE, go to the Windows search bar. Type IDLE and open it once it appears.

Alternatively, you can start Python IDLE from the command line. To do so, open up the command line, then enter the command idle.

Once it comes up, you can write your code and execute each line by hitting Enter.

You can save a Python IDLE as well. All you need to do is go to File > Save as. The IDLE then saves your file with a .py extension by default.

Run Python Script as a Module in Another Python File

You can also run a Python script in another Python file. There are many ways to do this, however, using the import statement is ideal.

But let’s have a look at the various ways to do this.

Run Your Python Script as an Imported Module

You can create and import your script as a Python module and then run it using another Python file.

To do so, create a new Python file in the same directory as the Python script you want to run. Open that new file and import the following script:

import myScript.py

If you only need a function or class in the script you want to run, then use an absolute import:

from myScript.py import myFunction
myFunction()

Run a Python Script in Another Python File Using the exec Function

Alternatively, you can run Python code with the built-in exec() function. Create a new Python file in the same directory as the one you want to run and execute the following code:

exec(open('myScript.py').read())

Run a Python Script Using the Built-in Runpy Module

You can also run a Python script with runpy.run_module(). You don’t need to include the .py extension while using this method:

import runpy
runpy.run_module('myScript')

However, you can use runpy.run_path() instead. But this requires the Python extension to work:

import runpy
runpy.run_path('myScript.py')

Using importlib

You can also run a script in another Python file using the importlib module. You don’t need to include the .py extension here either:

import importlib
importlib.import_module('myScript')

Execute Your Python File by Double-Clicking It

Merely double-clicking a Python file works as well. Typically, when you do this, it shows the output of your code in a command line. All you need to do is save the script you want to run with an appended .py extension, and double-click it.

The command line output might be brief, and you won’t see it before it closes. To prevent that, you can add an empty while loop to the end of the code to make the command-line output stay open.

For instance, double-clicking the script containing the code below executes successfully because of the empty while loop at the end:

exec(open('myScript.py').read())
hello = 1 + 2
print(hello)
while True:
""

How Does Python Run Its Scripts?

Python is an extremely versatile, compiled language that executes code with the aid of an interpreter. However, when you run a Python code, a compiler breaks the code down into bytecode before passing it to an interpreter. The interpreter then receives the bytecode and returns a human-friendly and readable output.