Exploring Module and package control of python

In my python series till now we have practiced and understood the basic concept of how to get started to use python development. In my last series, we have completed the concept of variables, data types, conditional control and functional control. If you are serious to learn all those concepts then I would be recommended to please check all post about the basic of python in python category. Here is the link to python Category.

In this python series, we will learn about the python module and packages. Modules actually refer to a file containing Python statements and definitions. I mean If a file name is saved with   my_module.py then   my_module  is the module of the python.  So whenever we want to use this file function, statement or definition we can use by  import module statement.

In simple word, We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.

Let’s see this as an example to understand clearly. I am creating the python file name my_module.py and defined the function inside this file. Now I want to access this function to others file then I need to import this module to access this function.

my_module.py

Now I need to import this my_module inside the module.py which is another file.

module.py

Output: Hello

Here Now we can use the sayHello() function inside the module.py file. Even we can modify the import module name as any specific name. Let’s see this as an example.

Similarly, Python is providing the internal existing API for import as a module to use in your file. Let’s see few examples of existing API (exp: MATH) to import as a module to use in the current file.

If we want to import everything from this API then we can use the * in end of the import statement.

The similar concept of packages too. Generally, we are creating the package to make our project modular and structured. So that anybody can understand to see the structure what are the files are here and their uses. So here we can see few examples to understand how can we create the package and how can we import as a module in our current file.

Here I create the my_package.py file inside the com/sunil/python/package1.

my_package.py

Now how can we import this into the module.py file as a module.

Output:

Sum 6

Sum 11

Now we have a good understanding of the module and packages and their uses in python development. In my next tutorial, we will learn about the Data Structure of Python. If you have any issue or doubt then please send me in the comment box, I will try to post the solution.

If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.

Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on below section.

Happy coding 🙂

 

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

Your email address will not be published. Required fields are marked *