Skip to main content

Command Palette

Search for a command to run...

Learn to Execute Python in the Shell: A Beginner's Guide

Published
2 min read
Learn to Execute Python in the Shell: A Beginner's Guide

Hi everybody!

In this article, we’ll discuss how to use Python in the shell—or to be more precise, how to use Python in the REPL.

These two terms are often used interchangeably, but they are not exactly the same. I’ve already explained the clear difference between a Shell and a REPL in my previous article.

👇(https://share.google/RFDbenXUXruU1DKED)

Why Do We Use Python in the Shell?

The first question you might have is:

📌Why should we use Python in the shell? What’s the need?

The answer is simple.

👉We use Python in the shell mainly to:

Quickly check outputs

Test small pieces of code

Clear doubts before writing code in the main file

This helps us catch mistakes early and avoid unnecessary errors in our actual program.

📌How to Use Python in the Shell (REPL)

👉Follow these steps:

Go to the folder where your Python files are present.

Right-click on the folder and choose “Open in Integrated Terminal.”

Type:

python on Windows

python3 on Mac

Press Enter — you are now inside the Python REPL.

📌Importing Files Inside the REPL

To access any Python file inside the folder, simply write:

import (space) filename

Once imported, you can access variables, functions, or classes defined inside that file.

Important Points to Note

1️⃣ Changes Made After Import Will NOT Reflect Automatically

If you modify a file after importing it, the new changes will not be available automatically in the REPL.

Example:

Suppose you add this to first.py:

username = "Riya"

If you try in the REPL:

first.username

You’ll get an error like:

module 'first' has no attribute 'username'

This happens because Python has already loaded the old version of the file.

2️⃣ Reload the Module to See Updates

To reload the file, do this:

from importlib import reload

reload(module name), here module name: first

⚠️ Important:

Do not write the .py extension.

Use only the module name.

Now, if you run:

first.username

You’ll get:

'Riya'

3️⃣ How to Exit the Python REPL

Windows: Ctrl + Z → Enter

Mac/Linux: Ctrl + D

4️⃣ How to Clear the Python REPL

Windows:

import os

os.system('cls')

Mac / Linux:

import os

os.system('clear')

📌Final Thoughts

Using Python in the REPL is a very powerful habit, especially when learning Python or debugging code.

It allows you to experiment freely without breaking your main program.

If you’re a beginner, spending time in the Python REPL will significantly improve your understanding of how Python works.

Happy coding! 🐍✨

📌You can refer to this video from chai and code which helped me to learn this concept 👉 https://youtu.be/OEKrDogH5ew?si=52ngKBGtN_mcDc7f

More from this blog

T

Tech Tea

5 posts

Tech Tea is a space where tech information, insights, and concepts are broken down and shared—just like conversations that reveal “the tea,” but focused entirely on technology.☕