avatarAayushi Johari

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4494

Abstract

ector-tag">a</span>,b) <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(addition)</span></span></pre></div><p id="4be5">In the above code, we have created an alias using the “<b>as”</b> keyword. The output of the above code will be the addition of the two numbers a and b using the logic specified in the add function in the calc.py module.</p><p id="9164">Let us take a look at another approach.</p><div id="b232"><pre><span class="hljs-keyword">from</span> calc import * a = 20 b = 30

<span class="hljs-built_in">print</span>(<span class="hljs-built_in">add</span>(a,b))</pre></div><p id="3421">In the above code, we have imported all the functions using the asterisk and we can simply mention the function name to get the results.</p><h2 id="2eba">Python Module Path</h2><p id="ceb4">When we import a module, the interpreter looks for the module in the build-in modules directories in sys.path and if not found, it will look for the module in the following order:</p><div id="c4c5"><pre>import sys <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(sys.path)</span></span></pre></div><p id="e2b0">When you run the above code, you will get the list of directories. You can make changes in the list to create your own path.</p><p id="badd">Built-in modules are written in C and integrated with python interpreter. Each built-in module contains resources for certain specific functionalities like Operating system management, disk input/output etc.</p><p id="bb0d">The standard library also has many python scripts containing useful utilities. There are several built-in modules in python at our disposal that we can use whenever we want.</p><p id="c18c">To get the list of all the modules in python, you can write the following command in the python console.</p><div id="8b41"><pre><span class="hljs-function"><span class="hljs-title">help</span><span class="hljs-params">(<span class="hljs-string">'modules'</span>)</span></span></pre></div><p id="4407">You will get a list of all the modules in python. Below are a few modules in python.</p><figure id="6cdb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*h38pMb4G0mz-ervaPnMnJg.png"><figcaption></figcaption></figure><h2 id="92d3">dir( ) Built-in Function</h2><p id="7c23">It returns a sorted list of strings containing the names defined in a module. The list contains the names of all the variables, functions, classes, etc.</p><div id="3748"><pre><span class="hljs-variable">import</span> <span class="hljs-variable">calc</span> <span class="hljs-function"><span class="hljs-title">print</span>(<span class="hljs-title">dir</span>(<span class="hljs-variable">calc</span>))</span></pre></div><p id="9ac7">You will get the list output like this:</p><figure id="cdf6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lk6bDa0nsLCXWUChL0h8bQ.png"><figcaption></figcaption></figure><p id="afe1">Similarly, you can get the names defined in any module using the dir( ) function.</p><p id="13cf">In this article, we have learned about modules in python, how we can create a module and use it in the program. We have also learned about the built-in modules in python. Python programming language has enormous applications and with the use of modules, the task becomes easier, maintainable and efficient. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to <a href="https://www.edureka.co/blog/?utm_source=medium&amp;utm_medium=content-link&amp;utm_campaign=python-modules">Edureka’s official site.</a></p><p id="9744">Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.</p><blockquote id="cf64"><p>1. <a href="https://readmedium.com/machine-learning-classifier-c02fbd8400c9">Machine Learning Classifier in Python</a></p></blockquote><blockquote id="faa6"><p>2. <a href="https://readmedium.com/python-scikit-learn-cheat-sheet-9786382be9f5">Python Scikit-Learn Cheat Sheet</a></p></blockquote><blockquote id="3dda"><p>3. <a href="https://readmedium.com/python-libraries-for-data-science-and-machine-learning-1c502744f277">Machine Learning Tools</a></p></blockquote><blockquote id="0ea9"><p>4. <a href="https://readmedium.com/python-libraries-for-data-science-and-machine-learning-1c502744f277">Python Libraries For Data Science And Machine Learning</a><

Options

/p></blockquote><blockquote id="0f18"><p>5. <a href="https://readmedium.com/how-to-make-a-chatbot-in-python-b68fd390b219">Chatbot In Python</a></p></blockquote><blockquote id="93df"><p>6. <a href="https://readmedium.com/collections-in-python-d0bc0ed8d938">Python Collections</a></p></blockquote><blockquote id="2b9b"><p>7. <a href="https://readmedium.com/web-scraping-with-python-d9e6506007bf">Web Scraping With Python</a></p></blockquote><blockquote id="6836"><p>8. <a href="https://readmedium.com/python-developer-skills-371583a69be1">Python developer Skills</a></p></blockquote><blockquote id="d1e8"><p>9. <a href="https://readmedium.com/oops-interview-questions-621fc922cdf4">OOPs Interview Questions and Answers</a></p></blockquote><blockquote id="b0f5"><p>10. <a href="https://readmedium.com/python-developer-resume-ded7799b4389">Resume For A Python Developer</a></p></blockquote><blockquote id="7fc5"><p>11. <a href="https://readmedium.com/exploratory-data-analysis-in-python-3ee69362a46e">Exploratory Data Analysis In Python</a></p></blockquote><blockquote id="e378"><p>12. <a href="https://readmedium.com/python-turtle-module-361816449390">Snake Game With Python’s Turtle Module</a></p></blockquote><blockquote id="5f96"><p>13. <a href="https://readmedium.com/python-developer-salary-ba2eff6a502e">Python Developer Salary</a></p></blockquote><blockquote id="4d92"><p>14.<a href="https://readmedium.com/principal-component-analysis-69d7a4babc96"> Principal Component Analysis</a></p></blockquote><blockquote id="4d96"><p>15. <a href="https://readmedium.com/python-vs-cpp-c3ffbea01eec">Python vs C++</a></p></blockquote><blockquote id="75b2"><p>16. <a href="https://readmedium.com/scrapy-tutorial-5584517658fb">Scrapy Tutorial</a></p></blockquote><blockquote id="aaa8"><p>17. <a href="https://readmedium.com/scipy-tutorial-38723361ba4b">Python SciPy</a></p></blockquote><blockquote id="092a"><p>18. <a href="https://readmedium.com/least-square-regression-40b59cca8ea7">Least Squares Regression Method</a></p></blockquote><blockquote id="284e"><p>19. <a href="https://readmedium.com/jupyter-notebook-cheat-sheet-88f60d1aca7">Jupyter Notebook Cheat Sheet</a></p></blockquote><blockquote id="0035"><p>20. <a href="https://readmedium.com/python-basics-f371d7fc0054">Python Basics</a></p></blockquote><blockquote id="38ef"><p>21. <a href="https://readmedium.com/python-pattern-programs-75e1e764a42f">Python Pattern Programs</a></p></blockquote><blockquote id="dec1"><p>22. <a href="https://readmedium.com/generators-in-python-258f21e3d3ff">Generators in Python</a></p></blockquote><blockquote id="2c6b"><p>23. <a href="https://readmedium.com/python-decorator-tutorial-bf7b21278564">Python Decorator</a></p></blockquote><blockquote id="c659"><p>24.<a href="https://readmedium.com/spyder-ide-2a91caac4e46"> Python Spyder IDE</a></p></blockquote><blockquote id="137f"><p>25. <a href="https://readmedium.com/kivy-tutorial-9a0f02fe53f5">Mobile Applications Using Kivy In Python</a></p></blockquote><blockquote id="713c"><p>26. <a href="https://readmedium.com/best-books-for-python-11137561beb7">Top 10 Best Books To Learn & Practice Python</a></p></blockquote><blockquote id="c21b"><p>27. <a href="https://readmedium.com/robot-framework-tutorial-f8a75ab23cfd">Robot Framework With Python</a></p></blockquote><blockquote id="27df"><p>28. <a href="https://readmedium.com/snake-game-with-pygame-497f1683eeaa">Snake Game in Python using PyGame</a></p></blockquote><blockquote id="4b00"><p>29. <a href="https://readmedium.com/django-interview-questions-a4df7bfeb7e8">Django Interview Questions and Answers</a></p></blockquote><blockquote id="a37e"><p>30. <a href="https://readmedium.com/python-applications-18b780d64f3b">Top 10 Python Applications</a></p></blockquote><blockquote id="e8e9"><p>31. <a href="https://readmedium.com/hash-tables-and-hashmaps-in-python-3bd7fc1b00b4">Hash Tables and Hashmaps in Python</a></p></blockquote><blockquote id="98ac"><p>32. <a href="https://readmedium.com/whats-new-python-3-8-7d52cda747b">Python 3.8</a></p></blockquote><blockquote id="5445"><p>33. <a href="https://readmedium.com/support-vector-machine-in-python-539dca55c26a">Support Vector Machine</a></p></blockquote><blockquote id="2d25"><p>34. <a href="https://readmedium.com/python-tutorial-be1b3d015745">Python Tutorial</a></p></blockquote><p id="0ac2"><i>Originally published at <a href="https://www.edureka.co/blog/python-modules/">https://www.edureka.co</a> on May 14, 2019.</i></p></article></body>

How To Create and Use Python Modules?

Python Modules — Edureka

Python programming language is one of the most popular language nowadays. It has numerous applications and developers are switching over to python for the implementation it provides us with. The modular programming approach where the code is broken down into separate parts is where python modules come into the picture. This article will help you understand the above topic in detail.

Following are the topics that will be covered in this article:

  • What Are Python Modules?
  • How To Create Python Modules?
  • How To Use Python Modules?
  • Built-in Modules In Python

What Are Python Modules?

Modules are simply a ‘program logic’ or a ‘python script’ that can be used for a variety of applications or functions. We can declare functions, classes, etc in a module.

The focus is to break down the code into different modules so that there will be no or minimum dependencies on one another. Using modules in a code helps to write a lesser line of codes, a single procedure developed for reuse of the code as well. It also eliminates the need to write the same logic again and again.

One more advantage of using modules is that the programs can be designed easily since a whole team works only on a part or module of the entire code.

Let us try to understand this with an example:

Suppose you want to make a program for a calculator. There will be operations like addition, subtraction, multiplication, division, etc.

We will break the code into separate parts, we can simply create one module for all these operations or separate modules for each of the operations. And then we can call these modules in our main program logic.

The idea is to minimize the code, and if we create modules, it doesn’t mean we can only use it for this program, we can even call these modules for other programs as well.

Now that we have understood the concept of modules, let us try to understand how we can create a module in python.

How To Create Modules In Python?

Creating a module in python is similar to writing a simple python script using the .py extension. For the above example, lets try to make a module for the various operations.

def add(x,y):
     return x + y
 
def sub(x, y):
     return x - y
 
def prod(x, y):
    return x * y
 
def div(x, y):
    return x / y

Save the above code in a file Calc.py. This is how we create a module in python. We have created different functions in this module. We can use these modules in our main file, let us take a look at how we are going to use them in a program.

How To Use Python Modules?

We will use the import keyword to incorporate the module into our program, from keyword is used to get only a few or specific methods or functions from a module. Let us see what are different methods to use a module in your program.

Let us say we have our file with a name main.py.

import calc as a
a = 10
b = 20
 
addition = a.add(a,b)
print(addition)

In the above code, we have created an alias using the “as” keyword. The output of the above code will be the addition of the two numbers a and b using the logic specified in the add function in the calc.py module.

Let us take a look at another approach.

from calc import *
a = 20
b = 30
 
print(add(a,b))

In the above code, we have imported all the functions using the asterisk and we can simply mention the function name to get the results.

Python Module Path

When we import a module, the interpreter looks for the module in the build-in modules directories in sys.path and if not found, it will look for the module in the following order:

import sys 
print(sys.path)

When you run the above code, you will get the list of directories. You can make changes in the list to create your own path.

Built-in modules are written in C and integrated with python interpreter. Each built-in module contains resources for certain specific functionalities like Operating system management, disk input/output etc.

The standard library also has many python scripts containing useful utilities. There are several built-in modules in python at our disposal that we can use whenever we want.

To get the list of all the modules in python, you can write the following command in the python console.

help('modules')

You will get a list of all the modules in python. Below are a few modules in python.

dir( ) Built-in Function

It returns a sorted list of strings containing the names defined in a module. The list contains the names of all the variables, functions, classes, etc.

import calc
print(dir(calc))

You will get the list output like this:

Similarly, you can get the names defined in any module using the dir( ) function.

In this article, we have learned about modules in python, how we can create a module and use it in the program. We have also learned about the built-in modules in python. Python programming language has enormous applications and with the use of modules, the task becomes easier, maintainable and efficient. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Web Scraping With Python

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21. Python Pattern Programs

22. Generators in Python

23. Python Decorator

24. Python Spyder IDE

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

Originally published at https://www.edureka.co on May 14, 2019.

Python
Python Module
Data Science
Python3
Python Programming
Recommended from ReadMedium