Up to this point we've learned about a few ways to organize our Python code.
The first thing we did.
Well we just wrote Python in a file and that organizes it right.
Our entire code has been so far on one single file.
So that was one way that we organized our code.
Next we learned about functions and functions were used as a way for us to not write code that we repeat
over and over.
We could write functions that we can use anywhere we want in our file so that we can call these functions
over and over and we can name the functions appropriately so that our code makes sense packages it nicely
for us to use all of these things were meant for us to keep our code clean make our code more maintainable
thus keeping our code organized because as you write more lines of code it gets harder and harder to
keep everything in your head.
If other people join your team to work on your project they need an easy way to figure out how things
are working.
But we still have a problem and that problem is that everything is contained in this one file all the
functions that we've been writing has been on just one dot pi file but in real life this isn't the case.
We have a lot more code and we can't just have one file with millions line of code.
So how do we stay organized
if we had multiple files of python like this.
Because our project is getting bigger and bigger.
Would this work.
Is there a way for us to link all of these files together.
As a matter of fact there is and this is very common practice all across industry and we call this way
of organizing code modules and modules are simply well these files.
Each one of these files each dot pi file is a module and by building these modules kind of like we built
different functions kind of like we built different classes because inside of these files we can have
classes we can have functions but we can also have different files so a layer higher to divide up our
code and in this section we're going to learn all about modules and try to master it and see how we
can write organized code let's have a look you can see over here if I click on files in our rebel we
have a main dot Pi.
And every time I click run this fall guy run.
But what if we have more files.
What if I add another file here and we'll call it utility dot pi so based on what I just tell you these
two things are what their modules right mean dot pi is a module you tilted up pi is a module and when
writing modules we use the same principles as variables in python which is snake case.
That is if there are spaces we use underscore everything is lowercase letter so how do we divide up
these modules.
And this comes with practice when you're just beginning.
It's hard to say Hey what goes into what file what goes into what file.
But it's similar to classes right.
Similar to functions we want to divide up our code into chunks that make sense again.
Let's say we're working at Netflix.
We might have a file dedicated to videos.
We might have another file or module dedicated to the logging of a user maybe another file dedicated
to analytics and figuring out recommendations for users.
We want to group these classes and functions together inside of a file that makes sense.
Now one of the most common ways to use a module is something like a utility.
Let's say we want to create a utility module that is a module that has a lot of what kind of like a
tool belt.
Very simple functions that we can use all across our project because we can imagine here having multiple
files.
So let's create a very simple example.
I'm going to say a multiply function exists here and this takes a number one and number two and in here
we're simply going to return num 1 times num to a very simple function.
And once again we'll also have a divide function.
And yes this is kind of useless because we can just use the plus or to divide operant in python.
But stick with me here and trying to show you an example.
All right.
So number two I'm going to return num 1 divided by num too.
Nice and simple.
So this is our utility module that has all these functions for us now.
Notice here that we're actually not doing anything in this file.
I'm simply defining functions but I'm not running any of the functions but let's say we have this utility
module and I want to use it in my main dot pi file.
How would we go about doing this well the way we communicate between these two files is quite simple.
All we need to do is use the import command we import and then give it the file name that we want to
import.
What do we want to import.
We want to import utility just like that.
And notice that I'm not adding the DOT PI because we assume that whatever we're going to import is going
to be a python file.
So we don't need to do that just the name just like that.
Now let's print utility here if I run this
Whoa.
Do you see that we have module utility from and then this is a file path that this Web site generates
for us.
So it doesn't really matter.
Now there's a few things that happened here.
One is that we generated this PI cache folder underscore underscore pi cache and oh boy what is this
gibberish hmm mm hmm.
This is this is pretty confusing right.
Well this PI cash is created every time we run a file with let's say enforcements.
So when we're using modules you see what pi cash does is when we click Run the interpreter is going
to create this PI cache folder and it's going to say hey I'm running this file this one main dot pi
file.
Anything that made that pi file imports let's say utility in our case I'm going to cash it.
So see how here it says Dot P Y C.
This is because it's using the C Python interpreter.
Remember this is the C Python which is the interpreter written and c.
So this is actually a compiled file so that next time I click Run here on my main dot pi nothing changes
because what's going to happen is instead of loading up utility dot pi it's going to load up this compiled
version of utility because nothing has changed and utility dot pi and this makes things faster.
When I tried to run main dot pi again that's what caching is caching is hey I'm going to remember this
and this is the compile version so I don't have to go through the compilation step again but notice
that the main dot pi file does not get compiled because while we run it every time.
Now if we change the utility function to not have the divide in there I click Run and you don't notice
it here.
But this has now been rerun because we have a different file.
You see that we have multiply here but not divide.
So let's bring back divide again go back to here click Run
and there you go.
We have divide here to now but this is something that we don't touch.
This just comes underneath the hood with any editor that we use.
And in a few videos we're going to show you how this also happens when you use something like pie chart.
This is just something that editors allow us to do just so our programs can run faster.
So I now have access let's make this a little bit smaller.
I now have access to utility and here's the interesting part.
If I do dot here look at that I have access to divide and multiply so that I can multiply numbers let's
say 2 and 3 click right and I am able to use the utility module in my main file.
Very very cool.
If I do divide and I click Run I can use this as well.
Very very cool.
And I can import as many times as I want if I had a different file in here and I want to import another
thing.
Let's say there is a utility to file.
Well I can import that and import as many things as I want and use them across my files.
How cool is that.
So now we have a way to organize our code and have all these files communicate together because in real
life we have big projects and these big projects are going to be worked on by different teams different
programmers and we need a way to work with all these files.
And by using the import statements between modules we're able to use functionality just like we were
able to use the functionality that functions provided us between different files and most of the time
if you're working at a company the first couple lines on a dot pi file are usually import statements
because usually you're communicating with other files let's learn some more in the next video.