0:46
In general, if you search the web and look for what people think a Python module is,
you'll get a lot of answers that says a module is a single file in Python.
This is not actually the case.
So, you can take my word for it or we can go look at the Python docs.
So let's do that.
So, we see here in the Python documentation that a module is
an object that serves as an organizational unit of Python code.
Wonder where I got my definition from.
Modules have a namespace containing arbitrary Python objects.
Modules are loaded into Python by the process of importing.
Nowhere here does it say that a module is a single Python file.
So, I find that definition a little bit suspect.
Clearly, that's not part of what Python believes in modules is.
It also talks about modules having a namespace.
So let's dive into that a little bit deeper.
So, a module also creates a namespace when you input it.
And we've seen this when we type import CSV,
CSV then becomes the name of a namespace.
So if we need to access something in this file in the CSV module,
we type csv.reader. So the csv.
part right here says let's use the CSV namespace
and access the reader object within that namespace.
So, now, what is a package?
Well, a package is a module that can contain other modules or packages.
So, this is correct.
It's definitely not the case that a module is a single file because a package is also
a module and that module contains potentially other modules or other packages.
Now, again, you shouldn't just take my word for it because
this is probably going to run counter to what people say on the web.
Right? If they say a module is a single file,
how can a package be a module if a package by definition can contain other modules?
That means it probably has more than one file associated with it.
So, let's go to the docs again.
And here we are in the Python documentation.
It says that a package is a Python module which can
contain sub modules or recursively sub packages.
It says, technically, a package is a Python module with
an under bar under bar path under bar under bar attribute,
but that effectively means that it has these sub modules or sub packages within it.
So, a package is a module and it is
a special kind of module that can contain other packages or modules.
Packages are generally used for
more complex modules that are better organized into multiple units.
You want to break things up into multiple files because you
have a lot of complex code and it just makes more sense.
Furthermore, Python code that does not come
with Python is generally distributed as packages.
So one example is Pygal which we will use in this course that is
distributed as a package which you can download
and install into your Python distribution.
So, what does it look like when you're using a package?
Well, it just looks like any other module.
So let's imagine that I have a package that's called animals and
it has a bunch of different operations that we can perform to,
I don't know, behave like an animal.
All right. So, if I wanted to use that I could type import animals.
Now, maybe, there is a sub package here which are lizard behaviors.
So I could also potentially import animals.lizards.
If I can spell.
And then finally, maybe I have a Chameleon module inside of all this.
And so I could say import animals.lizards.chameleon.
And so, you can see how a package allows you to organize information and modules
inside of the package in a way that makes
sense for that particular package so that you can use it.
Hopefully, you now have a little bit better understanding of
the differences between Python, modules and packages.
We've been working with modules all along primarily with
the modules that are built into Python and pretty
soon we're going to be working with packages as you install
the Pygal package or other packages into your Python distribution.
Now one of the points that I wanted to make with this video is you need to be wary
of answers that you find when you're searching
the web trying to learn new things about Python.
The typical web answer you get when you search
for what's the difference between a package and a module is
something along the lines of a module is basically
a single Python file whereas a package is a folder full of a bunch of Python files.
This is not technically correct as we have learned here.
If you go to the documentation it says specifically,
it's important to keep in mind that all packages are
modules but not all modules are packages.
And now you understand that so hopefully when you see things you have
a little bit better understanding of what's actually going on in Python.