Using Python in the Shell – A Practical Guide

Harsh GuptaHarsh Gupta
3 min read

When working with Python, you don’t always need to write a full script or run a .py file to test things out. Python offers an interactive shell environment that lets you quickly run individual lines of code, test logic, or explore how modules behave. In this blog, we’ll explore how to use the Python shell, common mistakes, and how importing and reloading modules works.

🔹What is the Python Shell?

The Python shell is a command-line interface where you can write and execute Python code interactively. It’s a great tool for:

  • Testing small snippets of code

  • Trying out Python features quickly

  • Debugging or prototyping

🔹How to Start the Pyhton Shell

To open the shell:

  • On Windows, run:

      python
    
  • On macOS/Linux, run:

      python3
    

You’ll see something like this:

Python 3.x.x (default, ...)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now you’re inside the interactive shell, ready to run Python code.

🔹Why Use the Shell?

Sometimes you want to test simple instructions without the overhead of writing a complete script. The Python shell is ideal for:

  • Learning and practicing Python basics

  • Checking how a built-in function behaves

  • Exploring new modules and APIs

🔹 Importing Modules in the Shell

Python allows you to import both built-in and custom modules. For example:

import os

The os module is built-in, so you don’t need to specify any paths or filenames. Python automatically finds and loads it for you.

here we are using getcwd method to know the current working directory

🔹 Common Mistakes in the Shell

Let’s look at a common mistake when using loops in the Python shell:

for char in "python":
... print(char)

This will throw an error:

IndentationError: expected an indented block after 'for' statement

Correct Way:

for char in "python":
...     print(char)
...

Output:

p
y
t
h
o
n

Always make sure your code is properly indented in the shell—just like in a Python file.

🔹 Importing Custom Python Files in the Shell

Suppose you have a file named hello.py with the following code:

print("hello python")

def greet(name):
    print("hello " + name)

greet("harsh")

Now, in the Python shell:

import hello

Output:

hello python
hello harsh

Python executes the entire file when it's imported—so any print statements or function calls inside it will run automatically.

🔹 Accessing Functions from Imported Files

Once imported, you can use its functions:

hello.greet("python") #outputs: hello python

🔹 Adding Code After Importing – The Catch

Let’s say you modify hello.py like this:

print("hello")

def greet(name):
    print("hello " + name)

greet("harsh")

country_one = "india"
country_two = "japan"

And in your shell, you try to access:

hello.country_one
#ouputs : AttributeError: module 'hello' has no attribute 'country_one'

❓ Why This Happens:

When you first imported hello, the country_one variable didn’t exist yet—so it wasn’t included in the compiled version of the module.

🔹 How to Fix It

✅ Method 1: Re-import the module

Exit the shell, reopen it, and re-import the module:

import hello

Now, hello.country_one will work.

✅ Method 2: Use importlib.reload()

Python provides a way to reload modules without restarting the shell:

from importlib import reload
import hello
reload(hello)

Now this works:

hello.country_one
#outputs: india

🧠 Final Thoughts

Using the Python shell is an excellent way to explore and experiment quickly. It's a core tool in every Python developer’s toolkit—great for learning, debugging, and even testing parts of large applications.

10
Subscribe to my newsletter

Read articles from Harsh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harsh Gupta
Harsh Gupta

I am a CSE undergrad learning DevOps and sharing what I learn.