3 Ways how to Run Python Code (Terminal, Shell, IDEs and Notebooks) with Example

In this beginner Python tutorial, you will learn about the 3 ways that you can use to run Python.

Along the way, you will learn about:

  • How to Use Python Interpreters
  • How to Run Python in the Command line
  • How to Use Integrated Development Environments (IDEs)
  • Python Scripts and Modules
  • Python Notebooks

Installing Python

In order to use Python, you will need to install the Python Interpreter.

Subscribe to my Newsletter

What is the Python Interpreter

The Python interpreter is the software that you need to use in order to Python code and scripts.

To install Python, read one of the following articles:

  • Install Python on MacOS
  • Install Python on Windows
  • Install Python with Anaconda

What are the 3 Ways to Run Python

The three options to run Python code are:

  1. Run Python in the Command line (Terminal)
  2. Run Python in an IDE (Integrated Development Environment)
  3. Run Python in a Python Notebook

1. Run Python with the Terminal (Command-Line)

The first way that you can run Python is using the Terminal. To run Python in the Terminal, or the Command-Line, open your shell.

Check if Python is installed

$ which python3

Open Python

Open the Python Interactive Window using the python3 keyword. Sometimes, the installer is under the python alias.

$ python3

Run Python

>>> print('hello world')

Exit the Python Interactive Window

Either type Command + Shift + D (on Mac) or type in exit() on Windows.

run python in the interactive shell

2. Run Python in an Integrated Development Environment (IDE)

The second way that you can execute Python code is from an Integrated Development Environment.

What is an Integrated Development Environment?

An Integrated Development Environment, also known as an IDE, is a software that gives you the tools to improve the programming workflow.

Some IDEs are Python Specific such as JetBrains’ PyCharm (paid), others can be used with most programming languages such as VS Code (free).

Why Use an IDE?

Integrated development environments are one of the ways that web developers can use to run code. They provide things such as a file management interface, access to a Terminal, run code, along with many other web development features:

  • Syntax Highlighting
  • Autocomplete
  • Debugging
  • Terminal
  • Git version Control

Run Python in IDE’s Terminal

In VS Code, open a new Terminal using Control + Shift + ` or by selecting Terminal > New Terminal in the menu.

From the new Terminal window that just opened, you can run Python just as before.

Run Python in IDE’s Interactive Window

VS Code has a Python interactive window that you can access using Shift + Enter. Alternatively, you can right-click on the line that you want to run and go to Run in interactive Window and select Run From Line in Interactive Window.

The Python Interactive Window will open and run whatever line you wanted to execute.

If this does not work, make sure that you have installed the Python plugins for VS Code.

3. Run Python in a Python Notebook

The third way that you can execute Python code is from a Python Notebook.

What is a Python Notebook?

A Python Jupyter Notebook is an open-source environment that lets you write, read and share your Python code for data analysis. It provides additional capacity such as run code line-by-line and commenting code using the Markdown syntax.

Some examples of Python notebooks are browser-based such a using Google Colab with Python or using a local version of a Jupyter Notebook.

How to use Notebooks

To use a Python notebook, you have to install Python notebook extension on VS Code or use something such as Google Colab.

You create notebook by using the ipynb extension.

Python notebooks have a different file extension than Python files:

  • Python file (.py)
  • Python Notebooks (.ipynb)

In Visual Studio Code, you can create a code block by clicking on + Code.

And then you can execute the block of code using the play button next to the block.

Only the block, not the entire file will have run.

On top of that you can create description for your code by clicking on the + Markdown button and use the markdown syntax to comment your code.

Python Scripts and Modules

Python scripts and modules are both Python files with the .py extensions that are waiting to be executed;

Difference Between a Python Script and a Module

  • Python script: Python file that you run Directly
  • Python module: Python file that you import to be used in another Python file

How to Run a Python Script

To run a Python script, you need to use the python keyword with the location of the file to be executed.

$ python3 path/to/file.py

Let’s take this simple Python file.

To execute the Python file in the Terminal, or in the shell of your IDE, you can type the python3 filename.py command.

How to Run a Python Module

To run a Python module, you need to import the module from another Python file and execute that file.

Take this file for example.

While we called the hello.py file a script earlier, it is now a called a module when I import it from module.py.

You can run this in a similar matter as earlier.

# python3 module.py

And you will see that the the print(‘hello world’) function was execute, but this time as a module.


This is it, you now know the 3 ways that you can use to run Python code: using the Terminal, using an IDE or using a Python Interactive Window.

Enjoyed This Post?