WEBVTT

0
00:00.000 --> 00:05.080
So, we're importing the turtle module in order to use it in our code.

1
00:05.080 --> 00:10.560
So I want to spend a few minutes just quickly talking about all the ways that we can import

2
00:10.560 --> 00:18.000
modules.

3
00:18.000 --> 00:21.799
So we've already seen the Basic Import and it's very simple,

4
00:21.799 --> 00:27.420
we have the keyword import and then we have the Module name that we want to import.

5
00:27.420 --> 00:33.060
So if we had just imported our turtle module with the simple import, then in order to create

6
00:33.060 --> 00:40.200
a new turtle, we would have to say the module name and then the name of the class,

7
00:40.200 --> 00:50.020
and we would create a new turtle like this.

8
00:50.020 --> 00:55.340
Now this is perfectly fine, but it would make it so much more convenient if we were using

9
00:55.340 --> 00:59.840
that Turtle class a lot to write our code like this.

10
00:59.840 --> 01:05.360
This way, we don't have to keep writing turtle.Turtle() or turtle.Whatever else it is that we want

11
01:05.360 --> 01:06.800
to import.

12
01:06.800 --> 01:08.320
And this code is again pretty simple,

13
01:08.320 --> 01:13.879
we have the keyword "from", we have the keyword "import", we have the Module name, and also the

14
01:13.879 --> 01:17.680
Thing in the module that we want to import.

15
01:17.680 --> 01:24.699
As you've seen, with this kind of syntax, we can simply write...

16
01:24.760 --> 01:28.320
And this means that if we were creating a lot of turtles, let's say, instead of just

17
01:28.320 --> 01:38.959
creating tim, we also created tom and terry, then we don't have to keep writing turtle.Turtle(),

18
01:38.959 --> 01:41.760
turtle.Turtle() every single time.

19
01:41.760 --> 01:47.160
So this from import is really helpful if you're going to use this thing that you're importing

20
01:47.160 --> 01:51.800
a lot and you don't want to keep writing the name of the module in front of it.

21
01:51.800 --> 01:54.940
Now you can actually go one step further,

22
01:54.940 --> 02:00.000
instead of saying just from turtle import whatever it is you want, you can actually

23
02:00.000 --> 02:06.180
import everything by using the asterisk.

24
02:06.180 --> 02:12.179
Now you can use everything that's in that module as if it were in the current file.

25
02:12.179 --> 02:17.979
And this has advantages as well as disadvantages, because it can make it really hard to see

26
02:17.979 --> 02:21.520
where each of these classes or methods come from.

27
02:21.520 --> 02:26.880
So for example, if I just wrote forward(), you can see I can, I can do this,

28
02:26.880 --> 02:33.080
but it's really confusing to just see this method somewhere in isolation, because it's

29
02:33.080 --> 02:35.320
like, "Well, what is moving forward?

30
02:35.320 --> 02:36.520
What's actually happening?

31
02:36.520 --> 02:39.500
Where does this come from?"

32
02:39.500 --> 02:43.759
And it's more obvious when you import a module like random.

33
02:43.759 --> 02:47.440
So from random import everything.

34
02:47.440 --> 02:52.619
And then somewhere else in our code, we might just write something like, choice(),

35
02:52.619 --> 02:58.300
and this is a method from random, where we can pick a random item from a sequence like

36
02:58.300 --> 02:59.520
a list.

37
02:59.520 --> 03:03.000
Now this code works, but it's really confusing.

38
03:03.000 --> 03:05.600
Like, "How is this choice() working?

39
03:05.600 --> 03:06.639
Where does it come from?

40
03:06.639 --> 03:10.039
Which module enables this capability?"

41
03:10.039 --> 03:13.600
Instead we just have the method whack right in our code,

42
03:13.600 --> 03:16.639
and it's very confusing as to its origins.

43
03:16.639 --> 03:21.500
So amongst the Python community, it's very unusual that you'll see good code written

44
03:21.500 --> 03:22.500
like this.

45
03:22.500 --> 03:27.320
I want you to know what it does, because you might come across it in the wild, just so

46
03:27.320 --> 03:29.800
you understand what it's actually doing,

47
03:29.800 --> 03:33.000
but I want you to try and avoid writing code like this.

48
03:33.000 --> 03:38.360
Instead, if you're using something from a module many times, so more than three times,

49
03:38.360 --> 03:41.880
then you can think about using this "from import",

50
03:41.880 --> 03:46.919
but if you're only using it once or twice, then just import the whole module and write

51
03:46.919 --> 03:49.199
out this turtle.Turtle().

52
03:49.199 --> 03:56.000
So you can see that this module is the one that contains this class and that is imported

53
03:56.000 --> 03:59.240
and we're using it to create this object.

54
03:59.240 --> 04:09.320
It's just a lot more expressive in terms of the code.

55
04:09.320 --> 04:15.119
Now the final thing I want to show you, which is quite a useful thing, is how to Alias Modules.

56
04:15.119 --> 04:19.640
So for example, we can import our turtle as "t".

57
04:19.640 --> 04:26.619
So what this does is we import from the turtle module and we give that module an alias name,

58
04:26.619 --> 04:29.839
so a name that we define.

59
04:29.839 --> 04:35.519
And what this means is that if you were to create your new object from the module turtle,

60
04:35.519 --> 04:40.600
instead of writing out turtle every single time, you can just write "t", and it will represent

61
04:40.600 --> 04:42.119
the entire module.

62
04:42.119 --> 04:45.440
So sometimes you'll have modules which are really, really long.

63
04:45.440 --> 04:50.480
So it could have a really long name and you don't want to type it out every single time.

64
04:50.480 --> 04:55.359
So you give it an alias name and it will be exactly the same as referring to the entire

65
04:55.359 --> 04:57.399
name of the module.

66
04:57.399 --> 05:00.839
And you can create a turtle like this.

67
05:00.839 --> 05:07.239
Now, even though we've been writing import, there are some modules that you can't just import, right?

68
05:15.119 --> 05:20.959
So for example, if I wanted to import the heroes module, which is something that I can

69
05:20.959 --> 05:30.440
use to generate hero names like Decepticon or Leopardon or Askew-Tronics, then I can use

70
05:30.440 --> 05:36.119
one of the functions that's in that module, heroes, called generate.

71
05:36.119 --> 05:42.880
But if I just go into my code and I straight up try to import this module, which is called

72
05:42.880 --> 05:50.839
heroes, then you can see I get an error and the error says, there's "No module named heroes."

73
05:50.839 --> 05:55.279
Why is it that I can't do this, but I can import turtle?

74
05:55.920 --> 06:02.079
Well, the reason is because turtle is a module that's packaged with the Python standard library,

75
06:02.079 --> 06:08.200
and this is a small library of code which contains just the basics to get you started.

76
06:08.200 --> 06:13.079
Like a core set when you buy a board game or like the basic track pieces when you buy

77
06:13.079 --> 06:14.920
a set of Hot Wheels.

78
06:14.920 --> 06:20.559
So you can imagine this library of code as like a family library, easily accessible,

79
06:20.559 --> 06:22.359
but very small.

80
06:22.399 --> 06:28.040
Now, if we wanted to access the whole world of Python modules and packages, then we need

81
06:28.040 --> 06:31.559
to go to a much bigger library.

82
06:31.559 --> 06:36.880
And that is, of course, the Python packages, which are hosted on the internet and we can

83
06:36.880 --> 06:41.019
install into our project as and when we need them.

84
06:41.019 --> 06:46.519
So this way, our final project doesn't become gigantic because we've got all of the modules

85
06:46.519 --> 06:48.519
loaded into it from the internet,

86
06:48.640 --> 06:52.839
instead, we only plug and play whatever it is we need.

87
06:52.839 --> 06:57.399
So PyCharm is actually already smart enough to know that I probably want a module that

88
06:57.399 --> 06:59.239
I haven't installed.

89
06:59.239 --> 07:04.880
And you can see that as soon as I click on this error with the red underline, I get a

90
07:04.880 --> 07:07.559
red light bulb over here.

91
07:07.559 --> 07:13.079
And if I click on it, it gives me the prompt to install this package called heroes.

92
07:13.079 --> 07:20.880
And once it's done, then we can actually tap into this module and we can say heroes.gen(),

93
07:20.880 --> 07:24.920
and this is going to generate us a new hero name.

94
07:24.920 --> 07:30.679
So let's go ahead and run our code.

95
07:30.679 --> 07:37.000
And you can see we've got a name called Galvatron, which is not bad, actually.

96
07:37.000 --> 07:42.640
So it's important to remember that when you try to import something that hasn't been installed,

97
07:42.640 --> 07:47.920
like this villains package, then when you try to run the code, you'll get an error that

98
07:47.920 --> 07:50.959
says, "No module named villains".

99
07:50.959 --> 07:56.160
And this should prompt you to think, "Uh, maybe that module is not a part of the Python standard

100
07:56.160 --> 08:00.399
library like turtle, and I actually have to install it."

101
08:00.399 --> 08:05.239
Just remember that before you can import a module, sometimes if it's not bundled with

102
08:05.239 --> 08:10.160
the Python standard library, then you might have to install it.

103
08:10.160 --> 08:18.119
Now what happens when you install a package is it gets installed into the local virtual

104
08:18.119 --> 08:24.660
environment, which as we said previously, is on a per project basis.

105
08:24.660 --> 08:30.000
So this is stored in the .venv or virtual environment folder,

106
08:30.000 --> 08:37.020
and you can see it here, heroes and villains, as well as other modules in Python.

107
08:37.020 --> 08:40.739
Now, for the most part, you don't need to touch this folder at all,

108
08:40.739 --> 08:43.659
you can leave it as it is already set up for you.

109
08:43.659 --> 08:49.219
And when you create new projects in PyCharm, it usually gets created automatically for you.

110
08:49.219 --> 08:57.619
So this is an important differentiation between modules, like what we import here, and software.

111
08:57.619 --> 09:02.179
So when you install software, for example, PyCharm, you only need to install it once

112
09:02.179 --> 09:06.859
and it's available globally across your computer.

113
09:07.700 --> 09:14.539
Now with a virtual environment, and with these packages, they get installed into the project

114
09:14.539 --> 09:15.539
that you're building.

115
09:15.539 --> 09:21.380
So for example, today we're building day-18-start, and these two modules are available

116
09:21.380 --> 09:22.859
in this project,

117
09:22.859 --> 09:24.859
but if I was to start a new project,

118
09:32.859 --> 09:39.460
then you'll see that those packages are not installed by default.

119
09:39.460 --> 09:41.780
So they're not global.

120
09:41.780 --> 09:47.700
The reason why we need to work with virtual environments when using Python is actually

121
09:47.700 --> 09:49.739
a bit of a historical problem.

122
09:49.739 --> 09:54.340
So previously, there was a version of Python called Python 2,

123
09:54.340 --> 09:59.140
and then people wanted new features to Python, so they created Python 3.

124
09:59.140 --> 10:05.020
However, sadly, Python 3 is not what we call backward-compatible with Python 2.

125
10:05.020 --> 10:11.059
So it means that if you have code that's written in Python 2, and then you try to add Python

126
10:11.059 --> 10:14.020
3 to that project, it's not going to work.

127
10:14.020 --> 10:19.659
So you kind of have to make a choice, either Python 2 project, or projects written in Python

128
10:19.659 --> 10:20.659
3.

129
10:20.659 --> 10:23.419
Now, we all know that Python 3 is the future,

130
10:23.419 --> 10:28.640
but that doesn't mean that there isn't a lot of existing projects written in Python 2.

131
10:28.640 --> 10:32.780
So the transition has been really, really slow,

132
10:32.780 --> 10:39.479
and virtual environments help us in a way by defining a small sandbox for our project.

133
10:39.479 --> 10:44.760
So we can decide whether we want to use Python 2 or Python 3 for our project,

134
10:44.760 --> 10:49.679
and then because we're in a virtual environment, we can install the modules that we need, for

135
10:49.679 --> 10:54.919
example, the heroes module, which works with Python 3, but not Python 2.

136
10:54.919 --> 11:00.299
And we know that because we have our virtual environment set up for each and every project,

137
11:00.299 --> 11:05.200
and it's different for every single project, then when we install each module, we know

138
11:05.200 --> 11:06.880
that it's compatible,

139
11:06.880 --> 11:11.559
we know that that particular version works with all of the other packages that we have

140
11:11.559 --> 11:12.559
installed,

141
11:12.559 --> 11:17.239
and it's kind of like freezing that project in time, so that you know, once you compile

142
11:17.239 --> 11:22.039
everything, once your program runs, you can continue running that program as and when

143
11:22.039 --> 11:29.080
you want, even if things like Python change versions, or your modules change versions.

144
11:29.080 --> 11:37.080
So that's one of the reasons why we want to use PyCharm to handle and install our modules,

145
11:37.080 --> 11:41.400
because it means that it works with the rest of our virtual environment.

146
11:41.400 --> 11:46.239
It ensures that there are no errors that occur when we install it.

147
11:46.239 --> 11:53.080
And we maintain the version that we use, so that our final project works as expected.

148
11:53.080 --> 11:57.559
Now some of you might have seen that you can also install these packages using pip.

149
11:57.559 --> 12:02.559
So you could go into the terminal and write "pip install..." and pick one of these projects.

150
12:02.559 --> 12:08.000
But as you can imagine, with virtual environments, with the different projects having different

151
12:08.000 --> 12:12.880
virtual environments, you might install these modules into the wrong location,

152
12:12.880 --> 12:18.280
and that means your project won't be able to access them outside of the sandbox.

153
12:18.280 --> 12:23.520
So I recommend following the video and doing what I do in the videos, so that we can all

154
12:23.520 --> 12:27.559
be on the same page and we can all learn as quickly as possible.