1
00:00:05,380 --> 00:00:09,050
so far we've been writing programs more or less largely contained every

2
00:00:09,050 --> 00:00:14,060
everything they need in a single
Python file now there were a couple of

3
00:00:14,060 --> 00:00:18,180
exceptions when we imported random to make our
guessing game more interesting and of

4
00:00:18,180 --> 00:00:21,880
course when we also imported shelve in
the last section dealing with

5
00:00:21,880 --> 00:00:26,990
file input and output now the Python
documentation refers to a file

6
00:00:26,990 --> 00:00:31,630
containing a Python program such as
we've been creating as a script but that

7
00:00:31,630 --> 00:00:36,180
said it's also possible to produce
Python code that is not intended to be

8
00:00:36,180 --> 00:00:41,050
executed directly instead to the
provides functions classes and

9
00:00:41,050 --> 00:00:46,250
variable definitions etc that are
designed to be used by other programs

10
00:00:46,250 --> 00:00:49,820
we managed to do quite a lot without
importing modules into our scripts

11
00:00:49,820 --> 00:00:52,570
the part of power of Python

12
00:00:52,570 --> 00:00:57,050
is that it allows us to use modules to
provide that extra functionality there's

13
00:00:57,050 --> 00:01:01,410
nothing to stop us from creating our own
modules and will be looking how to this

14
00:01:01,410 --> 00:01:05,550
later in this section in order to use a
module we have to import it into our program

15
00:01:05,550 --> 00:01:10,180
using the import command and of course you
saw that in the last section this

16
00:01:10,180 --> 00:01:14,300
can take a couple of forms actually so
let's have a play with the Python turtle

17
00:01:14,300 --> 00:01:19,740
module just to really understand how
import works so create a new file

18
00:01:19,740 --> 00:01:22,030
and setup a new project

19
00:01:22,030 --> 00:01:28,940
create a new Python file and call it Turtledemo

20
00:01:28,940 --> 00:01:31,910
now the turtle module is based on the
educational programming language from

21
00:01:31,910 --> 00:01:36,090
the late 1960's called logo and that
allowed amongst other things children to

22
00:01:36,090 --> 00:01:40,600
control an on-screen turtle by giving
it simple instructions to move and turn now

23
00:01:40,600 --> 00:01:45,140
there is a pen attached to this turtle so when it
moves it draws a line behind itself to

24
00:01:45,140 --> 00:01:48,630
so let us type in a simple example that's
gonna move the turtle around the screen

25
00:01:48,630 --> 00:01:56,100
gonna start with importing the module.....

26
00:01:56,100 --> 00:02:02,330
....and we type a few commands first things first will type in
the command to go forward so...

27
00:02:05,010 --> 00:02:10,020
...we can get it turn right 250 degrees....

28
00:02:10,020 --> 00:02:19,170
......can try going getting it to go forward again.....

29
00:02:19,170 --> 00:02:26,600
....so a simple example moves the turtle forward

30
00:02:26,600 --> 00:02:36,760
turns right 250 degrees and it moves
forward again so run it and did you see what

31
00:02:36,760 --> 00:02:40,590
happened briefly you saw the command
come up on the screen or little box come

32
00:02:40,590 --> 00:02:41,330
up on the screen, and you can see

33
00:02:41,330 --> 00:02:45,070
drawing something and then quickly close
the program down and that's how it

34
00:02:45,070 --> 00:02:49,230
works because our program ended the
graphic screen closed down so

35
00:02:49,230 --> 00:02:53,930
there is a good way to prevent this and we can
import another module to do that called time so we can type...

36
00:02:53,930 --> 00:03:00,030
...below the first import and then create another line of code on line 8.....

37
00:03:00,030 --> 00:03:06,460
we will get it to sleep for 4 seconds so we type time....which is getting to

38
00:03:06,460 --> 00:03:13,460
get to sleep before seconds so we should
be able to see it now so run it again  and you

39
00:03:13,460 --> 00:03:18,450
see what happen was that it went forward turned
250 degrees to the right and then

40
00:03:18,450 --> 00:03:22,480
continued on as you saw that it slept for
4 seconds giving us a time to see the

41
00:03:22,480 --> 00:03:26,550
turtles drawing on the screen before the
program ended and it close it down now

42
00:03:26,550 --> 00:03:32,430
interestingly enough there's actually a bug
in the IntelliJ product both IDEA and Pycharm

43
00:03:32,430 --> 00:03:37,110
and that causes them to display a warning about
cannot find reference forward in  turtle.py so

44
00:03:37,110 --> 00:03:41,930
if we hover this and have a
I'll fix that part because it needs a blank line in the end

45
00:03:41,930 --> 00:03:49,180
here is the actual error itself you can see it saying it cannot
find reference for two turtle.py and as you saw

46
00:03:49,180 --> 00:03:53,710
the program is fine so clearly not a problem
with the code but IntelliJ can struggle

47
00:03:53,710 --> 00:03:58,150
with importing from some modules and
noticed that importing the module from

48
00:03:58,150 --> 00:04:03,360
time using time module I should say using the
the sleep method had no problems it found

49
00:04:03,360 --> 00:04:07,920
that ok so warnings are useful or will
become less useful if they are incorrect because

50
00:04:07,920 --> 00:04:10,790
ultimately that can detract from the
ones we should be paying attention to

51
00:04:10,790 --> 00:04:16,100
and start ignoring them all together so
in this scenario where you convinced

52
00:04:16,100 --> 00:04:16,930
it's a warning

53
00:04:16,930 --> 00:04:20,459
that it is a bug where we're pretty sure
that it's a bug here right now with

54
00:04:20,459 --> 00:04:25,360
IntelliJ we can suppress them so they
can be safely ignored what we can do is

55
00:04:25,360 --> 00:04:30,080
do that and one way is on a statement by
statement basis so come up here and add a new

56
00:04:30,080 --> 00:04:42,750
line for we can put a.......

57
00:04:42,750 --> 00:04:48,020
you can see what happens as soon as I finish typing that the
a warning disappeared so that handles

58
00:04:48,020 --> 00:04:53,170
suppressing the error or the warning I
should say just on a single line it would

59
00:04:53,170 --> 00:04:58,380
soon get annoying having to do that with
each line to get rid of each warning message so what you can do is

60
00:04:58,380 --> 00:05:02,610
disable it for all future turtke commands which  isprobably be wise in this

61
00:05:02,610 --> 00:05:03,420
case

62
00:05:03,420 --> 00:05:08,450
because we still got 2 warnings here to the right hand side so we
come over here to line 6 click on

63
00:05:08,450 --> 00:05:15,440
right on line 6 see that little pop up comes up so we can
select the pop up and use the third down here mark all

64
00:05:15,440 --> 00:05:20,710
unresolved attributes of turtle as
ignored click on that you can see that

65
00:05:20,710 --> 00:05:24,780
then disappeared all the warnings and we got the green tick which means

66
00:05:24,780 --> 00:05:32,160
as far as IntelliJ is concern everything is ok so after doing that
you could delete line 4 because the

67
00:05:32,160 --> 00:05:36,860
command on line 6 when we right
clicked it overwrote that command anyway

68
00:05:37,440 --> 00:05:41,910
we have taken those warnings off let us just say for
some reason IntelliJ fix that error

69
00:05:41,910 --> 00:05:45,960
and it's now something you want to put
back on again you want to enable

70
00:05:45,960 --> 00:05:51,830
warnings again to do that it is on a file by file
basis so you open the file you want to deal with come up here to

71
00:05:51,830 --> 00:05:56,730
code to analyze
click on Config Current File

72
00:05:56,730 --> 00:05:58,040
Analysis

73
00:05:58,040 --> 00:06:02,080
click on configure inspections and what
you need to do is you need to scroll

74
00:06:02,080 --> 00:06:05,570
down and there will be a list of all these other bits
and pieces you want one that says Python

75
00:06:05,570 --> 00:06:09,790
and if there has been a change made you will notice
it will be showing in blue it is showing

76
00:06:09,790 --> 00:06:12,720
in blue here because we have made that
change so what I'm going to do is come down

77
00:06:12,720 --> 00:06:18,190
to unresolved references and
that's showing in blue again and if we want

78
00:06:18,190 --> 00:06:21,920
to get rid of that you can see here the ignore
references we can add some more here

79
00:06:21,920 --> 00:06:28,850
if we have some but otherwise we can click on it and click on minus
to remove it and the warnings then come

80
00:06:28,850 --> 00:06:29,220
back

81
00:06:29,220 --> 00:06:34,790
and if we go back in to analyze again
analyzed Config Current File Analysis

82
00:06:34,790 --> 00:06:36,880
configure inspections

83
00:06:36,880 --> 00:06:41,280
and looking back here now notice now its not in
blue anymore and also come up here too

84
00:06:41,280 --> 00:06:45,570
Python that is no longer in blue either so that's a quick way to

85
00:06:45,570 --> 00:06:51,190
determine whether you actually made that
any changes to this particular model or not

86
00:06:51,190 --> 00:06:55,170
so I'm gonna re add that again because it's
just an annoying warning we do

87
00:06:55,170 --> 00:07:00,360
wanna get rid off so I'm gonna click on right here and select the

88
00:07:00,360 --> 00:07:07,920
pop up click on mark all unresolved attributes
of turtle as ignored again and we get your green arrow back again

89
00:07:07,920 --> 00:07:13,320
so just running the app again so you can see we got our
drawing sits there for a few seconds

90
00:07:13,320 --> 00:07:18,810
four seconds to be exact and closes down
so the pause is a bit arbitrary so the correct way to

91
00:07:18,810 --> 00:07:23,480
give us time to view the reptilian artwork is to use the turtles module done

92
00:07:23,480 --> 00:07:27,450
method this also me so we can get rid of
the time import so I'm going to

93
00:07:27,450 --> 00:07:36,890
delete the time import and instead of time.sleep we are gonna put....

94
00:07:36,890 --> 00:07:37,450
essentially now

95
00:07:37,450 --> 00:07:42,730
the masterpiece is going to stay there until
we close the window so we just sit here for a few seconds

96
00:07:42,730 --> 00:07:45,630
to make sure you can see nothing's
happening so we literally had to come

97
00:07:45,630 --> 00:07:49,230
over here and close it down for that
change to take effect and for our program to

98
00:07:49,230 --> 00:07:54,080
exit the other thing to remember was
with using the import command to import

99
00:07:54,080 --> 00:07:57,640
the turtle module we can then use all
the objects it provides by prefixing

100
00:07:57,640 --> 00:08:02,260
them with turtle and a dot so turtle.forward as you saw turtle.right etc

101
00:08:02,260 --> 00:08:08,200
because we also saw that with time.sleep until I just remove the code there now there is another way to import

102
00:08:08,200 --> 00:08:12,310
from a module specifying just things we want to
import as supposed to doing an

103
00:08:12,310 --> 00:08:16,260
import of everything and what I mean by
that is I'm going to delete that line on the

104
00:08:16,260 --> 00:08:19,360
first line the import and we could do
something like this instead of we can be

105
00:08:19,360 --> 00:08:28,200
more selective we can say from....so we are only going

106
00:08:28,200 --> 00:08:33,480
to import the first method called for and
then by doing that I can get rid of

107
00:08:33,480 --> 00:08:38,630
this turtle. at the start and notice how it has found
forward because its found and look for it

108
00:08:38,630 --> 00:08:42,559
in the turtle module so again essentially what we're
doing is we're telling Python to import

109
00:08:42,559 --> 00:08:46,130
just the forward method from turtle and
doing it this way means that we don't

110
00:08:46,130 --> 00:08:50,090
have to keep prefixing forward with
turtle. all the time and if we run

111
00:08:50,090 --> 00:08:56,580
this we do get some errors and the errors on
lines for 4 5 and 7 saying that

112
00:08:56,580 --> 00:09:03,720
turtle is not defined but we could fix it if we
wanted to by changing our import on line 1 putting a...

113
00:09:03,720 --> 00:09:08,650
for the right method and done for the right one as well and
if we run that again we should find it

114
00:09:08,650 --> 00:09:16,180
works fine and sorry had to get rid of
the turtle. before our commands and

115
00:09:16,180 --> 00:09:20,400
notice how these are all enabled now
so it has found those now if I run it we find that it works

116
00:09:20,400 --> 00:09:25,780
quite nicely and we only imported those
functions from the

117
00:09:25,780 --> 00:09:30,630
turtle module that we actually need to
so close it down so doing it this way can be useful for

118
00:09:30,630 --> 00:09:34,090
using a particular object a lot in the
program but the problem is it only

119
00:09:34,090 --> 00:09:37,670
import things that are specifically
listed on the import line and as you saw

120
00:09:37,670 --> 00:09:41,900
those errors until I remove them the other ones aren't available so what I can't do now is to do something

121
00:09:41,900 --> 00:09:43,210
like....

122
00:09:43,210 --> 00:09:47,010
....which is a valid command

123
00:09:47,010 --> 00:09:53,010
if we run that now that is not going to work, unnamed turtle was
not to defined because again for that reason we

124
00:09:53,010 --> 00:09:57,020
only specified the specific things
we wanted and we don't need to specify turtle.

125
00:09:57,020 --> 00:10:01,050
and in actual fact turtle. doesn't
work because as far as Python is concerned that

126
00:10:01,050 --> 00:10:09,320
module doesn't exist and the other things
we can't do either is type...try to run that...we get the error

127
00:10:09,320 --> 00:10:16,390
turtle circle and if I comment this line and run it again we name circle is not

128
00:10:16,390 --> 00:10:20,140
defined as well that's because we
haven't specified circle up here on line

129
00:10:20,140 --> 00:10:25,770
1 so if wanted circle. to work I'll delete line 7 because we know how to do that we

130
00:10:25,770 --> 00:10:30,940
could just add circle up here on line 1 to fix that after the comma but if want to get line 5

131
00:10:30,940 --> 00:10:35,450
to to work like this what we can do is
below the from turtle command we can

132
00:10:35,450 --> 00:10:48,050
also put an import....so we should find if we run it now everything works fine

133
00:10:48,050 --> 00:10:52,860
and you can see we got a different result there we got a circle
created and then we went on and plotted

134
00:10:52,860 --> 00:10:58,990
or started plotting the triangle
so that's good so that works fine

135
00:10:58,990 --> 00:11:02,380
so this does demonstrate the two different ways
an import can be used and the effect each

136
00:11:02,380 --> 00:11:06,780
it has on how you can use the imported
objects generally speaking you wouldn't

137
00:11:06,780 --> 00:11:09,130
want to mix methods with the same module so

138
00:11:09,130 --> 00:11:12,140
in our example what you wanna do import
turtle

139
00:11:12,140 --> 00:11:16,890
on line 2 you don't only just have an import turtle like on line 2 or you import the existing

140
00:11:16,890 --> 00:11:21,490
individual methods class variables and
variables but rarely combined both

141
00:11:21,490 --> 00:11:25,630
because obviously that can lead to
confusing code especially because turtle

142
00:11:25,630 --> 00:11:29,110
.forward would also work as well as
forward now and that could be quite

143
00:11:29,110 --> 00:11:32,820
confusing to someone reading your code and
wondering why things are different and

144
00:11:32,820 --> 00:11:37,560
why you've done things sort of both ways so it is
good to choose one way or the other now there is one way

145
00:11:37,560 --> 00:11:40,840
to use import that's definitely not
recommended

146
00:11:40,840 --> 00:11:44,110
so it's possible to import everything
or more actually almost everything from the

147
00:11:44,110 --> 00:11:46,140
module using import star

148
00:11:46,140 --> 00:11:52,490
so we could do something like here change our references here from turtle
import I put import * and get rid of this

149
00:11:52,490 --> 00:12:00,760
import turtle on line 2 and I've got to delete line 5 as well so
we are left with our commands and i'll put the circle

150
00:12:00,760 --> 00:12:13,510
command back.....so we should be able to run that so you can
see that work ok and depending on

151
00:12:13,510 --> 00:12:18,240
your operating system you may get
some errors here in my case i didn't get any

152
00:12:18,240 --> 00:12:22,180
errors and there was a tick their but IntelliJ may struggle when underlining the

153
00:12:22,180 --> 00:12:27,400
method calls in red but as you saw the
program will still totally run anyway although you

154
00:12:27,400 --> 00:12:31,520
can import from a module in this way
with a star which will import everything

155
00:12:31,520 --> 00:12:35,770
who's name does not start with an
underscore doing so is frowned upon it's

156
00:12:35,770 --> 00:12:39,810
very useful to save typing when using an
interactive session in idle for example

157
00:12:39,810 --> 00:12:43,330
but other than that you should really
avoid this way of importing now one

158
00:12:43,330 --> 00:12:47,190
reason for that or you don't want to do that
is that you actually have no idea what

159
00:12:47,190 --> 00:12:51,770
is being imported so an imported name can hid
one of your objects or you can hide

160
00:12:51,770 --> 00:12:55,760
one of the imported names that
you need and just to give you an idea that

161
00:12:55,760 --> 00:12:59,550
in this example we got to
demonstrate why this could be a problem

162
00:12:59,550 --> 00:13:03,270
so lets just say we're going to use
the import start so what I'm going to do is put

163
00:13:03,270 --> 00:13:15,030
something on line 1 ill put.....and we got our from turtle

164
00:13:15,030 --> 00:13:18,200
import....

165
00:13:18,200 --> 00:13:29,650
on line 3 we got our import I come to line
10 we have our done and we wanna do....so if we run this

166
00:13:29,650 --> 00:13:34,840
we get a picture drawing just fine nothing
happens until we close down but once we

167
00:13:34,840 --> 00:13:38,750
do closed and you can see that we
you get the message printed is a

168
00:13:38,750 --> 00:13:44,810
function address of the turtle done
method and not the string message Well

169
00:13:44,810 --> 00:13:49,560
done you have finished your drawing like you would expect
so that could be confusing but if we did it the other way as

170
00:13:49,560 --> 00:13:59,120
well things could get even a little bit worse so if
we put the import on line 1 and change them around

171
00:13:59,120 --> 00:14:03,520
so literally just changed lines 1
and 3 around and done nothing else if we

172
00:14:03,520 --> 00:14:11,000
run this again it will work it will draw the picture or
starts to but then there's actually a

173
00:14:11,000 --> 00:14:15,470
problem and the problem is when it
reaches the done line which is on line

174
00:14:15,470 --> 00:14:19,450
10 and it says string object is not
callable so what's happening of course

175
00:14:19,450 --> 00:14:25,150
is its trying to call or to treat the string as
a method and executing it rather than

176
00:14:25,150 --> 00:14:29,270
using the done method from the turtle
import now of course this second

177
00:14:29,270 --> 00:14:33,390
example in particular but also the first
one before I swap them over are really quite

178
00:14:33,390 --> 00:14:37,190
contrived because we called the done method
with only a few lines defining the

179
00:14:37,190 --> 00:14:40,560
string variable so that would be very
hard to miss the fact that something

180
00:14:40,560 --> 00:14:44,280
wasn't right ordinarily but however if
you consider a program with hundreds of

181
00:14:44,280 --> 00:14:49,190
lines of code mistakes like this would be
much easier to make and harder to find so

182
00:14:49,190 --> 00:14:53,370
bottom line here is that unless you're
working in an interactive session do not

183
00:14:53,370 --> 00:14:58,490
do an import star so I'm gonna finish
the video here now and in the next video

184
00:14:58,490 --> 00:15:02,570
we are gonna start talking about the Python
Standard library so see you in that video

