- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to develop a Python Module?
A file containing Python commands and definitions is referred to as a module. These files named .py which contain that contains Python code, such as example.py, and the name of the module is an example. Modules are used to divide down huge applications into smaller, more manageable files.
The prerequisites for using modules you should have Python 3 installed and a programming environment set up. If you don't already have one, you can refer to the installation and setup recommendations for a local programming environment.
Let us look at an example to create a simple python module. Let us make a python file ad.py
def add(x, y): return (x+y)
After doing so, upon saving the file as ad.py, a module named ad.py is created.
In Python, we can import definitions from one module into another or into the interactive interpreter. To accomplish this, we employ the import keyword as shown below.
Let us look at an example on how to import a python module. In the Python prompt, type the following to import our previously defined module ad.
import ad #importing module ad.py
The below code can be referred to for understanding how to access functions developed in a custom module in python.
import ad print(ad.add(9, 2))
The output generated for the above line of code, when the add function from the user-defined ad module has been called can be seen below.
Let us another example to develop a python module. Create a file called fibonacci.py and enter the following code in it −
def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() def fib2(n): # return Fibonacci series up to n result =  a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result
Now open your terminal and use cd command to change the directory containing this file and open the Python shell. Enter the following statements −
>>> import fibonacci >>> fibonacci.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibonacci.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Standard Python Modules
Python's standard library is extensive, providing a wide range of facilities, as evidenced by the lengthy table of contents listed below.
The library includes built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as Python modules that provide standardized solutions to many problems encountered in everyday programming. Examples of python standard modules are, math, queue, venv and zipfile.
Some of these modules are explicitly designed to encourage and improve Python program portability by abstracting away platform-specifics into platform-neutral APIs.
We can use the import statement to import a module and the dot operator to access the definitions within it. Here's an example.
#python program to import math module import math print("Pi value is:", math.pi)
The output generated is as follows.
Pi value is: 3.141592653589793
Renaming the imported Python Modules
Modules provide greater flexibility in code organization. This allows for a more concise and contextual name. In the, for example, we can import numpy as np. This makes our code more readable, but it also allows different modules to export members with the same name.
import module as xyx
NumPy is a Python library for manipulating arrays. It also includes functions for working with linear algebra, the Fourier transform, and matrices. Travis Oliphant created NumPy in 2005.
It is an open-source project that you are free to use. NumPy is an abbreviation for Numerical Python. NumPy strives to provide array objects that are up to 50 times faster than traditional Python lists.
The following line of code can be used to rename a python module while importing it.
import numpy as np
- How to develop a game in Python?
- How to write a python module?
- How to install a Python Module?
- How to develop programs with Python Namespaced Packages?
- How to develop a soft keyboard for Android?
- How to locate a particular module in Python?
- Develop Notepad using Tkinter in python
- How to retrieve Python module path?
- How to import a single function from a Python module?
- How to list all functions in a Python module?
- Import a module in Python
- How to develop a soft keyboard for Android using Kotlin?
- Develop a WiFi Scanner for Android
- How do I unload (reload) a Python module?
- How to prohibit a Python module from calling other modules?