Python Module is nothing but a group of logically organized Python code.By grouping related code make it easier for user to use and understand. Inside your Python Modules you can define Variable,Functions,Classes etc.
Take a look at the example given below
- Save the following code as example.py in your Desktop(name it whatever you want)
- Here example.py is your Modules file,it is just like our other simple python script ,but if we want to use this module in our script , you have to add it to our script.To add Modules we have to use built-in keyword i.e “ import “.Lets create another file named it as example2.
- Here in above example the “import” statement import the module example.py into your script to use it,and you’ll get the fallowing output according to your code
- you can use “from module_name import funtion_name,variable_name etc.. ex–> from math import sqrt,pow,__name__
- Python Interpreter search for a specific module when you import it,it search in current directory if not found then search in each directory define in PYTHONPATH, else search in default path directory.
- When the interpreter encounters an import statement, it imports the module if the module is present in the search
path. A search path is a list of directories that the interpreter searches before importing a module
- If you want to see what is inside your module or any module just create a new file mod_info.py and type fallowing code as given in below image,it will output variables, function,classes that define in module
- For more details you can visit python official website https://docs.python.org/2/tutorial/modules.html
Hope guys you will understand the concept of Python Modules clearly.Try to implement it with more code,and if you find any difficulty, feel free to drop a comment on this post.