Learn how to run Python code?

You can execute Python code from the terminal or from a Python IDE. An IDE is a graphical environment that assitsts in software development.

If you are new to Python, I recommend this course:
Complete Python Programming Course & Exercises

Run Python Interactively

One of the ways to run Python code is by using the interactive shell (repl).
To start an interactive shell, open the terminal app or command line and type python enter.

~ python3
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

By default it shows >>>, where you can type Python code. You can type any expression or statement.

>>> print("Hello World")
Hello World
>>> 15+15
30
>>>

It shows the output of your code immediately.

The downside of the interactive shell is, once you close the shell, your code is gone.
To quit interactive mode:

  • presss Ctrl + d (Unix) or Ctrl + z (Windows)
  • type quit() and press enter.

How to Run Python

You can run Python in the command line. Create the following code in a text editor:

#!/usr/bin/env python3

print("Hello World")

Then save the file as hello.py. You must use the .py extension.

All Python programs are written in code, text files with lots of instructions. These are saved with the extension .py. A program made in Python can be one or more .py files.

python program

Then, if Python is installed, you can run the script with this command:

python3 hello.py

You should see Hello World on the screen. If you see it, you just ran your first Python code!

Related course: Complete Python Programming Course & Exercises

Run in Terminal

Run Python on Linux

To run a Python program, you need to have Python installed. Start the terminal.
In the terminal, type the command below to test if Python is installed

python --version

Python installed?

Open a terminal and enter the directory of your program. The directory may contain more than one python (.py) file.

cd your_app_directory

In the terminal type:

python file.py

where file.py is the name of your program. If you have more than one file, the main program is often the name of the program itself;

Related course: Complete Python Programming Course & Exercises

Run Python on Windows

To run Python programs on windows, there are a few steps you need to do.

  1. Save your code with the .py extension. Lets call it hello.py

  2. Open a command prompt, by pressing start, run and type “cmd” (in the text field) and press OK.

  3. Go to the directory of your Python file with the cd command.

cd C:\Documents and Settings\You\Desktop\Python
  1. Run the script with Python
python hello.py
  1. If you get an error, like the one shown below:
'python' is not recognized as an internal or external command, operable program or batch file.

then python is not in the path (environment variables). Make sure it’s included in your Windows environment variables.

The easiest way to do that, is selecting a box in the installation.

python environment variables on windows

If you have already installed Python, you can set the environment variable in the control panel.

Related course: Complete Python Programming Course & Exercises

Run Python on Mac

You need Python installed on your Mac OS X system. On Mac the older Python 2 is often installed by default.

If you don’t have Python installed, open the Terminal app. Then install Xcode tools:

$ xcode-select --install

Install homebrew with this command:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Finally install Python, type the command:

$ brew install python3

Run Python code

Then you can run the command on your code:

python3 hello.py

You must be in the same directory as your file, or add the path.

python3 /Users/You/Documents/python/hello.py

or move into the directory:

cd /Users/You/Documents/python
python3 hello.py

Run from IDE

Sometimes an easier way to execute Python programs is using a Python IDE.

What is an IDE?

An IDE is a tool that assists in software development. Its a software program that has many features like:

  • code coloring (syntax highlighting)
  • file navigation
  • quick search
  • and much more.

In most IDEs, there’s a play button that instantly runs the Python program. In other words, in a Python IDE, you simply press the ‘play’ button.

PyCharm

The PyCharm IDE lets you run Python code with a single click. After creating the project, press the green play button in the right corner.

pycharm toolbar

The image above shows the header of the program PyCharm, a Python IDE. The green play button can be used to start a program.

In PyCharm, you can also press “Run”, “Run ‘HelloWorld’”.

run python from menu

Download exercises

Related course: Complete Python Programming Course & Exercises