A module is code that you can import into a running Python process. Python Modules
Any variables, functions and classes defined in a .py file, and any symbols defined by the appropriate calls to Python’s APIs in a binary libraries then become visible within your Python process.
Simple Example:
import keyword is used to import the module in python. In this example we are importing the math module
# import standard math module
import math
print(math.pi)
#The value of pi is
3.141592653589793
We need to load the module in our python code to use its functionality. Python provides two types of statements as defined below.
- import statement
- from-import statement
import statement Syntax
import module1,module2..
from-import statement
from module-name import *
Creating and Importing Modules
We can create a module by saving a Python file with the .py
file extension. It can then be imported into another .py
file with an import
statement.
For example, a separate .py file that was previously saved can be imported in other files:
import demo_game
# Program starts here...
Python Standard Modules
Python comes with several different built-in modules that provide a variety of functions. They include:
- The
collections
module provides additional collection types. - The
glob
module allows matching file paths per Unix shell rules. - The
json
module provides functions for dealing with JSON objects. - The
math
module provides useful mathematical functions. - The
random
module provides functions for dealing with random numbers. - The
time
module provides various functions for dealing with time.
Top 10 must learn Python Modules
1. Requets
Requests will allow you to send HTTP/1.1 requests using Python. With it, you can add content like headers, form data, multi part files, and parameters via simple Python libraries. It also allows you to access the response data of Python in the same way.
2. Numpy/Scipy
Scrapy is a free and open-source web-crawling framework written in Python and developed in Cambuslang. Originally designed for web scraping, it can also be used to extract data using APIs or as a general-purpose web crawler. It is currently maintained by Zyte, a web-scraping development and services company.
3. Scrapy
Scrapy is a free and open-source web-crawling framework written in Python and developed in Cambuslang. Originally designed for web scraping, it can also be used to extract data using APIs or as a general-purpose web crawler. It is currently maintained by Zyte, a web-scraping development and services company.
4. Matplotlib
Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.
5. Scikit-learn
scikit-learn is a free software machine learning library for the Python programming language. It features various classification, regression and clustering algorithms including support-vector machines.
6. wxPython
wxPython is a wrapper for the cross-platform GUI API wxWidgets for the Python programming language. It is one of the alternatives to Tkinter. It is implemented as a Python extension module.
7. SQLAlchemy
SQLAlchemy is an open-source SQL toolkit and object-relational mapper for the Python programming language released under the MIT License.
8. Simplejson
Simple, fast, complete, correct and extensible encoder and decoder for Python 2.5+ and Python 3.3+. It is pure Python code with no dependencies, but includes an optional C extension for a serious speed boost.
9. Pygame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language.
10. Natural Language Toolkit(NLTK)
The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and programs for symbolic and statistical natural language processing for English written in the Python programming language.
- A module is code that you can import into a running Python process.
- import keyword is used to import the module in python.
- We can create a module by saving a Python file with the .py file extension. Python Modules