Let's talk about modules.
So in Python, there's these two similar but kind of different
concepts. There's this one called a module, and there's one called a package.
In the last few lessons, we were talking about packages.
So now let's talk about modules.
So a module is really just a different '.py' file.
So if you've got another '.py' file, and you're importing anything
from it, that's called a module.
And a package is usually a whole system of files.
It's a whole bunch of modules, or a whole bunch of Python
files put together,
and you can usually install a package with 'pip install', and
then the package name, 'packagename'.
But a module can simply be one file or a few files.
And the idea behind a module is really just to separate your
code into smaller files instead of having one massive Python
file. So we've seen some code in our Jupyter Notebook, and
that code really didn't get too long.
But in real life, you might end up having a file that has
a 1000, 2000, 14000 lines of code in it.
And if you ever see something like that, that is a really
good candidate for pulling out some of that code and putting it
into another file.
So let's go ahead and make a little example here.
I'm going to create a new file, and I'm going to save this
as 'custom_module.py',
and I'm going to put that up just on my Desktop here,
'custom_module.py'.
And I'm going to create a new function.
So 'def my_greeting():', I guess.
And it's just going to say, 'print("Hello from custom_module.
[no audio]
Now I'm going to create another file, but this one is
going to be called 'my_program.py'. 'my_program.py'.
And let's go ahead and oops, there is no folder in there.
So let's hide that.
And let's go ahead and reopen that other file.
So we have 'custom_module' and 'my_program.py' in here.
And let's label these as well, '#custom module',
and this one is, I'll delete that, '#my program'.
So because these live side by side, what I can do is import
one from the other.
So in my program, I can say 'from custom_module import',
and then I can import this particular function, and then I can
run it in here.
'my_greeting()', run.
Now let's go into our terminal here, and I'm going to 'cd'
into my Desktop, because that's where my files are.
And if I do 'ls -la', we'll see we've got 'my_program' from a previous
video, we also have 'custom_module', and 'my_program'.
So let's go ahead and run 'my_program'. 'python
my_program.py', and it says, "Hello from custom_module.py".
So 'my_program' actually doesn't have a 'print' statement in here at all.
What it's saying is from the 'custom_module' import this particular
function. Look at that, 'custom_module.py'
actually has this function, and it ran the 'print' statement.
And that's all there is to making a module. It's really, you're just
moving one bit of Python to another file.
Now, I would like you to give this a shot. Create two Python
files, and import a function from one of those, and run it
in a different file. So you can even copy what I have, 'my_program.py',
and 'custom_module.py',
and you can just have 'my_greeting' in there.
Go ahead and 'import my_greeting', and then execute it.
And again, there's no 'print' statement in here.
It is running the code from this file.
It's just moving it into here, and executing it.
Go ahead and give that a shot.
And once you are done with that, let's head on over to the
next lesson where we are going to create a package.