Lecturer: Welcome back.
We just learned about modules
but let's say our project is getting even bigger.
We have this main file you, 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 py.
So now this shopping cart dot py
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 buy
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 wanna also import our shopping cart buy 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 that says imported,
but unused. Okay, so does that mean I can use it?
Let's see, I'm gonna 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 pycache 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 wanna try and copy what we've just done
into a real life scenario.
Instead of using this website repl dot it
let's actually create our own project.
I'm going to use PyCharm
and I recommend you use PyCharm as well.
Just for these next couple of videos, let's open
up PyCharm 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 gonna talk about this virtualenv 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 these folders for now,
we're gonna talk about them later on.
The key here is that we wanna 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 main dot py.
And notice here that I created in a folder called V E N 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.
Oh, 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, we'll call this the shopping cart dot py.
So shopping cart dot py.
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 gonna be important.
So utility dot py.
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 repl except this time we're actually using an IDE.
Now, the reason I wanted to transfer over
to the IDE is that there's an extra file here
that we didn't see in the repl.
And the repl actually hides this away from us.
You see this underscore init dot 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 dot 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 init for us.
So although here everything worked without a init
this is just something that repl hides underneath the hood.
Anytime 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 PyCharm 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 dot py.
I can simply run the code here
by going to Run and then selecting Run.
Let's run our main file, Whoop, and we get an error.
Let's zoom in here.
And we see that module shopping dot 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 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 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 they're packages because they have this init file
and we have different modules in them.
And as projects get bigger and bigger
and we use IDEs like PyCharm, 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 wanna 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 gonna work on.
Now with those ones, you're gonna 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.