1 00:00:00,090 --> 00:00:10,580 The contents of a module are made available to the caller using the import and from statements when 2 00:00:10,580 --> 00:00:12,080 a module is access. 3 00:00:12,080 --> 00:00:21,670 Using these two statements Python performs the following steps first the first step is to search for 4 00:00:21,670 --> 00:00:22,610 the module. 5 00:00:22,780 --> 00:00:32,380 When a module is being imported into a file python will go through a search algorithm based on the search 6 00:00:32,530 --> 00:00:40,360 but and the search part is the list of directories available through says Dot. 7 00:00:40,670 --> 00:00:51,820 But and so this is the standard library module and part is the attribute of this module says Dot bar 8 00:00:52,810 --> 00:01:01,960 and says Dot but contained a list the directories that determine the interpreters search box for modules 9 00:01:02,530 --> 00:01:11,200 so it gives the order in which Python searches for modules in order to import the says Dot part variable 10 00:01:11,560 --> 00:01:21,430 is initialized from the following location so the first is the directive from which the input script 11 00:01:21,730 --> 00:01:31,560 was done or the current directly the interpreter is being run interactively so this means when you are 12 00:01:31,620 --> 00:01:39,810 running the program this entry is the directory can containing your program's script file when you're 13 00:01:39,810 --> 00:01:41,520 working interactively. 14 00:01:41,520 --> 00:01:50,230 This entry is the directory in which you're working that is the current working directory next the CIS 15 00:01:50,290 --> 00:01:58,240 dot but variable is initialized from the list of directories contained in the python part environment 16 00:01:58,250 --> 00:01:59,430 variable. 17 00:01:59,450 --> 00:02:07,610 The format for Python parties or is dependent but it should be similar to the part environment variable 18 00:02:07,610 --> 00:02:15,170 in your system and Python part is simply set to a list of user defined and platform specific names of 19 00:02:15,320 --> 00:02:22,260 directories that contain Python code files next. 20 00:02:22,330 --> 00:02:31,510 The easiest but variable is also initialized by installation dependent list of directories configured 21 00:02:31,600 --> 00:02:32,620 at the time. 22 00:02:32,620 --> 00:02:40,990 Python is installed so Python merges the home directory of the script that is being executed and any 23 00:02:40,990 --> 00:02:49,240 python by directories and the standard library directories and the result is a list of directory name 24 00:02:49,240 --> 00:02:55,540 strings that Python searches on each import of a new file. 25 00:02:55,540 --> 00:03:04,360 Now let us inspect the says Dot but variable from the command prompt in order to access the app attribute 26 00:03:04,360 --> 00:03:06,900 part of the source module. 27 00:03:06,910 --> 00:03:10,590 We will have to first import this module. 28 00:03:11,580 --> 00:03:15,340 So let us give the import statement. 29 00:03:16,280 --> 00:03:23,430 Import cease and then let's print the CIS Dot. 30 00:03:23,490 --> 00:03:25,080 But variable. 31 00:03:25,160 --> 00:03:33,860 So here we have the list of directives that Python is going to search when it searches in order to import 32 00:03:33,980 --> 00:03:34,570 a module. 33 00:03:35,940 --> 00:03:44,310 And the first entry as you can see here is an empty string which directs Python to search modules in 34 00:03:44,310 --> 00:03:55,040 the current working directory first say for example say we have found the module B in our example in 35 00:03:55,040 --> 00:04:00,300 one of the directories listed in these says Dot but variable. 36 00:04:00,350 --> 00:04:09,260 So let's go to the next step that Python needs to perform in order to import a module. 37 00:04:09,260 --> 00:04:16,770 So the second step is to translate all these statements in the module into byte code. 38 00:04:16,880 --> 00:04:26,360 So what is a byte code a bite code is a lower level language than Python and Python translate modules 39 00:04:26,720 --> 00:04:31,910 into byte court and then it's what machines or the interpreter. 40 00:04:32,920 --> 00:04:41,870 Execute these modules by interpreting their bite code and Python stores the byte code of the important 41 00:04:41,890 --> 00:04:54,670 modules inspect should captured files with the extension b y c so we have found the module for importing. 42 00:04:54,670 --> 00:05:02,320 Next we have translated All the python code in the module into byte code in the second step. 43 00:05:02,350 --> 00:05:07,850 Now the third step is to create a module object for the module. 44 00:05:08,200 --> 00:05:20,080 And initially it has an empty namespace module object import operations load module files into module 45 00:05:20,140 --> 00:05:22,390 object in memory. 46 00:05:22,390 --> 00:05:34,000 The module object contained the different attributes on variables that are accessible from outside the 47 00:05:34,000 --> 00:05:44,180 module so the module object contained the different attributes the variables that are accessible from 48 00:05:44,180 --> 00:05:47,320 the outside world. 49 00:05:47,340 --> 00:05:58,690 Odd outside of the module when the module object is created in the memory it is empty. 50 00:05:59,740 --> 00:06:12,460 Let us see when the module object get populated shortly and the last step of the import operation is 51 00:06:12,460 --> 00:06:16,270 to execute the bite code of the module. 52 00:06:17,410 --> 00:06:26,900 So all these statements in a module are executed and any assignment statement or any assignment made 53 00:06:26,900 --> 00:06:34,010 to the names during this step generate attribute of the resulting module object. 54 00:06:34,010 --> 00:06:42,810 So your module has been executed and now the module object needs to be populated 55 00:06:46,860 --> 00:06:58,200 so in the last step of the import statement all of the code Python code in the module B gets executed 56 00:06:58,680 --> 00:07:09,720 and the module object for module b get updated with the module name B and also the import statement 57 00:07:10,210 --> 00:07:19,260 when it imports a module it creates a reference to that module in the local namespace from the calling 58 00:07:19,260 --> 00:07:20,120 program. 59 00:07:20,430 --> 00:07:33,420 So the local namespace of module e is updated with a reference to the module B when we import a module 60 00:07:33,430 --> 00:07:37,620 we're going to refer to the whole module object. 61 00:07:37,620 --> 00:07:49,010 And what about the attributes defined in the global scope we can fetch these attributes using the dot 62 00:07:49,130 --> 00:07:52,100 notation like this. 63 00:07:52,100 --> 00:07:57,580 So if you want to call any of the attributes here you can use the dot notation. 64 00:07:57,920 --> 00:08:08,280 So here we are going to say B the module name dot the attribute name B dot Zi or if you want to call 65 00:08:08,340 --> 00:08:09,900 a function. 66 00:08:09,900 --> 00:08:18,320 This is how you can remember the variables or the attribute that are defined or part of the global scope 67 00:08:19,230 --> 00:08:26,110 remain in the modules private table and they're not meaningful in the local context. 68 00:08:26,160 --> 00:08:40,430 That means we cannot directly access the attribute Zi or add the function Carl directly in the module 69 00:08:40,530 --> 00:08:50,300 e in order to access these two variables we have to use d dot notation so the names of the attributes 70 00:08:50,450 --> 00:08:59,630 in the module must be prefixed by the module name we can import several modules using a single import 71 00:08:59,630 --> 00:09:09,710 statement and this is the syntax all of the module names are separated by comma next we can give alternate 72 00:09:09,710 --> 00:09:20,640 names to the modules to be imported and the import statement goes like this say for example import B 73 00:09:21,000 --> 00:09:23,000 as Sam. 74 00:09:23,120 --> 00:09:32,660 So in the calling program whenever you want to refer to module B hereafter you should use sample as 75 00:09:32,660 --> 00:09:45,540 the module name and module name B is not recognizable or recognized in the module e so if you want to 76 00:09:46,410 --> 00:09:52,760 fetch any of the attributes defined in module B in module eight from module e. 77 00:09:53,160 --> 00:09:58,960 This is how you refer sample dot Zi. 78 00:09:59,130 --> 00:10:08,910 So the object can be referenced by pra fixing the new module name or the alternate name. 79 00:10:08,910 --> 00:10:16,280 So in order to fetch the attribute it should be prefixed by the alternate name. 80 00:10:16,680 --> 00:10:24,110 So here I have created two modules a dot B Y and b dot B Y. 81 00:10:24,330 --> 00:10:32,160 So we have discussed before that in order to define a module we can use a text editor to type some Python 82 00:10:32,160 --> 00:10:37,980 code into the text file and save it with a dot B Y extension. 83 00:10:37,980 --> 00:10:48,970 And so I have two files here and b dot B Y is the module that is going to be imported into the module 84 00:10:49,120 --> 00:10:49,480 a. 85 00:10:49,780 --> 00:11:02,020 So in this module B I have to attribute in the global scope one as the attribute C and the other is 86 00:11:02,020 --> 00:11:13,560 the function can we want to call these attributes the attribute Z and the function card from the module. 87 00:11:13,560 --> 00:11:14,330 E here. 88 00:11:14,330 --> 00:11:21,170 So in order to do that we have put an import statement as a first statement in this module and then 89 00:11:21,170 --> 00:11:31,640 we have called the function call and after importing the module I also have a print function here. 90 00:11:31,790 --> 00:11:39,650 We have a print function that prints the content of the directory function the directory function is 91 00:11:40,310 --> 00:11:49,490 is used to find out the names defined in a given namespace after importing the module B we will see 92 00:11:49,490 --> 00:12:00,460 if the module B is included in the local namespace of module a no let's save both these files and then 93 00:12:03,350 --> 00:12:06,600 Randy file a from the command prompt. 94 00:12:06,680 --> 00:12:13,080 Now let execute the file a dot B Y from the command prompt 95 00:12:17,660 --> 00:12:28,550 so diff module B has been accessed from the module a and here are the print functions that got executed 96 00:12:29,000 --> 00:12:38,780 that are present in module B and then we also have placed a print function in module a in order to display 97 00:12:38,780 --> 00:12:46,790 the contents of the directory function and as you can see as you can see the directory function has 98 00:12:46,850 --> 00:12:56,840 return all the properties and the method even building properties which are D4 for all objects such 99 00:12:56,840 --> 00:13:06,110 as double underscore Doc double underscore name and so on and these special names are found in most 100 00:13:06,230 --> 00:13:16,430 or all name spaces and we're interested in this entry in the list the import statement as created a 101 00:13:16,430 --> 00:13:26,300 reference to module B in the local namespace of module 8 and the final print function has printed this 102 00:13:26,300 --> 00:13:33,430 line using the dot notation we have access the attribute Zi defined in module B. 103 00:13:33,680 --> 00:13:42,560 Now we will see different forms of the import statement the first one is the from import statement and 104 00:13:42,770 --> 00:13:56,180 this is the syntax from B that is the module name import the required attribute C Zi comma care and 105 00:13:56,180 --> 00:13:56,570 so on. 106 00:13:57,440 --> 00:14:06,530 So the forearm statement allows individual attributes from the module to be imported directly into the 107 00:14:06,620 --> 00:14:15,770 calling program or body calling programs namespace and also the module object. 108 00:14:15,830 --> 00:14:29,300 Now get updated with the attributes in the namespace Zi and can so we can now use the attributes Zi 109 00:14:29,840 --> 00:14:39,260 and Carl directly in our program without using the dot notation or without referring to the module name. 110 00:14:39,320 --> 00:14:48,410 So when we put these from import statement in this file a dot B Y we can directly access the attribute 111 00:14:48,830 --> 00:14:55,170 Z without using the dot notation or without using the module name as a prefix. 112 00:14:55,550 --> 00:15:05,030 Say for example D from import statement has imported only one attribute that is Z from module B. 113 00:15:05,090 --> 00:15:14,030 In that case we can only refer to this attribute in the file a dot B Y other attribute like function 114 00:15:14,030 --> 00:15:24,140 Cal cannot be accessed in the file a dot B Y because we have not imported this particular attribute 115 00:15:24,350 --> 00:15:33,330 during the import and the function Cal that has been defined in module B cannot be accessed in the file 116 00:15:33,450 --> 00:15:43,190 a dot B why now because this has not been copied to the local namespace of this file a dot B write another 117 00:15:43,190 --> 00:15:54,110 form of the import statement is this from B import start and this statement copies all of the names 118 00:15:54,230 --> 00:16:04,760 assigned a d global scope of the referenced module so after execution of the code in the module B the 119 00:16:04,760 --> 00:16:16,280 module object get updated with all of the attributes in the global scope also the calling programs namespace 120 00:16:16,640 --> 00:16:27,050 gets updated with all the attributes in the module object namespace so the namespace of defined Ada 121 00:16:27,070 --> 00:16:38,930 or b way of the calling program get updated with these attributes and we can directly access these attributes 122 00:16:39,230 --> 00:16:44,700 in the file Ada or b y without using the dot notation. 123 00:16:44,840 --> 00:16:54,050 So using this statement we can directly access the attributes that are part of the global namespace 124 00:16:54,110 --> 00:16:55,250 of module B.