< Return to Video

Modules, Packages, Libraries - What's The Difference?

  • 0:00 - 0:01
    FLORIAN DEDOV: What
    is going on, guys?
  • 0:01 - 0:02
    Welcome back.
  • 0:02 - 0:03
    In this video today,
    we're going to learn
  • 0:03 - 0:07
    what the differences are between
    libraries, packages, and modules
  • 0:07 - 0:08
    in Python.
  • 0:08 - 0:09
    So let's get right into it.
  • 0:09 - 0:11
    [MUSIC PLAYING]
  • 0:11 - 0:18
  • 0:18 - 0:18
    All right.
  • 0:18 - 0:21
    So this is going to be quite
    simple and straightforward.
  • 0:21 - 0:22
    We're going to learn
    what the differences are
  • 0:22 - 0:25
    between libraries, packages,
    and modules in Python today.
  • 0:25 - 0:28
    Now when I open a terminal and
    install something using pip.
  • 0:28 - 0:31
    So when I say pip
    or pip3 install,
  • 0:31 - 0:33
    I usually say that I'm
    installing an external Python
  • 0:33 - 0:34
    package.
  • 0:34 - 0:36
    However, this might
    actually not always be
  • 0:36 - 0:39
    100% correct because
    sometimes I might actually
  • 0:39 - 0:41
    be installing a library.
  • 0:41 - 0:44
    And I oftentimes say we import
    a package, we import a library,
  • 0:44 - 0:45
    we import a module.
  • 0:45 - 0:47
    And I'm not necessarily
    always using
  • 0:47 - 0:51
    the correct terminology 100% of
    the time, which I don't think
  • 0:51 - 0:51
    is a problem.
  • 0:51 - 0:53
    I'm going to continue to
    do it the way I do it.
  • 0:53 - 0:55
    But in this video
    today, you are going
  • 0:55 - 0:58
    to learn the exact correct
    terminology for these three
  • 0:58 - 1:00
    things, when to use what.
  • 1:00 - 1:05
    So let's start right away
    with a simple structure here.
  • 1:05 - 1:07
    The basic idea is
    that a model is
  • 1:07 - 1:10
    a Python file with some
    functionality-- classes,
  • 1:10 - 1:12
    functions, methods, and so on.
  • 1:12 - 1:15
    And then a package
    contains multiple modules,
  • 1:15 - 1:19
    and a library contains
    multiple modules and packages.
  • 1:19 - 1:22
    So we can start by
    saying I have MyLib here.
  • 1:22 - 1:27
    And in MyLib, I have a directory
    package 1, for example.
  • 1:27 - 1:30
    Then I might have
    package 2 and package 3.
  • 1:30 - 1:35
    And inside of the packages,
    I can have some module 1.
  • 1:35 - 1:38
    I can have a module 2.
  • 1:38 - 1:42
    I can have a module 3.
  • 1:42 - 1:44
    Of course, you can call
    them whatever you want here.
  • 1:44 - 1:46
    These are just example names.
  • 1:46 - 1:48
    And there you go.
  • 1:48 - 1:50
    And then also, I
    might have, outside
  • 1:50 - 1:52
    of this library, a script.
  • 1:52 - 1:53
    A script is a
    Python file that is
  • 1:53 - 1:57
    meant to be run so that you
    can actually execute some code.
  • 1:57 - 2:01
    So main.py is not a package,
    not a module, not a library.
  • 2:01 - 2:03
    It's a script.
  • 2:03 - 2:07
    So the basic idea now
    is that each package
  • 2:07 - 2:09
    contains a so-called init file.
  • 2:09 - 2:14
    So __init__pi.
  • 2:14 - 2:17
    That is what marks
    this as a package.
  • 2:17 - 2:20
    So I can just put it here, put
    it here, and also put it here.
  • 2:20 - 2:23
    And then also I can
    have here another module
  • 2:23 - 2:24
    without a package.
  • 2:24 - 2:28
    So I can have some module pi
    also outside of the packages.
  • 2:28 - 2:29
    And this is now the structure.
  • 2:29 - 2:31
    And these are the names
    that you should be using.
  • 2:31 - 2:34
    If I'm in my script and
    I'm importing something,
  • 2:34 - 2:38
    if I import MyLib, I'm
    importing a library.
  • 2:38 - 2:40
    If MyLib is something that
    I can install using pip,
  • 2:40 - 2:44
    I'm installing a library that
    contains multiple packages
  • 2:44 - 2:45
    and modules.
  • 2:45 - 2:50
    If I import MyLib.Package1, I'm
    importing a package from MyLib.
  • 2:50 - 2:56
    And I can, of course, also say
    from MyLib import Package1.
  • 2:56 - 2:59
    That is a package that
    I'm importing here.
  • 2:59 - 3:03
    I can also import
    from MyLib Package1,
  • 3:03 - 3:07
    I can import a specific
    module, all of it.
  • 3:07 - 3:08
    So the full module.
  • 3:08 - 3:11
    And then, of course, this module
    might have multiple functions.
  • 3:11 - 3:18
    I can have myfunction1 in
    here, which returns hello.
  • 3:18 - 3:19
    And I can copy it.
  • 3:19 - 3:22
    I can have myfunction2.
  • 3:22 - 3:26
    And then myfunction2
    returns world, for example.
  • 3:26 - 3:29
    And I can also just
    import specific functions
  • 3:29 - 3:32
    from the module of the
    package of the library.
  • 3:32 - 3:37
    So I can say from MyLib
    Package1 module1 import
  • 3:37 - 3:39
    and then myfunction1.
  • 3:39 - 3:43
    Then I can call it like this.
  • 3:43 - 3:46
    So that is the basic
    difference here.
  • 3:46 - 3:48
    Maybe, to understand
    this a little bit better,
  • 3:48 - 3:51
    let's go ahead and use
    matplotlib as an example here.
  • 3:51 - 3:53
    Matplotlib itself is a library.
  • 3:53 - 3:55
    It actually has it
    in the name already.
  • 3:55 - 3:57
    It has a lib at the end,
    which stands for library.
  • 3:57 - 4:01
    So matplotlib, in and
    of itself, is a library.
  • 4:01 - 4:04
    Now, matplotlib has
    multiple packages.
  • 4:04 - 4:07
    I can say matplotlib.,
    and you can
  • 4:07 - 4:10
    see we have modules which are
    here with the Python symbols.
  • 4:10 - 4:13
    But we also have this
    directory symbol here,
  • 4:13 - 4:15
    which is actually a package.
  • 4:15 - 4:18
    So for example
    matplotlib.backends
  • 4:18 - 4:19
    it's a package.
  • 4:19 - 4:21
    And then I can press dot again.
  • 4:21 - 4:24
    And you can see we have
    multiple modules here.
  • 4:24 - 4:27
    So backend of qt5, for example.
  • 4:27 - 4:30
    And of course, you can
    have packages and packages.
  • 4:30 - 4:32
    I don't know if we can find
    something like this here.
  • 4:32 - 4:35
    Maybe matplotlib.axes.
  • 4:35 - 4:35
    No.
  • 4:35 - 4:37
    We don't have this here.
  • 4:37 - 4:40
    Matplotlib.-- I'm not sure
    if we're going to find this.
  • 4:40 - 4:42
    Maybe in the backends.
  • 4:42 - 4:44
    Maybe in-- no.
  • 4:44 - 4:46
    But if you have
    packages and packages,
  • 4:46 - 4:47
    you can do the same thing.
  • 4:47 - 4:52
    So you would have something
    like library dot package 1, dot
  • 4:52 - 4:53
    package--
  • 4:53 - 4:58
    or you could say sub-package1
    module, something like this.
  • 4:58 - 5:00
    So that is the basic idea.
  • 5:00 - 5:02
    You have individual
    Python files with classes,
  • 5:02 - 5:06
    with functionality,
    with stuff to import.
  • 5:06 - 5:07
    That's a module.
  • 5:07 - 5:10
    The script you use to import
    all this and use all this
  • 5:10 - 5:11
    is the script.
  • 5:11 - 5:14
    And then you have packages
    which are directories
  • 5:14 - 5:17
    that contain one or multiple
    modules, and also an init file.
  • 5:17 - 5:20
    And then you have libraries
    if you have multiple packages
  • 5:20 - 5:22
    and modules.
  • 5:22 - 5:25
    So you could also just
    have a single package.
  • 5:25 - 5:27
    And you can also just
    publish a package.
  • 5:27 - 5:28
    And then you can
    install it using pip.
  • 5:28 - 5:31
    But if you have a library with
    multiple packages and modules
  • 5:31 - 5:35
    and sub-packages and so on, that
    is a library, not a package.
  • 5:35 - 5:36
    So that's it for today's video.
  • 5:36 - 5:39
    I hope you enjoyed it and hope
    you learned something new.
  • 5:39 - 5:40
    If so, let me know by
    hitting the Like button
  • 5:40 - 5:42
    and leaving a comment in the
    comment section down below.
  • 5:42 - 5:44
    And of course, don't forget
    to subscribe to this channel
  • 5:44 - 5:46
    and hit the notification
    bell to not miss
  • 5:46 - 5:47
    a single future video for free.
  • 5:47 - 5:49
    Other than that, thank
    you much for watching.
  • 5:49 - 5:52
    See you in the next
    video, and bye!
  • 5:52 - 6:08
Title:
Modules, Packages, Libraries - What's The Difference?
Description:

more » « less
Video Language:
English
Duration:
06:08

English subtitles

Revisions