So we're importing the turtle module in order to use it in our code.
So I want to spend a few minutes just quickly talking about all the ways that we
can import modules.
[inaudible]
You've already seen the basic import and it's very simple.
We have the keyword import,
and then we have the module name that we want to import.
So if we had just imported our turtle module with the simple import,
then in order to create a new turtle,
we would have to say the module name and then the name of the class.
And we would create a new turtle like this.
Now this is,
perfectly fine, but it would make it so much more convenient
if we were using that turtle class a lot to write our code like this.
This way, we don't have to keep writing turtle.Turtle or turtle.
whatever else it is that we want to import. And this code is again
pretty simple. We have the keyword from, we have the keyword import,
we have the module name,
and also the thing in the module that we want to import.
As you've seen with this kind of syntax, we can simply write...
And this means that if we were creating a lot of turtles, let's say
instead of just creating tim, we also created tom
and terry.
Then we don't have to keep writing turtle.Turtle,
turtle.Turtle every single time.
So this from import is really helpful
if you're going to use this thing that you're importing a lot and you don't want
to keep writing the name of the module in front of it.
Now you can actually go one step further. Instead of saying
just from turtle import whatever it is you want,
you can actually import everything by using the asterix.
Now you can use everything that's in that module as if it were in the current
file. And this has advantages as well as disadvantages
because it can make it really hard to see where each of these classes or methods
come from. So for example, if I just wrote forward, you can see, I can,
I can do this.
But it's really confusing to just see this method somewhere in isolation
because it's like, well, what is moving forward? What's actually happening,
where does this come from?
And it's more obvious when you import a module like random.
So from random import everything,
and then somewhere else in our code,
we might just write something like choice.
And this is a method from random where we can pick a random item from a
sequence, like a list. Now this code works
but it's really confusing. Like, how is this choice working?
Where does it come from? Which module enables this capability? Instead,
we just have the method whack right in our code
and it's very confusing as to its origins.
So well, amongst the Python community,
it's very unusual that you'll see good code written like this.
I want you to know what it does because you might come across it in the wild,
just so you understand what it's actually doing,
but I want you to try and avoid writing code like this.
Instead if you're using something from a module many times,
so more than three times, then you can think about using this from import.
But if you're only using it once or twice,
then just import the whole module and write out this turtle.Turtle so that
you can see that this module is the one that contains this class and that is
imported and we're using it to create this object.
It's just a lot more expressive in terms of the code.
Now, the final thing I want to show you which is quite a useful thing,
is how to alias modules. So for example,
we can import our turtle as t.
So what this does is we import from the turtle module and we give that module an
alias name, so a name that we define.
And what this means is that if you were to create your new object from the
module turtle, instead of writing out turtle every single time,
you can just write t and that it will represent the entire module.
So sometimes you'll have modules which are really, really long.
So it could have a really long name and you don't want to type it out every
single time.
So you give it an alias name and it will be exactly the same as referring to the
entire name of the module.
And you can create a turtle like this. Now,
even though we've been writing import,
there are some modules that you can't just import, right?
So for example, if I wanted to import the heroes module
which is something that I can use to generate hero names like Decepticon or
Leopardon, or Askew-Tronics,
then I can use one of the functions that's in that module,
heroes, called generate.
But if I just go into my code and I straight up try to import this module
which is called heroes, then you can see I get an error.
And the error says, there's no module named heroes.
Why is it that I can't do this but I can import turtle?
Well, the reason is because turtle is a module that's packaged with the Python
standard library. And this is a small library of code
which contains just the basics to get you started like a core set when you buy a
board game or like the basic track pieces when you buy a set of hot wheels.
So you can imagine this library of code as like a family library,
easily accessible, but very small. Now,
if we wanted to access the whole world of Python modules and packages,
then we need to go to a much bigger library. And that is,
of course, the Python packages,
which are hosted on the internet and we can install into our project as
and when we need them. So this way,
our final project doesn't become gigantic because we've got all of the modules
loaded into it from the internet.
Instead we only plug and play whatever it is we need.
So PyCharm is actually already smart enough to know that I probably want a module
that I haven't installed.
And you can see that as soon as I click on this error with the red underline,
I get a red light bulb over here. And if I click on it,
it gives me the prompt to install this package called heroes.
And once it's done,
then we can actually tap into this module and we can say heroes.gen.
And this is going to generate us a new hero name.
So let's go ahead and run our code.
And you can see we've got a name called Galvatron,
which is not bad actually.
So it's important to remember that when you try to import something that hasn't
been installed, like this villains package,
then when you try to run the code
you'll get an error that says no module named villains.
And this should prompt you to think, ah,
maybe that module is not a part of the Python standard library like turtle
and I actually have to install it.
Just remember that before you can import a module
sometimes if it's not bundled with the Python standard library,
then you might have to install it.