Welcome back.
We just learned about module's, 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.
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 carte
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.
You see that I get an underlining says imported but unused.
OK, so does that mean I can use it?
Let's see, 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.
Doctor 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 paid cash that was created.
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, replug it.
Let's actually create our own project.
I'm going to use Peja and I recommend you use pajamas.
Well, just for these next couple of videos.
Let's open up Iran and let's just create a new project here.
Now we can call it whatever we want, so let's just call it module's for now for our project, he 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 env 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 and use our 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 module's.
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 we can just call it, let's say, made dot by.
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.
And right away, you see that it 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, DCPI
So shopping.
Cart that 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 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 Reppel, except this time we're actually using
an ID.
Now, the reason I wanted to transfer over to the ID is that there's an extra file here that we didn't
see in the Reppel.
And the Reppel actually hides this away from us.
You see this underscore in it DCPI 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 in it, underscore, underscore DCPI.
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 Reppel 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 charts 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 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 in there, let's zoom in here.
And we see that model 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 by.
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 zip
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 are set up, 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 added file and we have different modules in them.
And as projects get bigger and bigger and we use IDs like petroleum, 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.
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 module's a little bit more in the next video.