WEBVTT 1 00:00:01.939 --> 00:00:05.609 so far we've been writing programs more or less largely contained every 2 00:00:05.609 --> 00:00:10.610 everything they need in a single Python file now there were a couple of 3 00:00:10.610 --> 00:00:14.739 exceptions when we imported random to make our guessing game more interesting and of 4 00:00:14.739 --> 00:00:18.430 course when we also imported shelve in the last section dealing with 5 00:00:18.430 --> 00:00:23.540 file input and output now the Python documentation refers to a file 6 00:00:23.540 --> 00:00:28.180 containing a Python program such as we've been creating as a script but that 7 00:00:28.180 --> 00:00:32.739 said it's also possible to produce Python code that is not intended to be 8 00:00:32.739 --> 00:00:37.609 executed directly instead to the provides functions classes and 9 00:00:37.609 --> 00:00:42.809 variable definitions etc that are designed to be used by other programs 10 00:00:42.809 --> 00:00:46.379 we managed to do quite a lot without importing modules into our scripts 11 00:00:46.379 --> 00:00:49.120 the part of power of Python 12 00:00:49.120 --> 00:00:53.609 is that it allows us to use modules to provide that extra functionality there's 13 00:00:53.609 --> 00:00:57.969 nothing to stop us from creating our own modules and will be looking how to this 14 00:00:57.969 --> 00:01:02.100 later in this section in order to use a module we have to import it into our program 15 00:01:02.100 --> 00:01:06.730 using the import command and of course you saw that in the last section this 16 00:01:06.730 --> 00:01:10.850 can take a couple of forms actually so let's have a play with the Python turtle 17 00:01:10.850 --> 00:01:16.290 module just to really understand how import works so create a new file 18 00:01:16.290 --> 00:01:18.580 and setup a new project 19 00:01:18.580 --> 00:01:25.490 create a new Python file and call it Turtledemo 20 00:01:25.490 --> 00:01:28.460 now the turtle module is based on the educational programming language from 21 00:01:28.460 --> 00:01:32.640 the late 1960's called logo and that allowed amongst other things children to 22 00:01:32.640 --> 00:01:37.150 control an on-screen turtle by giving it simple instructions to move and turn now 23 00:01:37.150 --> 00:01:41.690 there is a pen attached to this turtle so when it moves it draws a line behind itself to 24 00:01:41.690 --> 00:01:45.180 so let us type in a simple example that's gonna move the turtle around the screen 25 00:01:45.180 --> 00:01:52.650 gonna start with importing the module..... 26 00:01:52.650 --> 00:01:58.880 ....and we type a few commands first things first will type in the command to go forward so... 27 00:02:01.560 --> 00:02:06.579 ...we can get it turn right 250 degrees.... 28 00:02:06.579 --> 00:02:15.720 ......can try going getting it to go forward again..... 29 00:02:15.720 --> 00:02:23.150 ....so a simple example moves the turtle forward 30 00:02:23.150 --> 00:02:33.319 turns right 250 degrees and it moves forward again so run it and did you see what 31 00:02:33.319 --> 00:02:37.140 happened briefly you saw the command come up on the screen or little box come 32 00:02:37.140 --> 00:02:37.880 up on the screen, and you can see 33 00:02:37.880 --> 00:02:41.620 drawing something and then quickly close the program down and that's how it 34 00:02:41.620 --> 00:02:45.780 works because our program ended the graphic screen closed down so 35 00:02:45.780 --> 00:02:50.480 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:50.480 --> 00:02:56.580 ...below the first import and then create another line of code on line 8..... 37 00:02:56.580 --> 00:03:03.010 we will get it to sleep for 4 seconds so we type time....which is getting to 38 00:03:03.010 --> 00:03:10.019 get to sleep before seconds so we should be able to see it now so run it again and you 39 00:03:10.019 --> 00:03:15.000 see what happen was that it went forward turned 250 degrees to the right and then 40 00:03:15.000 --> 00:03:19.030 continued on as you saw that it slept for 4 seconds giving us a time to see the 41 00:03:19.030 --> 00:03:23.100 turtles drawing on the screen before the program ended and it close it down now 42 00:03:23.100 --> 00:03:28.980 interestingly enough there's actually a bug in the IntelliJ product both IDEA and Pycharm 43 00:03:28.980 --> 00:03:33.660 and that causes them to display a warning about cannot find reference forward in turtle.py so 44 00:03:33.660 --> 00:03:38.480 if we hover this and have a I'll fix that part because it needs a blank line in the end 45 00:03:38.480 --> 00:03:45.730 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:45.730 --> 00:03:50.260 the program is fine so clearly not a problem with the code but IntelliJ can struggle 47 00:03:50.260 --> 00:03:54.709 with importing from some modules and noticed that importing the module from 48 00:03:54.709 --> 00:03:59.910 time using time module I should say using the the sleep method had no problems it found 49 00:03:59.910 --> 00:04:04.470 that ok so warnings are useful or will become less useful if they are incorrect because 50 00:04:04.470 --> 00:04:07.340 ultimately that can detract from the ones we should be paying attention to 51 00:04:07.340 --> 00:04:12.659 and start ignoring them all together so in this scenario where you convinced 52 00:04:12.659 --> 00:04:13.480 it's a warning 53 00:04:13.480 --> 00:04:17.010 that it is a bug where we're pretty sure that it's a bug here right now with 54 00:04:17.010 --> 00:04:21.910 IntelliJ we can suppress them so they can be safely ignored what we can do is 55 00:04:21.910 --> 00:04:26.639 do that and one way is on a statement by statement basis so come up here and add a new 56 00:04:26.639 --> 00:04:39.300 line for we can put a....... 57 00:04:39.300 --> 00:04:44.570 you can see what happens as soon as I finish typing that the a warning disappeared so that handles 58 00:04:44.570 --> 00:04:49.729 suppressing the error or the warning I should say just on a single line it would 59 00:04:49.729 --> 00:04:54.930 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:54.930 --> 00:04:59.160 disable it for all future turtke commands which isprobably be wise in this 61 00:04:59.160 --> 00:04:59.979 case 62 00:04:59.979 --> 00:05:05.000 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:05.000 --> 00:05:11.990 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:11.990 --> 00:05:17.260 unresolved attributes of turtle as ignored click on that you can see that 65 00:05:17.260 --> 00:05:21.330 then disappeared all the warnings and we got the green tick which means 66 00:05:21.330 --> 00:05:28.710 as far as IntelliJ is concern everything is ok so after doing that you could delete line 4 because the 67 00:05:28.710 --> 00:05:33.410 command on line 6 when we right clicked it overwrote that command anyway 68 00:05:33.990 --> 00:05:38.460 we have taken those warnings off let us just say for some reason IntelliJ fix that error 69 00:05:38.460 --> 00:05:42.510 and it's now something you want to put back on again you want to enable 70 00:05:42.510 --> 00:05:48.380 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:48.380 --> 00:05:53.289 code to analyze click on Config Current File 72 00:05:53.289 --> 00:05:54.590 Analysis 73 00:05:54.590 --> 00:05:58.630 click on configure inspections and what you need to do is you need to scroll 74 00:05:58.630 --> 00:06:02.120 down and there will be a list of all these other bits and pieces you want one that says Python 75 00:06:02.120 --> 00:06:06.340 and if there has been a change made you will notice it will be showing in blue it is showing 76 00:06:06.340 --> 00:06:09.270 in blue here because we have made that change so what I'm going to do is come down 77 00:06:09.270 --> 00:06:14.740 to unresolved references and that's showing in blue again and if we want 78 00:06:14.740 --> 00:06:18.470 to get rid of that you can see here the ignore references we can add some more here 79 00:06:18.470 --> 00:06:25.409 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:25.409 --> 00:06:25.770 back 81 00:06:25.770 --> 00:06:31.349 and if we go back in to analyze again analyzed Config Current File Analysis 82 00:06:31.349 --> 00:06:33.430 configure inspections 83 00:06:33.430 --> 00:06:37.830 and looking back here now notice now its not in blue anymore and also come up here too 84 00:06:37.830 --> 00:06:42.120 Python that is no longer in blue either so that's a quick way to 85 00:06:42.120 --> 00:06:47.740 determine whether you actually made that any changes to this particular model or not 86 00:06:47.740 --> 00:06:51.720 so I'm gonna re add that again because it's just an annoying warning we do 87 00:06:51.720 --> 00:06:56.919 wanna get rid off so I'm gonna click on right here and select the 88 00:06:56.919 --> 00:07:04.479 pop up click on mark all unresolved attributes of turtle as ignored again and we get your green arrow back again 89 00:07:04.479 --> 00:07:09.870 so just running the app again so you can see we got our drawing sits there for a few seconds 90 00:07:09.870 --> 00:07:15.360 four seconds to be exact and closes down so the pause is a bit arbitrary so the correct way to 91 00:07:15.360 --> 00:07:20.039 give us time to view the reptilian artwork is to use the turtles module done 92 00:07:20.039 --> 00:07:24.000 method this also me so we can get rid of the time import so I'm going to 93 00:07:24.000 --> 00:07:33.449 delete the time import and instead of time.sleep we are gonna put.... 94 00:07:33.449 --> 00:07:34.000 essentially now 95 00:07:34.000 --> 00:07:39.289 the masterpiece is going to stay there until we close the window so we just sit here for a few seconds 96 00:07:39.289 --> 00:07:42.180 to make sure you can see nothing's happening so we literally had to come 97 00:07:42.180 --> 00:07:45.789 over here and close it down for that change to take effect and for our program to 98 00:07:45.789 --> 00:07:50.639 exit the other thing to remember was with using the import command to import 99 00:07:50.639 --> 00:07:54.199 the turtle module we can then use all the objects it provides by prefixing 100 00:07:54.199 --> 00:07:58.810 them with turtle and a dot so turtle.forward as you saw turtle.right etc 101 00:07:58.810 --> 00:08:04.750 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:04.750 --> 00:08:08.860 from a module specifying just things we want to import as supposed to doing an 103 00:08:08.860 --> 00:08:12.810 import of everything and what I mean by that is I'm going to delete that line on the 104 00:08:12.810 --> 00:08:15.919 first line the import and we could do something like this instead of we can be 105 00:08:15.919 --> 00:08:24.759 more selective we can say from....so we are only going 106 00:08:24.759 --> 00:08:30.039 to import the first method called for and then by doing that I can get rid of 107 00:08:30.039 --> 00:08:35.180 this turtle. at the start and notice how it has found forward because its found and look for it 108 00:08:35.180 --> 00:08:39.110 in the turtle module so again essentially what we're doing is we're telling Python to import 109 00:08:39.110 --> 00:08:42.680 just the forward method from turtle and doing it this way means that we don't 110 00:08:42.680 --> 00:08:46.640 have to keep prefixing forward with turtle. all the time and if we run 111 00:08:46.640 --> 00:08:53.130 this we do get some errors and the errors on lines for 4 5 and 7 saying that 112 00:08:53.130 --> 00:09:00.279 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:00.279 --> 00:09:05.209 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:05.209 --> 00:09:12.730 works fine and sorry had to get rid of the turtle. before our commands and 115 00:09:12.730 --> 00:09:16.950 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:16.950 --> 00:09:22.339 quite nicely and we only imported those functions from the 117 00:09:22.339 --> 00:09:27.180 turtle module that we actually need to so close it down so doing it this way can be useful for 118 00:09:27.180 --> 00:09:30.649 using a particular object a lot in the program but the problem is it only 119 00:09:30.649 --> 00:09:34.220 import things that are specifically listed on the import line and as you saw 120 00:09:34.220 --> 00:09:38.459 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:38.459 --> 00:09:39.769 like.... 122 00:09:39.769 --> 00:09:43.560 ....which is a valid command 123 00:09:43.560 --> 00:09:49.560 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:49.560 --> 00:09:53.579 only specified the specific things we wanted and we don't need to specify turtle. 125 00:09:53.579 --> 00:09:57.600 and in actual fact turtle. doesn't work because as far as Python is concerned that 126 00:09:57.600 --> 00:10:05.870 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:05.870 --> 00:10:12.949 turtle circle and if I comment this line and run it again we name circle is not 128 00:10:12.949 --> 00:10:16.690 defined as well that's because we haven't specified circle up here on line 129 00:10:16.690 --> 00:10:22.329 1 so if wanted circle. to work I'll delete line 7 because we know how to do that we 130 00:10:22.329 --> 00:10:27.490 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:27.490 --> 00:10:32.000 to to work like this what we can do is below the from turtle command we can 132 00:10:32.000 --> 00:10:44.600 also put an import....so we should find if we run it now everything works fine 133 00:10:44.600 --> 00:10:49.410 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:49.410 --> 00:10:55.540 or started plotting the triangle so that's good so that works fine 135 00:10:55.540 --> 00:10:58.930 so this does demonstrate the two different ways an import can be used and the effect each 136 00:10:58.930 --> 00:11:03.339 it has on how you can use the imported objects generally speaking you wouldn't 137 00:11:03.339 --> 00:11:05.680 want to mix methods with the same module so 138 00:11:05.680 --> 00:11:08.699 in our example what you wanna do import turtle 139 00:11:08.699 --> 00:11:13.440 on line 2 you don't only just have an import turtle like on line 2 or you import the existing 140 00:11:13.440 --> 00:11:18.040 individual methods class variables and variables but rarely combined both 141 00:11:18.040 --> 00:11:22.180 because obviously that can lead to confusing code especially because turtle 142 00:11:22.180 --> 00:11:25.660 .forward would also work as well as forward now and that could be quite 143 00:11:25.660 --> 00:11:29.370 confusing to someone reading your code and wondering why things are different and 144 00:11:29.370 --> 00:11:34.110 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:34.110 --> 00:11:37.399 to use import that's definitely not recommended 146 00:11:37.399 --> 00:11:40.660 so it's possible to import everything or more actually almost everything from the 147 00:11:40.660 --> 00:11:42.690 module using import star 148 00:11:42.690 --> 00:11:49.040 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:49.040 --> 00:11:57.310 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:11:57.310 --> 00:12:10.069 command back.....so we should be able to run that so you can see that work ok and depending on 151 00:12:10.069 --> 00:12:14.790 your operating system you may get some errors here in my case i didn't get any 152 00:12:14.790 --> 00:12:18.730 errors and there was a tick their but IntelliJ may struggle when underlining the 153 00:12:18.730 --> 00:12:23.959 method calls in red but as you saw the program will still totally run anyway although you 154 00:12:23.959 --> 00:12:28.079 can import from a module in this way with a star which will import everything 155 00:12:28.079 --> 00:12:32.329 who's name does not start with an underscore doing so is frowned upon it's 156 00:12:32.329 --> 00:12:36.360 very useful to save typing when using an interactive session in idle for example 157 00:12:36.360 --> 00:12:39.880 but other than that you should really avoid this way of importing now one 158 00:12:39.880 --> 00:12:43.740 reason for that or you don't want to do that is that you actually have no idea what 159 00:12:43.740 --> 00:12:48.329 is being imported so an imported name can hid one of your objects or you can hide 160 00:12:48.329 --> 00:12:52.319 one of the imported names that you need and just to give you an idea that 161 00:12:52.319 --> 00:12:56.100 in this example we got to demonstrate why this could be a problem 162 00:12:56.100 --> 00:12:59.829 so lets just say we're going to use the import start so what I'm going to do is put 163 00:12:59.829 --> 00:13:11.589 something on line 1 ill put.....and we got our from turtle 164 00:13:11.589 --> 00:13:14.759 import.... 165 00:13:14.759 --> 00:13:26.209 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:26.209 --> 00:13:31.399 we get a picture drawing just fine nothing happens until we close down but once we 167 00:13:31.399 --> 00:13:35.309 do closed and you can see that we you get the message printed is a 168 00:13:35.309 --> 00:13:41.360 function address of the turtle done method and not the string message Well 169 00:13:41.360 --> 00:13:46.110 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:46.110 --> 00:13:55.679 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:55.679 --> 00:14:00.079 so literally just changed lines 1 and 3 around and done nothing else if we 172 00:14:00.079 --> 00:14:07.550 run this again it will work it will draw the picture or starts to but then there's actually a 173 00:14:07.550 --> 00:14:12.029 problem and the problem is when it reaches the done line which is on line 174 00:14:12.029 --> 00:14:16.009 10 and it says string object is not callable so what's happening of course 175 00:14:16.009 --> 00:14:21.709 is its trying to call or to treat the string as a method and executing it rather than 176 00:14:21.709 --> 00:14:25.829 using the done method from the turtle import now of course this second 177 00:14:25.829 --> 00:14:29.949 example in particular but also the first one before I swap them over are really quite 178 00:14:29.949 --> 00:14:33.749 contrived because we called the done method with only a few lines defining the 179 00:14:33.749 --> 00:14:37.110 string variable so that would be very hard to miss the fact that something 180 00:14:37.110 --> 00:14:40.839 wasn't right ordinarily but however if you consider a program with hundreds of 181 00:14:40.839 --> 00:14:45.749 lines of code mistakes like this would be much easier to make and harder to find so 182 00:14:45.749 --> 00:14:49.929 bottom line here is that unless you're working in an interactive session do not 183 00:14:49.929 --> 00:14:55.049 do an import star so I'm gonna finish the video here now and in the next video 184 00:14:55.049 --> 00:14:59.129 we are gonna start talking about the Python Standard library so see you in that video