1
00:00:00,330 --> 00:00:04,500
So we're importing the turtle module in order to use it in our code.

2
00:00:05,010 --> 00:00:09,450
So I want to spend a few minutes just quickly talking about all the ways that we

3
00:00:09,450 --> 00:00:10,770
can import modules.

4
00:00:10,940 --> 00:00:15,940
[inaudible]

5
00:00:18,380 --> 00:00:21,740
You've already seen the basic import and it's very simple.

6
00:00:21,740 --> 00:00:23,480
We have the keyword import,

7
00:00:23,750 --> 00:00:26,300
and then we have the module name that we want to import.

8
00:00:27,320 --> 00:00:31,580
So if we had just imported our turtle module with the simple import,

9
00:00:32,000 --> 00:00:34,280
then in order to create a new turtle,

10
00:00:34,640 --> 00:00:39,620
we would have to say the module name and then the name of the class.

11
00:00:40,100 --> 00:00:42,290
And we would create a new turtle like this.

12
00:00:49,970 --> 00:00:50,630
Now this is,

13
00:00:50,630 --> 00:00:54,170
perfectly fine, but it would make it so much more convenient

14
00:00:54,200 --> 00:00:59,120
if we were using that turtle class a lot to write our code like this.

15
00:00:59,660 --> 00:01:04,099
This way, we don't have to keep writing turtle.Turtle or turtle.

16
00:01:04,129 --> 00:01:07,580
whatever else it is that we want to import. And this code is again

17
00:01:07,610 --> 00:01:11,240
pretty simple. We have the keyword from, we have the keyword import,

18
00:01:11,570 --> 00:01:13,160
we have the module name,

19
00:01:13,280 --> 00:01:16,280
and also the thing in the module that we want to import.

20
00:01:17,570 --> 00:01:21,110
As you've seen with this kind of syntax, we can simply write...

21
00:01:24,440 --> 00:01:27,890
And this means that if we were creating a lot of turtles, let's say

22
00:01:27,980 --> 00:01:30,680
instead of just creating tim, we also created tom

23
00:01:32,690 --> 00:01:33,800
and terry.

24
00:01:34,340 --> 00:01:39,020
Then we don't have to keep writing turtle.Turtle,

25
00:01:39,020 --> 00:01:41,180
turtle.Turtle every single time.

26
00:01:41,660 --> 00:01:44,510
So this from import is really helpful

27
00:01:44,510 --> 00:01:48,890
if you're going to use this thing that you're importing a lot and you don't want

28
00:01:48,890 --> 00:01:51,320
to keep writing the name of the module in front of it.

29
00:01:51,740 --> 00:01:55,430
Now you can actually go one step further. Instead of saying

30
00:01:55,430 --> 00:01:58,640
just from turtle import whatever it is you want,

31
00:01:59,000 --> 00:02:04,000
you can actually import everything by using the asterix.

32
00:02:06,020 --> 00:02:10,759
Now you can use everything that's in that module as if it were in the current

33
00:02:10,759 --> 00:02:14,870
file. And this has advantages as well as disadvantages

34
00:02:15,410 --> 00:02:20,410
because it can make it really hard to see where each of these classes or methods

35
00:02:20,630 --> 00:02:25,580
come from. So for example, if I just wrote forward, you can see, I can,

36
00:02:25,580 --> 00:02:26,420
I can do this.

37
00:02:26,720 --> 00:02:31,720
But it's really confusing to just see this method somewhere in isolation

38
00:02:32,600 --> 00:02:36,380
because it's like, well, what is moving forward? What's actually happening,

39
00:02:36,380 --> 00:02:37,550
where does this come from?

40
00:02:39,230 --> 00:02:43,640
And it's more obvious when you import a module like random.

41
00:02:43,640 --> 00:02:46,520
So from random import everything,

42
00:02:47,180 --> 00:02:49,310
and then somewhere else in our code,

43
00:02:49,340 --> 00:02:52,250
we might just write something like choice.

44
00:02:52,490 --> 00:02:57,490
And this is a method from random where we can pick a random item from a

45
00:02:57,620 --> 00:03:01,060
sequence, like a list. Now this code works

46
00:03:01,090 --> 00:03:04,990
but it's really confusing. Like, how is this choice working?

47
00:03:05,410 --> 00:03:10,150
Where does it come from? Which module enables this capability? Instead,

48
00:03:10,150 --> 00:03:13,150
we just have the method whack right in our code

49
00:03:13,510 --> 00:03:16,030
and it's very confusing as to its origins.

50
00:03:16,570 --> 00:03:18,370
So well, amongst the Python community,

51
00:03:18,400 --> 00:03:22,210
it's very unusual that you'll see good code written like this.

52
00:03:22,540 --> 00:03:26,890
I want you to know what it does because you might come across it in the wild,

53
00:03:27,130 --> 00:03:29,350
just so you understand what it's actually doing,

54
00:03:29,650 --> 00:03:32,410
but I want you to try and avoid writing code like this.

55
00:03:32,860 --> 00:03:36,610
Instead if you're using something from a module many times,

56
00:03:36,610 --> 00:03:41,380
so more than three times, then you can think about using this from import.

57
00:03:41,770 --> 00:03:44,080
But if you're only using it once or twice,

58
00:03:44,320 --> 00:03:49,320
then just import the whole module and write out this turtle.Turtle so that

59
00:03:49,380 --> 00:03:54,380
you can see that this module is the one that contains this class and that is

60
00:03:55,060 --> 00:03:58,540
imported and we're using it to create this object.

61
00:03:59,110 --> 00:04:01,960
It's just a lot more expressive in terms of the code.

62
00:04:09,240 --> 00:04:12,660
Now, the final thing I want to show you which is quite a useful thing,

63
00:04:12,870 --> 00:04:16,019
is how to alias modules. So for example,

64
00:04:16,110 --> 00:04:19,500
we can import our turtle as t.

65
00:04:19,709 --> 00:04:24,710
So what this does is we import from the turtle module and we give that module an

66
00:04:25,830 --> 00:04:27,930
alias name, so a name that we define.

67
00:04:29,700 --> 00:04:33,630
And what this means is that if you were to create your new object from the

68
00:04:33,630 --> 00:04:37,770
module turtle, instead of writing out turtle every single time,

69
00:04:37,980 --> 00:04:41,760
you can just write t and that it will represent the entire module.

70
00:04:42,030 --> 00:04:45,360
So sometimes you'll have modules which are really, really long.

71
00:04:45,360 --> 00:04:49,350
So it could have a really long name and you don't want to type it out every

72
00:04:49,350 --> 00:04:50,183
single time.

73
00:04:50,400 --> 00:04:54,780
So you give it an alias name and it will be exactly the same as referring to the

74
00:04:54,780 --> 00:04:56,490
entire name of the module.

75
00:04:57,240 --> 00:05:00,990
And you can create a turtle like this. Now,

76
00:05:01,050 --> 00:05:03,330
even though we've been writing import,

77
00:05:03,630 --> 00:05:07,170
there are some modules that you can't just import, right?

78
00:05:15,030 --> 00:05:19,560
So for example, if I wanted to import the heroes module

79
00:05:19,920 --> 00:05:24,920
which is something that I can use to generate hero names like Decepticon or 

80
00:05:26,160 --> 00:05:28,950
Leopardon, or Askew-Tronics,

81
00:05:29,310 --> 00:05:33,390
then I can use one of the functions that's in that module,

82
00:05:33,450 --> 00:05:35,310
heroes, called generate.

83
00:05:35,940 --> 00:05:40,940
But if I just go into my code and I straight up try to import this module

84
00:05:42,420 --> 00:05:45,900
which is called heroes, then you can see I get an error.

85
00:05:46,290 --> 00:05:49,830
And the error says, there's no module named heroes.

86
00:05:50,580 --> 00:05:55,170
Why is it that I can't do this but I can import turtle?

87
00:05:55,980 --> 00:06:00,620
Well, the reason is because turtle is a module that's packaged with the Python

88
00:06:00,620 --> 00:06:04,640
standard library. And this is a small library of code

89
00:06:04,940 --> 00:06:09,800
which contains just the basics to get you started like a core set when you buy a

90
00:06:09,800 --> 00:06:14,150
board game or like the basic track pieces when you buy a set of hot wheels.

91
00:06:14,810 --> 00:06:19,130
So you can imagine this library of code as like a family library,

92
00:06:19,160 --> 00:06:22,580
easily accessible, but very small. Now,

93
00:06:22,610 --> 00:06:27,140
if we wanted to access the whole world of Python modules and packages,

94
00:06:27,530 --> 00:06:32,150
then we need to go to a much bigger library. And that is,

95
00:06:32,150 --> 00:06:34,580
of course, the Python packages,

96
00:06:34,880 --> 00:06:39,470
which are hosted on the internet and we can install into our project as

97
00:06:39,470 --> 00:06:41,540
and when we need them. So this way,

98
00:06:41,540 --> 00:06:46,540
our final project doesn't become gigantic because we've got all of the modules

99
00:06:46,640 --> 00:06:48,260
loaded into it from the internet.

100
00:06:48,560 --> 00:06:51,860
Instead we only plug and play whatever it is we need.

101
00:06:52,760 --> 00:06:57,050
So PyCharm is actually already smart enough to know that I probably want a module

102
00:06:57,320 --> 00:06:58,670
that I haven't installed.

103
00:06:59,150 --> 00:07:03,740
And you can see that as soon as I click on this error with the red underline,

104
00:07:04,250 --> 00:07:08,480
I get a red light bulb over here. And if I click on it,

105
00:07:08,550 --> 00:07:12,440
it gives me the prompt to install this package called heroes.

106
00:07:12,950 --> 00:07:14,180
And once it's done,

107
00:07:14,360 --> 00:07:19,360
then we can actually tap into this module and we can say heroes.gen.

108
00:07:20,780 --> 00:07:24,200
And this is going to generate us a new hero name.

109
00:07:24,830 --> 00:07:27,080
So let's go ahead and run our code.

110
00:07:30,460 --> 00:07:33,820
And you can see we've got a name called Galvatron,

111
00:07:33,850 --> 00:07:35,680
which is not bad actually.

112
00:07:36,880 --> 00:07:41,410
So it's important to remember that when you try to import something that hasn't

113
00:07:41,410 --> 00:07:44,440
been installed, like this villains package,

114
00:07:44,800 --> 00:07:46,540
then when you try to run the code

115
00:07:46,840 --> 00:07:50,230
you'll get an error that says no module named villains.

116
00:07:50,770 --> 00:07:53,050
And this should prompt you to think, ah,

117
00:07:53,080 --> 00:07:57,460
maybe that module is not a part of the Python standard library like turtle

118
00:07:57,760 --> 00:07:59,710
and I actually have to install it.

119
00:08:00,220 --> 00:08:03,100
Just remember that before you can import a module

120
00:08:03,280 --> 00:08:06,670
sometimes if it's not bundled with the Python standard library,

121
00:08:07,030 --> 00:08:08,710
then you might have to install it.

