How I explained what Python Modules and Packages are to an Absolute Beginner
To the best of my abilities
Hi there ๐
If I ever become a tech mentor, this would be how my journey started. (lol...)
So a fellow intern who I had previously helped debug his code contacted me again, but this time he wanted to know what python packages and modules are.
I quickly looked up two articles here on hashnode after which I was able to give him an explanation.
I could tell by his response that my explanation was okay but just how good the explanation is will be decided by you.
PermalinkThe Explanation:
PermalinkA Python module
A python module is basically a python file . It is called a module and not just a file because of its contents. It contains anything from statements to variables, functions e.t.c and it can be imported and used by another python file (or module).
To illustrate, let's create a python file that will have just one variable. We should name it hello.py
. Our hello.py
file should contain the following code:
# say hello
greeting = "Hello, Learner!"
In the code above, we have a variable named greeting
in the file, and its value is "Hello, Learner!".
Now if we create another file, an empty Python file and name it appreciate.py
, we can make use of the greeting
variable from hello.py
in this new file by importing it.
This is how we import it:
# import the greeting variable from hello.py
from hello import greeting
# now we can use it
print(greeting)
# the code below will displayed together with the value from the greeting variable
print("Thank you for learning python")
Copy and paste the code above in your empty appreciate.py
file, save the file and run it. Then watch as the magic unfolds.
This should be the output:
>>> Hello, Learner!
>>> Thank you for learning python
Did it work? I'm sure it did if you followed the instruction thoroughly.
Yes, you can see that we could use the greeting
variable from the hello.py
file despite not directly defining it in our appreciate.py
file. By simply importing it, we were able to access its value.
Such is the power and beauty of Python modules. They give us access to codes stored in them whenever we import them. An ordinary file (text/doc) can not attain this feat.
Modules in Python provide us the flexibility to reuse and organize codes logically. - viscabar
PermalinkA Python Package
Now that we are here, it is probably the right time to say that I have kept something from you.
It is about how modules are imported. We used from hello import greeting
because we assumed that they are both in the same folder/directory. if they were to be in different folders, our import statement would be different.
Since we assumed that they are in the same folder/directory, let's name the folder introduction
Now the introduction folder is what we call a package. It has two modules in it, the hello.py
and appreciate.py
modules.
A Python package is a collection of modules - Python for traders
Our introduction package in this instance is a collection of two modules.
Just as a folder can have several files in it, a package also has several modules in it, and just as the name of a directory can tell you what type of files are stored in them, a package name tells you what type of modules are stored in them.
The name of our package, introduction, is fitting because it contains modules that greets and says goodbye at the end.
Keeping modules in packages helps us to reuse and organize them well.
P.S The example I used here is just a primitive way to use Modules and Packages. Learn more about the way they are used here and here
If you are an absolute beginner just like my colleague, do not worry, you will come across many Modules and Packages in your journey to becoming a Pythonista and when you do, you will see just how useful they are in writing clean, reusable, and optimized codes and programs
P.S I'm enrolled in the ongoing Zuri x I4G internship program and I get to collaborate with both people more skilled than I am and those that are just beginning their programming journey. Learn more about my experience here
Well, that's it. That was how I explained what a Module and Package are in Python. Please let me know what you think about it.
Thanks ๐
Cover photo credit: pexels.com
The two articles I read: viscabar and Python for traders