Menu
Try in Playground

How to Run a Python Script (From Terminal, IDE, or File)

Three ways to run Python code — the interactive shell, a saved .py file from the terminal, and from an editor — with guidance on when to use each.

Three Ways to Run Python, and When to Use Each

Before you write anything serious, it's worth knowing the three places Python code actually runs. Each one fits a different situation, and mixing them up is a common source of confusion on day one.

The three options are:

  1. The interactive shell — a live prompt where every line runs as you type it.
  2. A saved .py file, run from the terminal — the standard way to run anything non-trivial.
  3. An editor's "run" button — a shortcut for option 2 once you're working in VS Code, PyCharm, or similar.

We'll walk through each, then talk about when to pick which.

The Interactive Shell (Great for Poking Around)

Open a terminal and type:

python3

You'll get a prompt that looks like >>>. Every line you type runs immediately. This is perfect for trying out one-liners, remembering what a function does, or testing a small expression without creating a whole file:

>>> 2 + 2
4
>>> name = "Ada"
>>> f"Hello, {name}"
'Hello, Ada'

When you're done, type exit() or hit Ctrl-D.

The shell is a sketchpad. It's fantastic for exploration, but it doesn't save anything. Close the window and your work is gone. That's why, the moment you want to keep code around, you move to a file.

Running a Saved .py File

This is how 99% of Python actually runs — in a file, from the terminal. The steps are always the same:

  1. Create a plain text file with a .py extension. The name is up to you; hello.py works.
  2. Write some Python in it.
  3. Open a terminal in the folder that contains the file.
  4. Run python3 hello.py.

Here's the minimum possible script. Save this in a file called hello.py:

main.py
Output
Click Run to see the output here.

Then in your terminal:

python3 hello.py

You should see three lines of output. That's it — that's the whole workflow. Every tutorial, every production deployment, every cron job, every data pipeline is some version of "run a .py file."

A few things worth understanding about that command:

  • python3 is the interpreter. On Windows, it's usually just python.
  • hello.py is a path relative to your current directory. If you're in the wrong folder, you'll see No such file or directory — check where your terminal is with pwd (macOS/Linux) or cd (Windows) and move to the right place.
  • Python runs the file once, top to bottom, and exits. It doesn't leave anything running afterwards.

Running From an Editor

Once your files grow past a few lines, an editor makes life easier. The two most common options:

  • VS Code, with the official Python extension. Hit the Run button in the top right, or press F5.
  • PyCharm, which has its own Run configuration system.

Both of these do exactly what you just did by hand — they invoke python3 your_file.py behind the scenes, in a terminal pane they embed in the editor. There's no magic. When something misbehaves, you can always drop back to running the file yourself from a terminal.

One useful habit: even when you use an editor's run button, check the command it displays. If it says something like /opt/homebrew/bin/python3.12 hello.py, that tells you exactly which Python interpreter was used. Confusing interpreter problems (where pip install seems to work but import can't find the library) are almost always caused by two different Pythons on the same machine.

Passing Input to Your Script

A script isn't always a one-shot thing. Often you want to give it input while it's running. The simplest way is with input():

main.py
Output
Click Run to see the output here.

When you run this in a real terminal, it waits for you to type something and press enter. You can run the snippet above here in the browser too — the embedded runtime handles the prompt for you.

When Scripts Don't End

Most scripts run once and exit. Some run forever — a web server, for example, or a bot that watches a folder for new files. If you accidentally write an infinite loop and your terminal seems to hang, that's what's happening: your script is still going. Press Ctrl-C to stop it. That key combo is your reset button for anything that gets stuck.

Pick Your Tools, Then Forget About Them

Everything else in these docs assumes you can run a snippet — either by pasting it into a file and running python3 file.py, or by using the editor embedded on the page. Which tool you use for that is up to you, and the right answer often changes over a career. Start simple: a plain text editor and a terminal is a perfectly good setup for the next few weeks.

Next up: the rules Python actually cares about — indentation, line breaks, and the minimal syntax you need to know before anything else makes sense.

Frequently Asked Questions

How do I run a Python script?

Save your code in a file ending in .py, open a terminal in the folder where that file lives, and run python3 your_file.py. Python will execute the lines top-to-bottom and print anything your script prints.

How do I run Python in the terminal?

Typing python3 (or python on Windows) with no filename drops you into Python's interactive shell — a prompt where each line you type runs immediately. Typing python3 file.py runs the saved script end to end.

Do I need an IDE to run Python?

No. The terminal is enough for everything in the early chapters. An editor like VS Code or PyCharm is helpful once your projects grow past a single file, but it's not a requirement for learning the language.

Learn to code with Coddy

GET STARTED