>> Let's take a look at how we can use
modules and install packages,
so up here what I have is
a little module that I'm going to use as my sample.
So what you'll notice is I've got
a little function that I'm calling display,
it takes two parameters,
message, which is whatever we're going to print out,
and then an is warning,
which is just a flag,
we're going to set that to False by default.
Then what you'll notice is that if it is a warning,
we'll just print out, 'This is a warning',
and if it's not, then we'll just not print that,
in either case we will print out the message.
So I now want to use that from my demo here,
so what I can do is I can say,
"Import helpers" and now that I've done that,
now I have that little display that we just saw available to me.
Let me actually just put that side by
side so that way we can see that,
let me hit the right button,
there we go, cool.
So now, let me go ahead and start using that,
so what I could do is I could say "Helpers.display,
and then sample message."
So now, if I run my little
demos.py and make my IntelliSense go away,
there we go, Python and then my demos.py,
and sure enough we've got our little sample message.
If I would have passed in True,
it puts it out onto the next line there,
let me just readjust,
there we go and rerun this.
Now, you'll notice that it prints out 'This is a warning',
and my sample message,
so just like that,
I can import in modules, that's all there is to it.
Now, if I wanted to always make display available
without having to use that little bit of helpers.syntax,
then I could say "From helpers import',
and if I wanted everything,
I could use the asterisk or if I just wanted that display,
then I could just simply say, "Import display."
Now I can say,
"Display sample message," and now if I rerun this,
now what you're going to notice is that sample message is
showing up twice because I've called display twice,
and it's the exact same thing,
the only difference is simply how we did the import.
Right there is the exact sweet spot
without it putting in line breaks,
so that's how I could go in and do that.
But now, let's take a look at how we could set-up
a virtual environment and install a package in to there.
So to set-up my virtual environment,
let me clear my screen here,
the way that I do this is by saying,
"Python take m, venv for virtual environment,
and then I'm actually just going to say venv second time."
Now that second parameter there,
this one right here,
that is the name of the folder that I'm creating.
So when you look at that syntax, you might be thinking,
"Well, why am I typing venv twice?"
The second one is the name of the folder,
the first is the command,
you're going to notice most people will either just do venv for
virtual environment or sometimes they'll just do
env for just environment.
I really like that venv,
so that's what I'm going to stick with,
and I'll hit "Enter",
and you'll notice that VS Code then pops up and says,
"Hey, we've noticed a new virtual environment has been created.
Do you want to select it for the workspace folder?"
Now, what this is going to do,
is this is going to tell VS Code about
all of the packages that I'm going to be using,
it's going to look at that folder for all of those packages.
What's the impact to ask?
Well, the impact is going to be that now VS Code can help me out,
VS Code is now going to know everything that's there,
and will be able to show me the IntelliSense.
You almost always want to click "Yes" on this,
because I'm going to want that help from VS Code,
so I'm going to click "Yes",
and now, it set that up inside of VS Code.
But I still want to set that up inside of
my local execution environment,
and the way that I'll do that is by saying,
"Current directory and then venv scripts, and then activate.ps1."
Now, the way that I know that I'm using
my virtual environment is right here at the very beginning,
you'll notice that it's showing me
the name of the virtual environment.
If you see that,
you are now using that virtual environment,
if you don't see that,
you're not using the virtual environment.
Now that's for my code execution,
how do I know if VS Code is using the right thing?
Well, I can actually see that down here.
Let me actually zoom in on that,
hopefully make that a little bit easier to read.
So down here on the lower left-hand corner,
you'll notice right here,
this is where it's telling me the environment that it's using.
So I want to see my venv there,
that's the name of my virtual environment,
now I know that VS Code is using it.
Again, the impact here is
that VS Code will now be able to help me out,
so when we install Colorama,
which we'll do in a moment here,
it's now going to know that inside of VS Code,
and I'll get my IntelliSense.
So let me now do my installation,
so we'll say "Pip install, and then Colorama."
Now, by doing it that way,
it's just going to install Colorama one time, and that's it.
Typically the way that you'll want to do this,
is by using a requirements.txt file,
so I'm just going to create a brand new file,
just going to call it requirements.txt,
no magic, it's just a text file, that's it.
It's in my current directory,
just a text file,
and I'm going to type out "Colorama".
Now, I'll go ahead and say,
"Pip install and then take r for requirements file,
requirements.txt," and now I want to do my installation.
Now, it's going to install it into
the virtual environment, how do I know that?
Well, because I've activated it,
and because one more time,
I'm seeing that little venv right there at the beginning.
Now, I'm getting a little error message here
telling me that Pip needs to be upgraded,
you'll probably get that like half the time that you run Pip,
I swear, and that's fine,
you can go ahead and upgrade it,
I'm not going to worry about it for right now,
I've now got Colorama installed.
Let's put Colorama to use then,
so I'm going to put that to use inside of my
helpers.py because printing out this as a warning,
that's kind of cheesy,
let's change colors here.
So I'm going to say "From Colorama",
and I'm going to say,
"Import", and then we'll say, "Init",
which is what I'm going to need for Colorama,
and I'm going to need my foreground colors,
which are going to be like that.
Why are you giving me green squiggles?
Colorama, there we go.
So now, let me go ahead and update this,
so that if it is a warning we'll print it in red,
and if it's not a warning
then we'll just print it in the normal color.
So we'll go ahead and say "Else",
and then we'll just print message,
and it will print the normal messages in green.
The way that I do that is, I
need to make sure that my casing is correct, there we go.
So now I will say "Fore.BLUE",
and then our message,
otherwise we'll go ahead and say,
"Fore.RED", and then our message.
Now, if you're curious as to how I know to do that
even though I got my casing wrong on Fore,
the way that I knew to do that is because
I've worked with Colorama in the past.
Again, if you're ever looking to do something,
just search the Interwebs,
honestly that's how I discovered Colorama.
I went, "Hey, I want to be able to display
different colors when I print," I did a search,
found Colorama, and here we are,
that's exactly what I did.
So now, I've got my little red if it's a warning,
blue if it's not, we're good,
let's go ahead and save that.
If we come back over here to our demos,
what we'll notice is onetime we said True,
so we should see that in red,
the other time we didn't do anything,
we should see that in blue, let's run it.
So I'll say, "Python and then our little demo," hit "Enter",
and sure enough our warning is in red,
our non-warning is in blue.
So what we saw in this demo and what we saw in this module,
is how we can work with our own modules,
and how we can work with external packages,
and how we can set-up a virtual environment.
Pretty much every application that you're
ever going to be creating,
is going to have some level of
requirement for installing packages.
That if you're going to be using Cognitive Services,
if you're going to be using Flask,
if you're going to be using Django,
you're going to be installing all of those various packages.
That's how we do it, set-up a virtual environment,
set-up a requirements text file,
list the packages that you want,
and then it's simply "Pip install" from there.
That is how we work with packages and modules in Python.