Welcome back.
We just learned about modules but let's say our project is getting even bigger.
We have this main file.
We have some utility.
But let's say that we're now working for Microsoft and we have a new let's say shopping functionality
an entire let's say shopping area where you can buy Microsoft T-shirts.
Well we can now create a folder so we can add folders to our project and inside of this folder.
Let's say we have a new file and we'll call this the shopping cart.
Dot pi.
So now this shopping cart dot pi is a folder in our project.
You see that our project is getting bigger and bigger.
And let's say that here we just have a function called by and we give it an item and let's say here
we we have a cart variable that let's say adds.
It's not a very sophisticated buying system but for our example it should be fine.
And then we just return the cart now.
In here we have this by function and let's say in our main file.
So up here we want to also import our shopping cart by function.
How can we do that well if we tried to do shopping cart like this.
Remember that's the name of our file.
You see that I get an underline it says imported but unused.
OK.
So does that mean I can use it.
Let's see.
Most say shopping cart and if I click Run module not found error no module named shopping cart.
You see what we've created here is what we call a package a package is simply a folder and we learned
about module which is these files these Python files a package is a level up a package is a folder containing
modules so you can have a package with multiple modules inside of them.
So this shopping cart is a module but this shopping is a package so the way it works when we import
from a package is we say the package name.
So what's the package name it's shopping.
And then we do a dot so dot shopping cart so package name and then the module name.
Now if we do this let's see if this works.
Let's say I do shopping dot shopping cart.
Now if I print this let's make this a little bit smaller
All right.
Now I have access to the shopping cart module by importing the package shopping which is the folder
and in the shopping cart a you notice once again we have a PI cache that was created for the shopping
cart so let's see if we can use the BI function.
So I'm going to say bye and then item I'll say I'm buying it just an apple if I click Run.
There you go.
I get my apple list and I was able to imported from a package.
Now here's the thing I want to try and copy what we've just done into a real life scenario instead of
using this Web site old Dot I.T. let's actually create our own project.
I'm going to use pi Char and I recommend you use pi chairman's Well just for these next couple of various
let's open up by picture and let's just create a new project here.
Now we can call it whatever we want.
So let's just call it modules for now.
For our project you can see where it's being saved.
And remember here we get to pick what interpreter we want.
We're going to talk about this virtual and later on in the videos.
But the key thing is to make sure that our base interpreter is using Python 3.
In my case the Python 3 that we've downloaded and user user local.
If you're on a Mac or Linux if you're on Windows this path will probably be different.
Now we can just create a project
and we see here we have our modules project.
Now we're going to ignore all these folders for now we're going to talk about them later on.
The key here is that we want to create a new file.
I'm going just right click here and say new
and we can just call it let's say main dot pi and notice here that I created in a folder called V and
V.
We'll get back to that shortly.
Now let's make this a little bit bigger so we can see and we're going to copy whatever we've just done
here.
So I'm going to copy
the main file like this that did not copy.
Let's try that again
like that and then we're also going to create a few more files so we'll create a folder so in here.
I'm just going to say right click.
Say New in right away.
You see that it says Python package.
I'm going to click on Python package or I can create a director if I want as well and when I click here
let's name the package like we've done here as shopping all right.
We get our shopping folder and in here let's right click.
Say new new file and we will call this the shopping cart dot pie.
So shopping cart dot by.
And then finally we'll add our utility file.
You know we're doing a lot but hey I'm trying to prove a point here.
Trust me this is gonna be important.
So utility dot pie.
There we go.
So we have the same setup that we have here.
Let me copy this over as well.
All right.
And we copy let's make this bigger like that right.
So we have our files just like we have in our Repsol except this time we're actually using an IDC.
Now the reason I wanted to transfer over to the IDC is that there's an extra file here that we didn't
see in the repo and the repo actually hides this away from us.
You see this underscore in it dot pi far and it's completely empty.
There's nothing in here.
One of the rules of a package of a python package is that on the root of this package you have to have
an underscore underscore in it underscore underscore dot pi.
Now why is that well because the interpreter is going to read this and say oh this is a python package.
Remember how when I right click here I said new and then I had an option of a directory or a python
package by clicking Python package it automatically added this in it for us.
So although here everything worked without a net.
This is just something that reptile hides underneath the hood.
Anytime you want to create a package you'll see that we need to have an Internet file and it can just
be completely empty now.
The beauty here with pie chart is that it does it for you which is really really nice OK.
So let's run this code and make sure that it still works.
So this is our main dot pie.
I can simply run the code here by going to run and then selecting run let's run our main file and we
get an error.
Let's zoom in here
and we see that module shopping does a shopping cart has no attribute buy and that is because we did
not import the BI function here.
So let's do that.
Let's go to shopping cart buy.
Make sure I copy that paste it in let's zoom that in.
And by the way I'm zooming here on my touchpad I can just use two fingers to zoom in here if I click
run again and this time once you ran it once you can just click on run again and there we have it.
We have apple right here as the output.
And it says process finished with exit code 0 in programming when something exits with code 0 it means
there were no errors.
If you get one.
Well that's usually an error.
All right so we have everything working.
And this is now our setup.
We have our main file.
We have our other modules like utility and then we also have our packages and their packages because
they have this init file and we have different modules into and as projects get bigger and bigger and
we use ideas like Pi charm we organize our functionality our grouping into these packages and modules
and really really good developers are able to organize code in really nice packages that makes sense
really nice modules that make sense.
And this is a skill that is really hard to teach.
It just comes with practice by looking at other people's projects by working on projects.
But the key thing is that you want to do what makes sense and when we start working on some of our final
projects we have over 10 projects coming up that we're going to work on now with those ones.
You're going to start to learn different ways to organize code as you follow along.
So don't worry.
Let's take a break and explore modules a little bit more in the next video.