How To Create A Python Shell

In this tutorial, we will learn about creating a custom Python Shell, which can be used to execute Python commands and get the output. Python Shell is an interactive interpreter that provides a command-line interface to the Python programming language.

It serves as an excellent tool for beginners who wish to learn the language or seasoned developers looking to try out their code snippets quickly.

To achieve this, we will use Python’s in-built modules, specifically the code and sys modules. This tutorial assumes that you have Python installed in your environment and have some basic understanding of the language.

Step 1: Import Modules

Start by importing the required code and sys modules. The code module provides a simple way to create an interactive interpreter, while the sys module lets you interact with the system.

Step 2: Create a Custom Interpreter Class

Next, create a custom class that inherits from the code.InteractiveConsole class. This will serve as the base for our Python Shell. You can override the write method of the class to handle the output of the Shell. For simplicity, we shall print the output to the console.

Step 3: Instantiate the Custom Python Shell

Now that we have our Custom Interpreter class defined, we will create an instance of the class.

This will serve as the main entry point for our Python Shell. We will also initialize the interpreter’s local environment using the built-in globals() function. This will provide access to the global variables and functions of the Shell.

Step 4: Add Custom Commands (optional)

At this step, you can choose to add custom commands to the Python Shell. These commands can be useful for performing specific operations or accessing additional functionalities.

To add a custom command, you can define a new function and add it to the interpreter’s local environment. For example, here is how to add a custom command to print “Hello, World!”:

Now, when you type hello() in the Python Shell, it will print “Hello, World!”. You can extend this principle to create more complex custom commands as per your requirements.

Step 5: Start the Custom Python Shell

Finally, it is time to start the Custom Python Shell. Call the interact() method on the interpreter instance. This will start the interactive interpreter and show the command prompt on the console, ready to accept Python commands for execution.

Full Code


$ python
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, Python Shell")
Hello, Python Shell
>>> hello()
Hello, World!
>>> exit()


In this tutorial, you learned how to create a Custom Python Shell using Python’s built-in code and sys modules. You now know how to create a custom class for the interpreter, instantiate it, and start the interactive interpreter.

Additionally, you also learned how to incorporate custom commands for added functionality. With this knowledge at hand, you can create your Python Shells tailored to your specific needs or use it as a base for building more advanced interfaces or tools in Python.