>> In the last module, Susan introduced how you can create
functions which give you a level of reuse and readability.
Now, let's say that you decided to
take a handful of functions that you've created
and now you want to be able to reuse them
inside of your current application or
maybe even in different applications
that you're going to be creating.
Well, the way that we do that is by utilizing modules
and how we can import them into separate projects,
is through the use of packages.
Well, let's start off by taking a look at
how to create a module on our own.
So if you take a look at the little slide here,
what you'll notice is that we're introducing
the concept of a module.
In a module, really is nothing
but a Python file with some functions,
maybe some classes or other
components that you're going to be able to reuse.
Why set them up?
Well, because it gives you the opportunity to
break down your code into
that reusable module structure and
helps make things a bit more readable as well.
Now to create a module,
it's actually pretty easy.
All you need to do is just create a file.
In my case, I'm naming it helpers.py,
and then add in your appropriate code.
So if you take a look at the function that's here,
there's actually nothing that's special about it.
Susan showed you how to do something
exactly like this in the prior module.
That's really all there is to creating a module.
Now, when it comes time to use a module,
then what we need to do is we need to import it.
Now, there's a lot going on on this slide.
But the big thing that I want to
highlight here is the fact that I'm
actually doing the exact same thing.
I'm just doing it three different ways.
Depending on how you do it,
it'll have a slightly different impact on your code.
There is however, no performance difference, whatsoever.
It really is simply a matter of how we're going to
gain access to whatever happens to be inside of our module.
So you'll notice here if I grab my highlighter,
that that first little section here I'm going
to import in helpers.
Helpers is the name of that py file that we created.
If you remember, there was a display function inside of that.
So in order for me to now call that display,
now that I've imported in helpers this way,
I simply say helpers and then.display.
So what the above line of code did
is it automatically pulled in all of
the functions and made it available inside of a little collection,
if you will, or inside of what's known as
a namespace called helpers.
So anytime I want something that's going to be inside of there,
I type in the name of the namespace,
in my case, helpers.
and then the name of the function that I want.
Now, let's say that I want everything that's inside of
there and I don't always want to type in helpers.
Well, the way that I do that is
by just modifying my from statement.
So this serves my import statement.
So this time, what I'm going to do is I'm going
to say from helpers.
So that's going to be the name of the module this time.
So from that module, what do I want?
Well, I want to import
in everything and that's what that little star means.
Now, once I import in everything,
then everything that's inside that module
will become globally available,
or in a slightly more technical manner,
it will then be imported into the current namespace.
What's the impact?
Well, the impact is now when I want to access display,
all I have to do is just simply say display.
Let me clear the ink from the slide here,
and just highlight one more time here the little difference.
What I want you to notice here- oops,
we go back to the right little section here. There we go.
Pen. Let me go ahead and highlight
this little block right here and this little block right here.
The only difference between them is whether or
not we're using that helpers prefix.
I want to stress this one more time.
There's no logical impact on your code,
there's no performance impact on your code,
the only difference is literally just how we're going to call it.
A lot of times,
it's just going to come down to a matter of personal preference.
Sometimes, it can make your code a little bit more readable,
knowing where a particular function is coming from.
Sometimes, I just want everything right there
so I don't always have to type out that prefix.
Again, the choice is yours.
The last import statement that we have up here is where we're
going to import in just a specific item from a module.
This is very similar to what we saw up above.
The only difference is now instead
of grabbing everything by using that asterisk,
we're just grabbing one item and that we're doing
by using that import and then just the one item.
Are you going to let me turn you off? You're not.
Okay. Yeah, that's all right.
Down to the very bottom, you can see me calling the display.
I know the slide is showing
the little stars that are sort of on the
how exactly how readable it is.
But it is actually
the exact same thing that we're seeing right up here.
So it actually winds up being the exact same code.
Now, what's nice about just importing in
individual items is it can help clean up your current namespace,
that one of the little challenges that you'll run into,
if you bring in everything from a large module,
is it can start to clutter your IntelliSense,
so that every key that you start to type in,
gives you just a huge list of options.
So if you know that you only need like one,
or two, or three items from there,
quite frequently, it's best to just import those individual items.
Now, for me personally,
I typically trend towards that last bit of syntax there,
where I'll just go in and grab the individual items that I need.
But again, I want to stress,
it's frequently a matter of personal preference.
About the only time that it's actually going to matter is if you
might have two items with the exact same name.
Now, this is a module that we've created,
but how about getting something that somebody else has created?
Well, this is where a package comes into play.
A package is really just a published collection of modules.
That's all there is to it.
Now, the number one question that I get,
whenever I start talking about packages is,
how do I know what's available?
The answer really is just through experience and through use.
Now, if you actually want to start searching around,
there is a full index available of packages called the
Python Package Index and you can go in and search inside of there.
You could also go in and do an Internet search.
Honestly, if I'm about to do
something that I think somebody else has done,
I will always start by searching on the Internet.
That's quite common because chances are,
whatever it is that you're building,
somebody else has already built it or
maybe they've built individual components.
They just haven't put it together the
same way that you're going to be putting it together.
Reuse somebody else's code.
The best code in the world is the code that's already written.
So don't be afraid at all to go grab packages that
are out there and import them into your application.
Now, to install them,
all that you need to do is to use pip.
So pip is the command line installer that you're going to use.
So in my case, I'm saying pip install and then colorama.
Colorama being a little package that will help
you easily change the color of text when you do your print.
Now, if you happen to have a whole list of
packages that you want to be able to install,
then what you can do is set them up inside of
your a little text file that's frequently called requirements.txt.
Let me get back to the right slide here. There we go.
Let me bring that back up.
There we go. So my requirements.txt, what's inside of it?
Well, it's actually pretty straightforward.
It's simply a text file.
It's just simply a list of
all of the packages that you're going to be using.
Now, if you want to pin down to
specific versions and things like that,
there is syntax and I'll let you go check the docs for that.
This is actually going to install the most up-to-date version.
Sometimes that can be exactly what you're looking for,
sometimes that can potentially lead to problems,
and you'll want to be aware of those possibilities.
So if you're looking for, hey,
how do I specify a particular version and so forth?
You can go check the docs.
You're going to find that quite frequently though.
All that you need is just the most recent version and that's
exactly what that syntax is going to get for you.
Okay. This is a perfect time to take a real quick break.
Then what I want to do when we come back is
talk a little bit about environments,
because pip is going to behave in a way especially if you're
familiar with Node that you might not quite be expecting,
and we'll take a look at that in the next video.