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 pie.
So now this shopping cart pie.
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 buy 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 try 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.
Okay.
So does that mean I can use it?
Let's see.
I'm going to 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 then the shopping cart.
And you notice once again, we have a pie cache that was created.
For.
The shopping cart.
So let's see if we can use the buy function.
So I'm going to say buy and then item.
I'll say I'm buying just an apple.
If I click Run.
There you go.
I get my apple list and I was able to import it 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 website, replace it.
Let's actually create our own project.
I'm going to use pie chart and I recommend you use pie charm as well just for these next couple of videos.
Let's open up pitcher 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 EMF later on in the videos.
But the key thing is to make sure that our base interpreter is using Python three.
In my case, the Python three that we've downloaded in user local bin.
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 of 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 to just right click here.
And say new.
Python file.
And we can just call it, let's say Maine dot pie.
And notice here that I created in a folder called V, E 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.
And right away you see that it says Python package.
I'm going to click on Python package or I can create a directory 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'll call this the shopping cart pie.
So shopping.
Cart dot by.
And then finally we'll add our utility file.
I know we're doing a lot, but hey, I'm trying to prove a point here.
Trust me, this is going to 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.
All right, so we have our files just like we have in our ripple.
Except this time we're actually using an ID.
Now, the reason I wanted to do transfer over to the ID is that there's an extra file here that we didn't
see in the ripple.
And the ripple actually hides this away from us.
You see this underscore in it py file 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 init underscore underscore py.
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 clicked 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 init, this is just something that Ripoll hides underneath
the hood.
Any time you want to create a package, you'll see that we need to have an init 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.
Okay, so let's run this code and make sure that it still works.
So this is our main pie.
I can simply run the code here by going to run and then selecting run.
Let's run our main file.
Oh, and we get an error.
Let's zoom in here.
And we see that module shopping.
The shopping cart has no attribute buy and that is because we did not import the buy 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 run 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 zero in programming.
When something exits with code zero, 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 in them.
And as projects get bigger and bigger and we use IDs 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 make sense, really nice modules that make sense.
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 ten 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 models a little bit more in the next video.