# Running Python programs

> Learn the different ways to run Python programs, from typing code in the interactive REPL and IDLE to saving your code in a file and executing it.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-11-28 | Topics: [Python](https://flaviocopes.com/tags/python/) | Canonical: https://flaviocopes.com/python-running-programs/

There are a few different ways to run [Python](https://flaviocopes.com/python-introduction/) programs.

In particular, you have a distinction between using interactive prompts, where you type Python code and it's immediately executed, and saving a Python program into a file, and executing that.

Let's start with interactive prompts.

If you open your terminal and type `python`, you will see a screen like this:

![Python REPL terminal showing Python 3.9.0 startup message with prompt ready for input](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_13.44.07.png)

This is the Python REPL (Read-Evaluate-Print-Loop)

Notice the `>>>` symbol, and the cursor after that. You can type any Python code here, and press the `enter` key to run it.

For example try defining a new variable using

```python
name = "Flavio"
```

and then print its value, using `print()`:

```python
print(name)
```

![Python REPL showing variable assignment and print function execution with output Flavio](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.11.57.png)

> Note: in the REPL, you can also just type `name`, press the `enter` key and you'll get the value back. But in a program, you are not going to see any output if you do so - you need to use `print()` instead.

Any line of Python you write here is going to be executed immediately.

Type `quit()` to exit this Python REPL.

You can access the same interactive prompt using the IDLE application that's installed by Python automatically:

![IDLE Python shell window with same variable assignment and print execution as terminal](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.13.25.png)

This might be more convenient for you because with the mouse you can move around and copy/paste more easily than with the terminal.

Those are the basics that come with Python by default. However I recommend to install [IPython](https://ipython.org/), probably the best command line REPL application you can find.

Install it with

```sh
pip install ipython
```

Make sure the pip binaries are in your path, then run `ipython`:

![IPython enhanced REPL with syntax highlighting showing variable assignments and numbered input prompts](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-11_at_09.36.29.png)

`ipython` is another interface to work with a Python REPL, and provides some nice features like syntax highlighting, code completion, and much more.

The second way to run a Python program is to write your Python program code into a file, for example `program.py`:

![Nano text editor showing program.py file with Python code for name variable and print statement](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.01.24.png)

and then run it with `python program.py`

![Terminal showing python program.py command execution with Flavio output](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.01.32.png)

> Note that we save Python programs with the `.py` extension, that's a convention.

In this case the program is executed as a whole, not one line at a time. And that's typically how we run programs.

We use the REPL for quick prototyping and for learning.

On Linux and macOS a Python program can also be transformed into a shell script, by prepending all its content with a special line that indicates which executable to use to run it.

On my system the Python executable is located in `/usr/bin/python3`, so I type `#!/usr/bin/python3` in the first line:

![Nano editor showing Python file with shebang line pointing to usr bin python3 interpreter](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.17.26.png)

Then I can set execution permission on the file:

```sh
chmod u+x program.py
```

and I can run the program with

```sh
./program.py
```

![Terminal showing execution of Python script using dot slash program.py with Flavio output](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.18.42.png)

This is especially useful when you write scripts that interact with the terminal.

We have many other ways to run Python programs.

One of them is using [VS Code](https://flaviocopes.com/vscode), and in particular the official Python extension from Microsoft:

![VS Code Extensions marketplace showing Python extension by Microsoft with install options](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.23.32.png)

After installing this extension you will have Python code autocompletion and error checking, automatic formatting and code linting with `pylint`, and some special commands, including:

**Python: Start REPL** to run the REPL in the integrated terminal:

![VS Code integrated terminal running Python REPL after Start REPL command execution](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.31.36.png)

**Python: Run Python File in Terminal** to run the current file in the terminal:

![VS Code terminal showing Python file execution with program path and Flavio output](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.31.06.png)

**Python: Run Current File in Python Interactive Window**:

![VS Code Python Interactive window showing split view with code editor and interactive output panel](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.30.02.png)

and many more. Just open the command palette (View -> Command Palette, or Cmd-Shift-P) and type `python` to see all the Python-related commands:

![VS Code command palette showing Python-related commands including Run Python File and Start REPL options](https://flaviocopes.com/images/python-running-programs/Screen_Shot_2020-11-10_at_14.32.51.png)
