-
FLORIAN DEDOV: What
is going on, guys?
-
Welcome back.
-
In this video today,
we're going to learn
-
what the differences are between
libraries, packages, and modules
-
in Python.
-
So let's get right into it.
-
[MUSIC PLAYING]
-
-
All right.
-
So this is going to be quite
simple and straightforward.
-
We're going to learn
what the differences are
-
between libraries, packages,
and modules in Python today.
-
Now when I open a terminal and
install something using pip.
-
So when I say pip
or pip3 install,
-
I usually say that I'm
installing an external Python
-
package.
-
However, this might
actually not always be
-
100% correct because
sometimes I might actually
-
be installing a library.
-
And I oftentimes say we import
a package, we import a library,
-
we import a module.
-
And I'm not necessarily
always using
-
the correct terminology 100% of
the time, which I don't think
-
is a problem.
-
I'm going to continue to
do it the way I do it.
-
But in this video
today, you are going
-
to learn the exact correct
terminology for these three
-
things, when to use what.
-
So let's start right away
with a simple structure here.
-
The basic idea is
that a model is
-
a Python file with some
functionality-- classes,
-
functions, methods, and so on.
-
And then a package
contains multiple modules,
-
and a library contains
multiple modules and packages.
-
So we can start by
saying I have MyLib here.
-
And in MyLib, I have a directory
package 1, for example.
-
Then I might have
package 2 and package 3.
-
And inside of the packages,
I can have some module 1.
-
I can have a module 2.
-
I can have a module 3.
-
Of course, you can call
them whatever you want here.
-
These are just example names.
-
And there you go.
-
And then also, I
might have, outside
-
of this library, a script.
-
A script is a
Python file that is
-
meant to be run so that you
can actually execute some code.
-
So main.py is not a package,
not a module, not a library.
-
It's a script.
-
So the basic idea now
is that each package
-
contains a so-called init file.
-
So __init__pi.
-
That is what marks
this as a package.
-
So I can just put it here, put
it here, and also put it here.
-
And then also I can
have here another module
-
without a package.
-
So I can have some module pi
also outside of the packages.
-
And this is now the structure.
-
And these are the names
that you should be using.
-
If I'm in my script and
I'm importing something,
-
if I import MyLib, I'm
importing a library.
-
If MyLib is something that
I can install using pip,
-
I'm installing a library that
contains multiple packages
-
and modules.
-
If I import MyLib.Package1, I'm
importing a package from MyLib.
-
And I can, of course, also say
from MyLib import Package1.
-
That is a package that
I'm importing here.
-
I can also import
from MyLib Package1,
-
I can import a specific
module, all of it.
-
So the full module.
-
And then, of course, this module
might have multiple functions.
-
I can have myfunction1 in
here, which returns hello.
-
And I can copy it.
-
I can have myfunction2.
-
And then myfunction2
returns world, for example.
-
And I can also just
import specific functions
-
from the module of the
package of the library.
-
So I can say from MyLib
Package1 module1 import
-
and then myfunction1.
-
Then I can call it like this.
-
So that is the basic
difference here.
-
Maybe, to understand
this a little bit better,
-
let's go ahead and use
matplotlib as an example here.
-
Matplotlib itself is a library.
-
It actually has it
in the name already.
-
It has a lib at the end,
which stands for library.
-
So matplotlib, in and
of itself, is a library.
-
Now, matplotlib has
multiple packages.
-
I can say matplotlib.,
and you can
-
see we have modules which are
here with the Python symbols.
-
But we also have this
directory symbol here,
-
which is actually a package.
-
So for example
matplotlib.backends
-
it's a package.
-
And then I can press dot again.
-
And you can see we have
multiple modules here.
-
So backend of qt5, for example.
-
And of course, you can
have packages and packages.
-
I don't know if we can find
something like this here.
-
Maybe matplotlib.axes.
-
No.
-
We don't have this here.
-
Matplotlib.-- I'm not sure
if we're going to find this.
-
Maybe in the backends.
-
Maybe in-- no.
-
But if you have
packages and packages,
-
you can do the same thing.
-
So you would have something
like library dot package 1, dot
-
package--
-
or you could say sub-package1
module, something like this.
-
So that is the basic idea.
-
You have individual
Python files with classes,
-
with functionality,
with stuff to import.
-
That's a module.
-
The script you use to import
all this and use all this
-
is the script.
-
And then you have packages
which are directories
-
that contain one or multiple
modules, and also an init file.
-
And then you have libraries
if you have multiple packages
-
and modules.
-
So you could also just
have a single package.
-
And you can also just
publish a package.
-
And then you can
install it using pip.
-
But if you have a library with
multiple packages and modules
-
and sub-packages and so on, that
is a library, not a package.
-
So that's it for today's video.
-
I hope you enjoyed it and hope
you learned something new.
-
If so, let me know by
hitting the Like button
-
and leaving a comment in the
comment section down below.
-
And of course, don't forget
to subscribe to this channel
-
and hit the notification
bell to not miss
-
a single future video for free.
-
Other than that, thank
you much for watching.
-
See you in the next
video, and bye!
-