Let's learn about different ways that we can actually import these modules.
Up until now we've only learned about import.
But sometimes things can get pretty hectic.
For example we can actually have a another package inside of another package if for example I create
a new folder here and say new Python package.
We'll call this more shopping click Okay.
You see that I've shaded a nother package inside of a package that has its own in it file and let's
move this now to let's do refactor move and we'll move it inside of the more shopping folder shall say
more if I click Okay.
You'll see that I've moved this module over here so I have a package inside of a package and the way
we would access this is to say simply shopping dot more shopping dot shopping cart and let's copy that
over and do the same here and now when I click run you'll see that it still works.
But this is getting a little bit too crazy you can imagine as we have more and more packages that we
constantly have to this dot this dot and this is getting a little too long and ugly.
So how can we better this.
Well one way of doing this in a cleaner fashion is to do something like this instead of saying import
we say from this part I want to import the function.
So here I'm saying hey from package name package name module import whatever function I want in our
case we want to import these by function.
So you see here I can just do that.
And in my code I can just run by as if it was part of this main file if I run this you'll see that it
still works awesome.
So that's definitely cleaner and we can do this with utility as well.
I can say from utility import and actually import the two files or the two functions.
So that is multiply.
And if we want another function we can just add a comma and say divide.
So now we can print Apple or by Apple as well as divide let's say 5 by two and we can also multiply
and make sure it's a comma here because it is a function like this so that if I run this you'll see
that there we go.
This works as well.
So this is a really nice way to clean up your code.
You can also do something like this where you say from package name package name and then import the
entire module.
So let's say I want to import these shopping cart module.
I can say import so package dot package or you could just hand have done one package and import the
actual module shopping cart.
So now I would access it like this shopping cart dot by and this is going to work as well.
Now the reason you might want to do this instead of what we had before is that sometimes you can get
name collisions let me show you.
Let's say I had a print and then in here we use the max function which gives us it's a built in function.
We give it an iterator and it finds the max for us so that if I run this you'll see that the max in
this list is three well imagine in our utility function we have a let's say instead of divide or we
add a max that simply says returns and loops Well if I go back to my main dot pi
get max as my function and I click Run well you see I get an error I get Max takes zero positional arguments
but was given but one was given because now Max does a mean what we thought we meant at least in this
file we've imported Max.
And while Max has a whole different function now so we can actually override existing functions.
So by using something like this shopping cart we make sure that we have only one case that we might
have named collision but all the properties or all the methods that shopping art has are connected to
shopping cart.
So instead of having multiple functions that could be overwritten I only have one namespace and by the
way this is also one of the reasons that I don't recommend doing something like this.
You can actually do import star and what this says is Hey from the utility module import everything.
And when you do that you'll see that once again we get an error because it imports the max as well.
And because we don't even know what's being imported right now we can't see it.
We have to go to the file to see it when we get an error like this.
We're gonna be like what's going on.
I thought Max is working and it's because of this line.
So it's always good to be explicit and say exactly what you want to import but let's remove Max for
now.
But as you can see modules can be important in different ways.
My recommendation is to always be explicit and say exactly what you want to import and the format usually
follows either using the import statement followed by the package dock module or import the module or
you can do the from statement to give it package sub package and module if you want or you can do module
and then saying import and the specific function or the actual module itself many ways of doing it depending
on your need.
The key takeaway here is that modules and packages help us have good engineering practices and build
big projects in an organized fashion yet still have all these files talk to each other right.
That was a lot.
Let's learn more in the next video.