1 00:00:02,290 --> 00:00:06,940 Hello welcome to this tutorial on packages and modules 2 00:00:10,320 --> 00:00:14,710 left to understand packages and modules using some examples. 3 00:00:16,680 --> 00:00:19,550 Say for example have created a directory in my system. 4 00:00:20,830 --> 00:00:34,000 Called basic math and this directory has four files and each of these files have an extension. 5 00:00:34,120 --> 00:00:46,930 Dot B white indicating these files contain python script or Python code and each of these files is used 6 00:00:46,930 --> 00:00:50,890 to perform a specific task. 7 00:00:51,030 --> 00:01:01,500 So for example the first file addition dot B Y is used to perform additions on some input variables 8 00:01:02,060 --> 00:01:04,420 or input parameters. 9 00:01:04,440 --> 00:01:14,920 Similarly we have the other file subtraction multiplication and area performing different tasks so instead 10 00:01:14,920 --> 00:01:22,620 of having the code the entire code for different tasks in a single file. 11 00:01:22,750 --> 00:01:32,030 We have split the code into small parts and each of these but is called a module 12 00:01:34,770 --> 00:01:44,010 and a module in Python is a file containing Python definitions and statements in a module and defined 13 00:01:44,010 --> 00:01:50,880 functions classes and variables in order to define a module. 14 00:01:51,050 --> 00:01:59,580 We need to simply use a text editor to type some Python code and save it with the dot B Y extension 15 00:02:00,810 --> 00:02:05,330 and any such file is automatically considered a Python module. 16 00:02:06,560 --> 00:02:15,080 And the module name is molded out of a file name by removing the dot B Y suffix. 17 00:02:15,080 --> 00:02:27,790 So the file name here is addition dot B Y and the module name is Ed. So if you remove the extension 18 00:02:27,850 --> 00:02:38,330 part b y it becomes the module name so a module in Python is a file containing Python definitions and 19 00:02:38,330 --> 00:02:49,150 statements and the modules are built separately and more or less independently and any file which has 20 00:02:49,150 --> 00:03:01,470 the extension dot B Y and counters of proper Python code is considered a Python module next we will 21 00:03:01,470 --> 00:03:05,100 talk about packages in the previous tutorials. 22 00:03:05,100 --> 00:03:07,860 We were introduced to Python packages. 23 00:03:08,040 --> 00:03:19,850 We have downloaded packages from the Python packaging index and a python package is a collection of 24 00:03:20,210 --> 00:03:31,490 modules and we can build applications by putting all of these modules together and a package is basically 25 00:03:31,550 --> 00:03:40,740 a directory but Python modules and a file with the name underscored underscored in it underscored underscore 26 00:03:40,950 --> 00:03:42,520 dot be white. 27 00:03:42,530 --> 00:03:50,630 This means that every directory inside of the Python part which contains a file name double underscored 28 00:03:51,560 --> 00:03:52,000 in it. 29 00:03:52,010 --> 00:03:55,940 Dot be y will be treated as a package by Python. 30 00:03:56,060 --> 00:04:04,490 A package is basically a directory but Python modules and a file with the name double underscore in 31 00:04:04,490 --> 00:04:08,570 a dot B white. 32 00:04:08,590 --> 00:04:17,280 This means that every directory inside of the python but which contains a file named w underscored in 33 00:04:17,280 --> 00:04:25,710 it will be treated as a package by Python so basic underscore might. 34 00:04:25,870 --> 00:04:36,150 In our example is a package and here we have modules in this package. 35 00:04:36,310 --> 00:04:39,470 And what does this inert file contain. 36 00:04:40,590 --> 00:04:49,350 This the inert file can be left empty but we generally place the initialization code for that package 37 00:04:49,440 --> 00:04:51,170 in this file. 38 00:04:51,240 --> 00:05:00,150 We will talk about the initialization code in the upcoming topics but for now we must remember that 39 00:05:00,210 --> 00:05:04,890 a directory must contain a file named double underscore in it. 40 00:05:04,890 --> 00:05:08,010 In order for Python to consider it as a package 41 00:05:10,840 --> 00:05:21,130 Python package can have sub packages and modules in order to further organize the files and packages 42 00:05:21,130 --> 00:05:25,920 can contain nested sub packages to arbitrary length. 43 00:05:25,960 --> 00:05:31,110 So in this example here we have two sub packages. 44 00:05:31,390 --> 00:05:39,910 And in order for Python to treat these two folders or directories as sub packages we have placed the 45 00:05:40,510 --> 00:05:45,320 double underscored in it Phi in these sub packages as well. 46 00:05:45,820 --> 00:05:55,480 So Python packages a collection of modules and these modules co-operate or work together in order to 47 00:05:55,510 --> 00:05:58,570 implement a task next. 48 00:05:58,570 --> 00:06:04,740 Python packages can have sub packages to arbitrary depth. 49 00:06:04,850 --> 00:06:11,740 Next a directory must contain the doublet underscored in it file in order for Python to consider it 50 00:06:11,860 --> 00:06:13,270 as a package. 51 00:06:14,160 --> 00:06:18,120 So we have understood what modules and packages are. 52 00:06:18,870 --> 00:06:21,620 Now let's go to the next example. 53 00:06:21,630 --> 00:06:31,890 So here I have defined two files a dot B Y and b dot B Y and in this module B I have declared the variable 54 00:06:32,040 --> 00:06:43,140 Z which is a global variable and then a function can and as you can see we have not put any function 55 00:06:43,140 --> 00:06:45,890 calls to this particular function. 56 00:06:46,500 --> 00:06:56,310 In the module B next I want to access the variables and the functions that are defined in module B from 57 00:06:56,310 --> 00:07:06,840 the module a in the global scope or the global namespace of the module b we have the attribute Z which 58 00:07:06,840 --> 00:07:19,300 is pointing to the object one and then we also have the function can which is pointing to the function 59 00:07:19,390 --> 00:07:29,410 object and these two attributes in the global namespace are accessible from anywhere in the in the file. 60 00:07:29,410 --> 00:07:37,420 B Not coming to the local scope we have two variables that are defined in the local scope and because 61 00:07:37,420 --> 00:07:42,470 they are defined within the function body of the function. 62 00:07:42,470 --> 00:07:48,690 Carl they are only accessible within this particular function. 63 00:07:48,700 --> 00:07:59,650 Now I want to access the variables and functions that are defined in the module B from module 8 and 64 00:07:59,830 --> 00:08:03,190 Python provide the import. 65 00:08:03,190 --> 00:08:10,650 And from statements in order to access the content the content of a module. 66 00:08:11,020 --> 00:08:19,930 So in order to access the content defined in the module B we will have to put an import statement in 67 00:08:19,930 --> 00:08:31,440 Module 8 so we will have the import statement and we are going to import the module be the python import 68 00:08:31,440 --> 00:08:41,430 statement gives the module a access to everything that is defined in the global scope or namespace of 69 00:08:41,760 --> 00:08:42,970 module B. 70 00:08:43,020 --> 00:08:57,690 So then we import module B in the file a dot B why this import statement gives access to the attribute 71 00:08:57,780 --> 00:09:01,500 in the global scope of module B. 72 00:09:01,500 --> 00:09:13,820 Remember we do not have a function call to the function that is defined in module B can so so we have 73 00:09:13,910 --> 00:09:23,120 access to the function call that is defined in the global namespace of module b and this function can 74 00:09:23,120 --> 00:09:36,150 be called from the module e using the dot notation so using the dot notation B dark Cal we can make 75 00:09:36,210 --> 00:09:47,190 a function call parse the arguments when Python encounters an import statement in a file it performs 76 00:09:47,250 --> 00:09:49,150 a sequence of steps. 77 00:09:49,290 --> 00:09:53,790 Now let's see the steps involved when importing a module.