How To Run Program In Python Shell

In this tutorial, we will be learning how to run a Python program in the Python shell. The Python shell provides an interactive environment to test and debug your code, making it a useful tool for beginners and experienced developers alike. Let’s dive right in and explore how to execute Python code in the Python shell.

Step 1: Launch the Python Shell

To start with, you need to have Python installed on your system. If you haven’t installed it yet, download the latest version of Python from the official website and follow the installation instructions.

After you have Python installed, launch the Python shell by following the steps below:

  • On Windows: Navigate to your Python installation folder (usually found in Program Files), find the folder named “Scripts,” and double-click the “python.exe” application.
  • On macOS or Linux: Open a terminal and type python3 (or python if using an older version) and hit Enter.

The Python shell prompt should now appear, looking something like this:

Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

You are now ready to run Python code in the shell.

Step 2: Run Commands Directly in the Python Shell

To execute simple statements or expressions in the Python shell, just type your code at the prompt and hit Enter. For example:


You can even define and call Python functions in the shell. However, remember that anything you define in the shell will be lost once you close it. Here’s a simple function example:

'Hello, John!'

Step 3: Running a Python Script in the Python Shell

Sometimes you may want to run an entire Python script (*.py file) in the Python shell. Follow these steps to do so:

  1. Save your Python script to a file (for example, “”).
  2. In the Python shell, use the exec() function to run the contents of your script as a string. You will need to read the contents of your script file into a string first. To do this, you can use the following code:

This will execute the code in your “” file inside the Python shell. The code will not display any output, but any functions or variables defined in the file will now be accessible in your Python shell session.

For example, if “” contains the following:

You can now call the greet() function from your Python shell:

'Hello again, John!'

Full Code


Running Python code in the Python shell is a handy way to quickly test and debug your code. It enables you to execute individual commands, define functions, and even run complete Python scripts in an interactive environment. However, it is crucial to remember that any code entered and defined in the Python shell will be lost once the shell is closed.