Python Shell, Modules, and Importing Your Own Files
MY LEARNING JOURNEY · 04
Learning Python with Chai aur Code, one video at a time.
Learning source: Chai aur Code — Python Video 4
1. What Is the Python Shell?
The Python shell is an interactive space where we can type Python code and immediately see its result.
For example, if I type:
>>> 10 + 20
30
I get the output immediately, without creating a separate Python file.
But there's one important thing to remember: the code we write directly in the shell isn't automatically saved as a Python file.
Once we close the shell, the code and variables from that interactive session are gone.
I like to think of it as disappearing messages on Instagram, except we're sending code instead of text!
2. How Do I Open the Python Shell?
We can open the Python shell directly from the terminal.
On Windows, type:
python
On macOS or Linux, you may need to type:
python3
Then press Enter.
If Python is installed and available through the terminal, you'll see something similar to:
Python 3.x.x
>>>
That >>> is the Python prompt. It means the shell is ready to accept Python commands.
3. How Do I Exit the Python Shell?
The keyboard shortcut depends on your operating system.
- Windows: Press
Ctrl + Z, then press Enter. - macOS/Linux: Press
Ctrl + D.
These shortcuts send an end-of-input signal, which tells the interactive Python session to exit.
4. A Useful VS Code Terminal Trick
Here's a small tip I found useful while working with directories.
Sometimes, we need to open a particular folder in the terminal, but we don't remember its exact path.
If the folder is already visible in VS Code's Explorer:
- Right-click the folder.
- Select Open in Integrated Terminal.
- VS Code opens a terminal with that folder as the current working directory.
This is especially useful when navigating between directories.
For example, if I accidentally move into the wrong directory or want to return to a particular project folder, I don't always need to manually type the entire path using cd.
A small shortcut, but a helpful one!
5. Does It Matter Which Folder We Open the Python Shell In?
Initially, I thought the folder from which we open the Python shell wouldn't make much difference.
And that's mostly true when we're writing independent Python statements.
For example:
>>> a = 10
>>> b = 20
>>> print(a + b)
30
This doesn't depend on a particular project folder.
However, things change when we want to import a module from a specific file.
Python searches for modules in certain locations, including the current working directory and other locations listed in its module search path.
So, if our Python file is inside a particular project folder, opening the shell from that folder can make importing it easier.
For example, imagine this folder structure:
MyProject/
│
├── main.py
└── chai.py
If we open the Python shell from MyProject, importing chai.py is straightforward:
>>> import chai
But if we open the shell from an unrelated directory, Python might not find chai.py unless its location is included in the module search path.
My takeaway: The current directory matters when working with files, modules, and imports, even though it doesn't affect every Python command.
6. What Does ... Mean in the Python Shell?
While experimenting with the Python shell, we might notice that sometimes the prompt changes from:
>>>
to:
...
So, what does that mean?
The ... prompt is called the continuation prompt.
It appears when Python knows that we're still writing a statement and haven't completed it yet.
This commonly happens with:
- Loops
- Conditional statements
- Functions
- Classes
- Other multi-line statements
For example:
>>> for i in range(3):
... print(i)
...
0
1
2
Notice that after writing the for statement, Python doesn't immediately execute it. It waits for the indented block.
Once we've finished writing the block, we press Enter on an empty line to tell Python that we're done.
Python then executes the statement.
So, ... doesn't indicate an error. It simply means Python is waiting for more input.
7. What Are Modules in Python?
A module is a Python file containing code that can be reused in other Python files.
It can contain:
- Functions
- Variables
- Classes
- Other Python statements
Modules help us organize code into separate files instead of writing everything in one large program.
And modules aren't exclusive to Python. Other programming languages also have ways to organize and reuse code.
Example 1: The os Module
Python provides several built-in modules.
For example:
import os
Here, os is the name of the module we're importing.
Now, we can use functions and other features provided by that module.
For example:
os.getcwd()
getcwd() stands for get current working directory.
It returns the path of the directory from which our Python process is currently working.
Example 2: The sys Module
Another example is:
import sys
We can use:
sys.platform
This gives information about the platform on which Python is running.
For example, it can return values such as win32 on Windows or darwin on macOS.
The general pattern is:
module_name.function_name()
We first import the module and then access its functions or attributes using a dot (.).
8. How Do We Import Our Own Python Files?
We aren't limited to importing Python's built-in modules. We can also create our own modules and import them into other files.
Imagine that we have two files:
MyProject/
│
├── chai.py
└── main.py
Inside chai.py, let's write:
def hello():
print("Hello from Chai!")
Now, inside main.py, we can import the module:
import chai
To call the function from that module:
chai.hello()
We can also access variables defined inside the module.
For example, if chai.py contains:
name = "Python"
We can access it using:
chai.name
One important thing: When importing a Python file as a module, we use its filename without the .py extension.
So, the general syntax is:
import fileName
And to access something inside it:
fileName.functionName()
or:
fileName.variableName
9. What Happens If We Change an Imported File?
This was one of the interesting things I learned.
Suppose we import a module into the Python shell:
>>> import chai
Now, imagine that we open chai.py, change something in it, and save the file.
Will the changes automatically appear in our already-running Python shell?
No, not automatically.
Python doesn't reread the module's source file every time we access it.
When a module is imported, Python normally keeps it in memory. Further imports in the same session generally reuse the already-loaded module.
So, if we modify the original file, our current Python session may still be using the previously loaded version.
There are two ways to handle this.
Method 1: Restart the Python Shell
The simplest approach is to:
- Exit the Python shell.
- Make sure the changes in the Python file are saved.
- Open the Python shell again.
- Import the module again.
This starts a fresh session and loads the updated module.
Method 2: Use importlib.reload()
Python also provides a way to reload an already-imported module without restarting the entire shell.
First, import the reload function:
from importlib import reload
Then, reload the module:
reload(chai)
Here, chai is the module we imported earlier.
This tells Python to re-execute the module's code using its updated source file.
Important: reload() updates the module object, but it doesn't automatically update names that were previously imported directly using statements such as from chai import hello.
For those, you may need to import the name again.
10. My Key Takeaways
Here's what I understood from this video:
- Python Shell: An interactive environment where we can write Python code and immediately see its output.
- Exiting the Shell: We can use
Ctrl + Zfollowed by Enter on Windows, orCtrl + Don macOS/Linux. - Current Directory: The folder from which we open the shell can affect whether Python can find and import our files.
- Continuation Prompt (
...): It indicates that Python is waiting for the remaining part of a statement. - Modules: Python files containing reusable code, such as functions, variables, and classes.
- Importing Modules: We can use
import module_nameand access its contents using dot notation. - Reloading Modules: If we change an imported file, we can restart the shell or use
importlib.reload()to load the updated code.
Final Thoughts
This video helped me understand how the Python shell works and how Python handles modules.
I particularly liked the connection between the current working directory and importing files. Earlier, I didn't think much about where I opened the terminal from, but now I understand why it can matter.
I also learned that importing a module isn't the same as continuously reading its file. Python keeps imported modules loaded in the current session, which explains why changes to a file don't automatically appear.
And honestly, the VS Code trick of opening an integrated terminal directly from a folder is something I'll probably use quite often!
This is part of my Python learning journey, where I document what I understand from each Chai aur Code lecture, the questions I encounter, and the concepts I explore along the way.
Did I miss something or misunderstand a concept? If you have a better explanation or a useful tip for working with Python modules, I'd love to learn from you in the comments.
About the author
- As a newbie, learning Python, bit by bit and error by error. I write about the things I learn, the queries I have and things I finally understand. If you’re learning as well, let’s do this together!
Comments
Join the discussionNo comments yet. Be the first to comment.
Python Learning Journey
I'm documenting my journey of learning Python through Chai aur Code, one video at a time. I'll share what I understand, the questions I encounter, the concepts I explore with AI, and the code I experiment with along the way. My goal is to learn by questioning, explaining concepts in my own words, and sharing the journey publicly. I'm a beginner, and I'm always open to suggestions that can help me learn better.


