Detailed Notes
This episode looks into modules and packages in Python as we continue moving through topics on the certification syllabus
Useful Links: https://docs.python.org/3/tutorial/modules.html
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] well hello and welcome back we are continuing looking at various things to help us through our python certification this time we're going to look at modules we have touched on these along the way but now let's take a closer look into these things now modules in the past we've seen a few examples where we do an import and then we give it a name that would be a module now for this example we've got a couple of files today that we're working with so i have a module which is i'm calling my mod and all it's got is a variable my name and it's got a function called add we've seen this ad before now what happens when we bring in a module so if i do and the module name is based on the file name uh other than the dot py so i can do an import and this one's actually going to be until enough tells you enough it sees it right there import my mod and so now i can do print my mod dot my name or [Music] i can do my mod.add and i'm gonna do two comma three uh i think that's got to be a string and so now if i can find this let's clear that crap out and if i do what i call it module there we go and so i can see that it's going to print out here it prints the name and it prints a function so now i've added those in i can use these in multiple places so i could take actually some of these examples i have here where i have these nice little functions and helpers and all of this becomes available if i just do a full import now let's look at because i've imported it i do want to show you that my dot name and we can see here so you can get a name out of a module in case you wanted to know this isn't essentially again for some of the other things we've done that are more uh dynamic but you can grab the module name and you can see here that's what it is so it's typically going to be i mean it's going to be exactly this and it's because this name function was actually imported when i did this import my mod now separately let's push these down because what i'm going to do another thing i can do is i can name what i want to pull in so let me go back to my mod and i'm going to do add and let's do multiply so not much difference there now let's see if i come here and i do import my mod i'm going to be able to see again that i can do my mod dot multiply 2 comma three and it prints out six all is well in the world i can also do i can do an import uh my mod let's see i want to do i'm sorry so now i'm going to change a little bit and i'm going to do from my mod import uh what was it called add and if i do that multiply is not going to work anymore and note that all of these have gotten bad according to this and we're going to see that look my mod is not defined so if i do for my mod import add then um i can do let's move this guy up here now i'm importing that function i'm not actually importing the module and so i'm also going to see here mymod.name is no no longer going to be available but here i can directly do add and we see that that ad works because i've simply added this function brought this function in i've imported it and so with this name it is basically just a part of this a part of this class now this gives us an example there's a good little thing we can look at is let's do print dir oh my mod so first let's just do the import my mod because we can actually see some information about this as we've seen when we did our class stuff so now when i do my mod i can see this is all what has come in built-ins cached doc file loader name which we've seen package expect and then add multiply my name so those are those functions in that variable that are brought in as part of it now if i do this let's see so if i just do add now my mod is no longer available and this is not quite it's not going to allow me to do that i don't think yeah oh here we go and so here if i just do a der this is for this script because it's just a straight script when i do that import ad and do a directory notice that add is there so i can also do add comma multiply and now i can get this in [Music] and now we're going to see that i have add and multiply those are now part of this script and i can see where multiply works so i can actually pull in just specific things and also note that the my name is not available i have not imported it so i don't it's not available anywhere now if i do both then everything is going to work fine but we would just have to it really wouldn't i guess it could be moderately confusing because you have to actually have ad but you also have mymod.ad but this still works because it says all right i'm just going to add it all in and you can still see where and multiply my mod are all in they don't show up twice even though they exist twice or they were imported twice essentially and note that i have exam i have the availability to get to those uh one is this add multiply i get to it through this script or i get through it to it through the import so if i bring an import in then i can i reference it that way now i can also change that so i could do import my mod as m and then that means i'm going to come in here and do this and now it's all going to work but now it's referred to as m we can see here everything worked the name is still going to be the correct name so it's going to show me what the actual name is even though i have essentially aliased it out as a different name here now i could also do instead of this line where i do add and multiply i can do from my mod import star and let's just get rid of this one here and let's see what it gets when i do the let's get rid of this guy and let's see what it adds to this local script when i do the dir of it and we can see here because i added star it brought my name in as well so i can either bring functions or variables so this import star is the same as if i did add multiply and my name and note that it realizes that it doesn't need that there we go and so i've got all of those now note here because i did my import as m i didn't point to this earlier m is now available so these are things that i can do stuff with whether it's a function or a variable so since i've brought a module in under specific name then that's how it how the script essentially sees it and so i can also do i can do let's change this around a little bit uh so let's just comment that out for now now i can also do uh import add as uh let's say a multiply as mu actually let's do ad and my name as mn and now i could change it to a d so i can change also not only my module names but i can also change my uh functions and so that's uh all right let's see i'll just do that i'll do that twice so now instead of that it's going to be mn so i can do that and now i can see up here here's my short names you see that that called my name in both those cases now an interesting thing that we could do is i could say i'm going to take m i'm going to do this and this shows me in pycharm what i just did because i did multiply as m then i'm going to have some stuff break because m was imported as a module and now i've overwritten it and so let's see what happens boom right here oh that was mu so let's just do m and so now when i come down here when i do the m dot my name it doesn't exist anymore because the m that i imported is uh oh i don't i didn't get that far into it to be able to print the name but i was uh so i could do this let me move this up a little bit and so here i can see uh let me put up something right there just to show uh let's see what is m so here what is m is multiply so m is this guy but if i call him mu then when i do what is it what is m now it's my mod so you have to watch out it's not necessarily going to let you know when you overwrite stuff and it's note it's top to bottom so whatever you set last is the one it's gonna take so you can get into um you know some issues with that now one other thing with uh with modules that we need to be aware of is the idea of packages and so now what i did is i've got a package called samples which is just a folder that has this init py and then it's got this my mod now this my mod has add two this is different from uh let me do it like this this is different from let's do this this way as well from the one that we used so which is where you could run into stuff so now i can do import samples dot my mod as m and it's going to blow up in a bunch of places because now it actually has um wait yeah so m dot my name doesn't exist but my name too does so let me do my name too first nice maple open a couple places boom boom boom uh no it made it so my mod too but then when it tried to call my name because it's not my mod it's samples not my mind so i could do samples.mymod as sm and then i could do my mods my mind as whoops as m and now i'm specifying which package i'm working with and boom everything works now this init is going to be something that you can throw out for anything within that package so if i wanted to move uh my name to let's say well let's leave that one but let's say that within here i'm just going to do my name equals samples package and then come in here and do well actually let's just do this i'm going to keep it very simple i'm going to do a samples package a knit fired and let's see where that comes up that's probably easier to see what goes on so notice this is right at the beginning when i do the import it kicks off that value it kicks off that script and i can do things um let's see it would not be well let's try this let's do it this way so i'm going to define uh if i go here and i define uh let's just call it me i don't give it anything and then it's just gonna do print me me uh let's see oh that's what it doesn't like okay so if i do define me and now come into my script and do a dir let's see what happens [Music] and so we see here that it's not available that me did not show up however let's look at [Music] let's try samples not me and see what it does oh i'm sorry not samples sm dot and so i can't exit i can't access it here either we're gonna find out or can i boom not uh yeah samples my mod has no attribute because here although i've executed it uh for any package i'm gonna see this so let me uh let's go ahead and duplicate this so my mod two so it's going to be the same thing but just to show you i can go let's go back to net uh well that'll stay the same because i don't really need to do anything with him but if i go to whoops if i go to modules and now i do my mod 2 ssm2 then let's go look at here whoops i don't want to do i don't want to call this because that's just going to break it well and now we can see uh let's see it's fired and so it's only fired once we don't see it fired a second time it's the first time i hit anything in this package it's gonna do stuff what is this stuff i don't know it could vary there's probably some reasons to have packages where you may want to uh maybe log that this is being used or something like that or there may be some additional setup i don't know maybe some resources you want to bring in things like that it may bring in some additional imports so you could have these packages all need certain things available that it would need to bring that in so i could do uh now i'm gonna get too complicated at that because then we'll start getting pretty long into this but it's really the important thing is to understand the different ways that you can bring in modules packages uh the only way in this init underscore underscore and knit underscore underscore that has to be in a folder for it to be a package if i delete this file let's just take that and i'm just going to rename it to not in it uh yes do the refactor and so now when i do that this thing's going to totally blow up because samples oh no i did take it okay i did allow for that used to be you could you were not supposed to have that let me see if i delete it let's just make sure uh do refactor there we go [Music] okay so we are good it used to be that you had to have that init that was how you noted that it was a package but apparently now it's gotten a point where you just need a folder and you can refer to that folder as needed you can use the init as a way to as i said to do some additional work once you're getting into that package i think that's good enough for now we will wrap this one up and as always the uh the code will be out in the repository you can see links in the show notes take a look at those we are we're getting there we're i don't know about 75 80 of the way through this at this point and uh a couple things left that are sort of uh common things like we're gonna deal some file i o and stuff like that so we're getting there just stick with it and before you know it we will be to a point where we can go jump out there and take a shot at getting a python certification as always go out there have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
well hello and welcome back we are
continuing looking at various things to
help us through our python certification
this time we're going to look at modules
we have touched on these along the way
but now let's take a closer look into
these things now modules in the past
we've seen a few examples where we do an
import and then we give it a name
that would be
a module
now for this example we've got a couple
of files today that we're working with
so i have a module
which is i'm calling my mod
and all it's got is a variable my name
and it's got a function called add we've
seen this ad before
now what happens
when we bring in a module
so if i do and the module name is based
on the file name uh other than the dot
py so i can do an import
and this one's actually going to be
until enough tells you enough it sees it
right there import my mod
and so now
i can do print
my mod
dot my name
or
[Music]
i can do
my mod.add
and i'm gonna do two comma three
uh i think that's
got to be a string
and so now if i can find this
let's clear that crap out
and if i do what i call it module
there we go
and so i can see that
it's going to print out here it prints
the name
and it prints a function so now i've
added those in i can use these in
multiple places
so i could take actually some of these
examples i have here where i have these
nice little functions and helpers
and
all of this becomes available if i just
do a full
import
now let's look at because i've imported
it i do want to show you that
my dot
name
and we can see here
so you can get a name out of a module in
case you wanted to know this isn't
essentially again for some of the other
things we've done that are more
uh dynamic
but you can grab the module name and you
can see here that's what it is so it's
typically going to be i mean it's going
to be exactly this
and it's because this name
function was actually imported when i
did this import my mod
now separately
let's push these
down because what i'm going to do
another thing i can do
is
i can name what i want to pull in so let
me
go back to my mod
and i'm going to do add
and
let's do
multiply
so not much difference there
now let's see if i come here and i do
import my mod
i'm going to be able to see
again that i can do my mod dot multiply
2 comma three
and
it prints out six all is well in the
world
i can also do
i can do an import
uh my mod let's see i want to do
i'm sorry
so now i'm going to change a little bit
and i'm going to do from
my mod
import
uh
what was it called add
and if i do that multiply is not going
to work anymore
and
note
that all of these have gotten bad
according to this and we're going to see
that look
my mod is not defined
so if i do for my mod import add
then
um
i can do
let's move this guy up here
now i'm importing that function i'm not
actually importing the module
and so i'm also going to see here
mymod.name is no no longer going to be
available
but here i can directly do add
and we see that that ad works
because i've simply added this function
brought this function in i've imported
it and so with this name it is basically
just a part of this a part of this class
now this gives us an example there's a
good little thing we can look at is
let's do print
dir
oh my mod
so first
let's just do the import my mod because
we can actually see some information
about this as we've seen when we did our
class stuff
so now when i do my mod
i can see this is all what has come in
built-ins cached
doc file loader name which we've seen
package expect and then add multiply my
name so those are those functions in
that variable that are brought in as
part of it
now if i
do this
let's see so if i just do add now my mod
is no longer
available
and this is not quite it's not going to
allow me to do that
i don't think
yeah oh here we go
and so
here if i just do a
der this is for this script
because it's just a straight script when
i do that import ad and do a directory
notice
that add is there
so i can also do add comma
multiply
and now i can get this in
[Music]
and now we're going to see that i have
add and multiply those are now part of
this script and i can see where multiply
works
so i can actually pull in just specific
things and also note
that the my name is not available
i have not imported it so i don't it's
not available anywhere
now if i do both
then everything is going to work fine
but
we would just have to it really wouldn't
i guess it could be moderately confusing
because you have to actually have ad but
you also have mymod.ad
but this still works because it says all
right i'm just going to add it all in
and you can still see where
and multiply my mod are all in they
don't show up twice
even though they exist twice
or they were imported twice essentially
and note that i have exam i have the
availability to get to those uh one is
this add multiply i get to it through
this script or i get through it to it
through the import so if i bring an
import in
then
i can i reference it that way now i can
also change that so i could do import my
mod as m
and then
that means i'm going to come in here
and do this
and now it's all going to work but now
it's referred to as m we can see here
everything worked the name
is still
going to be the correct name so it's
going to show me what the actual name is
even though
i have essentially aliased it out
as a different name here
now i could also do instead of this line
where i do add and multiply
i can do from
my mod
import
star
and let's just get rid of this one here
and let's see what it gets when i do the
let's get rid of this guy
and let's see what it adds to this local
script when i do the
dir of it and we can see here
because i added star it brought my name
in as well
so i can either bring functions or
variables
so this import star
is the same as if i did
add multiply and my name
and note that it realizes that it
doesn't need that there we go
and so i've got all of those
now note here because i did my import as
m
i didn't point to this earlier m is now
available so these are things that i can
do stuff with whether it's a function or
a variable
so since i've brought a module in under
specific name then that's how it
how the script essentially sees it
and so i can also do
i can do
let's change this around a little bit
uh so let's just comment that out for
now now i can also do
uh import add as
uh let's say a
multiply as
mu
actually let's do ad
and my name
as
mn
and now i could change it to a d so i
can change also not only my
module names
but i can also change
my uh functions
and so
that's uh
all right let's see i'll just do that
i'll do that twice
so now instead of that it's going to be
mn
so i can do that and now i can see up
here
here's my short names
you see that that called my name in both
those cases
now an interesting thing that we could
do is i could say i'm going to take m
i'm going to do this
and this shows me in
pycharm what i just did because i did
multiply as m
then i'm going to have some stuff break
because m
was imported as a module and now i've
overwritten it
and so let's see what happens
boom right here
oh that was mu so let's just do m
and so now
when i come down here when i do the m
dot my name
it doesn't exist anymore because the m
that i imported
is uh
oh i don't i didn't get that far into it
to be able to print the name
but
i was uh so i could do this let me move
this up a little bit
and so here
i can see uh let me put up
something right there just to show
uh let's see
what is
m
so here what is m is multiply so m is
this guy
but if i call him mu
then
when i do what is it what is m now it's
my mod
so you have to watch out it's not
necessarily going to let you know when
you overwrite stuff and it's note it's
top to bottom so whatever you set last
is the one it's gonna take
so you can get into um you know some
issues with that
now one other thing with uh with modules
that we need to be aware of is the idea
of
packages and so now what i did is i've
got a package called samples which is
just a folder
that has this init py
and then it's got this my mod now
this my mod has add two this is
different from
uh let me do it like this
this is different from let's do this
this way as well from the one that we
used
so
which is where you could run into stuff
so now i can do
import
samples dot my mod
as m
and it's going to blow up in a bunch of
places because now it actually has
um wait
yeah so m dot my name doesn't exist
but
my name too does
so let me do my name too first
nice maple open a couple places boom
boom boom uh no it made it so my mod too
but then when it tried to call my name
because it's not my mod it's samples not
my mind so i could do
samples.mymod as sm
and then i could do my mods
my mind as
whoops
as m
and now
i'm specifying which package i'm working
with and boom everything works
now this init
is going to be something that you can
throw out for anything within that
package so if i wanted to move uh
my name to
let's say well let's leave that one but
let's say that within here
i'm just going to do my name equals
samples package
and then come in here and do
well actually let's just do this i'm
going to keep it very simple
i'm going to do a samples package
a knit fired
and let's see where that comes up
that's probably easier to see what goes
on
so notice this is right at the beginning
when i do the import it kicks off that
value it kicks off that script
and i can do things um let's see it
would not be
well let's try this let's do it this way
so i'm going to define
uh if i go here and i define
uh
let's just call it me
i don't give it anything and then it's
just gonna do print me
me
uh let's see
oh
that's what it doesn't like
okay so if i do define me
and now come into my script and do a dir
let's see what happens
[Music]
and so we see here
that it's not available that me did not
show up
however let's look at
[Music]
let's try samples not me
and see what it does oh i'm sorry not
samples
sm
dot
and so i can't exit i can't access it
here either we're gonna find out or can
i
boom
not uh yeah samples my mod has no
attribute
because here although i've executed it
uh for any package
i'm gonna see this so let me uh let's go
ahead and duplicate this so my mod two
so it's going to be the same thing but
just to show you i can go let's go back
to net uh well that'll stay the same
because i don't really need to do
anything with him
but if i go to whoops if i go to modules
and now i do my mod 2
ssm2
then let's go look at here
whoops i don't want to do i don't want
to call this because that's just going
to break it well
and now we can see
uh let's see
it's
fired and so it's only fired once we
don't see it fired a second time
it's the first time i hit anything in
this package it's gonna do
stuff
what is this stuff i don't know it could
vary there's probably some reasons to
have packages where you may want to
uh maybe log that this is being used or
something like that
or there may be some additional setup
i don't know maybe some resources you
want to bring in things like that
it may bring in some additional
imports so you could have these packages
all need certain things available
that it would need to bring that in so i
could do
uh
now i'm gonna get too complicated at
that because then we'll start getting
pretty long into this but
it's really the important thing
is to understand the different ways that
you can bring in modules
packages uh the only way in
this init underscore underscore and knit
underscore underscore
that has to be in a folder for it to be
a package
if i delete this file
let's just take that and
i'm just going to rename it to
not in it
uh yes do the refactor and so now when i
do that
this thing's going to totally blow up
because samples
oh no i did take it
okay i did allow for that
used to be you could you were not
supposed to have that let me see if i
delete it let's just make
sure uh do refactor there we go
[Music]
okay so we are good it used to be that
you had to have that init that was how
you noted that it was a package but
apparently now it's gotten a point where
you just need a folder
and you can refer to that folder as
needed you can use the init
as a way to as i said to do some
additional
work once you're getting into that
package
i think that's good enough for now
we will wrap this one up
and as always the uh the code will be
out in the repository you can see links
in the show notes take a look at those
we are we're getting there we're i don't
know about 75 80 of the way through this
at this point
and uh a couple things left that are
sort of
uh common things like we're gonna deal
some file i o and stuff like that so
we're getting there
just stick with it and before you know
it we will be to a point where we can go
jump out there and take a shot at
getting a python certification
as always go out there have yourself a
great day a great week and we will talk
to you
next time
you