Python Import Statement

As the Python programming language contains a vast array of functionality, it is split up in to Modules, or Libraries, as they are sometimes referred to. The benefit of this is that a Python program need only include the Modules that contains the functionality it requires to run and nothing more. The Modules which make up the Python programming language that are available when Python is installed are sometimes collectively referred to as the ‘Standard Library’.

As well as the Modules found in the ‘Standard Library’ it is also possible to create user defined Modules that can be reused. These are simply a file containing Python code with a ‘.py’ extension.

In order to include a Module, whether it be from the ‘Standard Library’, or one that is user defined, the ‘import’ statement needs to be used, which is placed at the top of a Python program. The example below imports the ‘random’ Module, which contains functionality to generate random numbers.

import random

Below are some examples of built-in modules that come as part of the ‘Standard Libary’, together with a link to a full list of modules in the Python documentation.

  • abc – Provides a means to define abstract base classes.
  • csv – Allows for the reading and writing of CSV, or Comma Separated Value, files.
  • html – Facilitates the manipulation of HTML.
  • json – Encoding and decoding of JSON (JavaScript Object Notation) files.
  • logging – Provides event logging functionality for applications and libraries.
  • math – Includes mathematical functions.
  • os – Facilitates the use of operating system dependent functionality.
  • pdb – Python debugger for debugging programs.
  • random – Pseudo-random number generator.
  • re – Regular Expression operations.
  • shutil – Provides high-level operations on files and folders, including coping and deleting.
  • socket – Can be used to find out IP address and domain information. A domain name can be used to find out its IP address, and similarly, an IP address can be used to find out its domain name.
  • traceback – Allows for the extraction, formatting and printing of Python program stack traces.
  • Full Python module index.

In order to use a Module that does not come as part of the ‘Standard Library’, it must first be installed. Instructions for doing this are usually provided with the Module.

Below are some examples of third party Modules:

  • BeautifulSoup – Provides a means to extract data from HTML and XML files.
  • Django – A web framework for rapid development.
  • Matplotlib – A plotting library for representing data in tables and graphs, which can be output in a variety of formats.
  • NsLookup - Provides high-level DNS lookups in Python (on top of dnspython dns.resolver).
  • NumPy – A scientific library that provides multi-dimensional arrays and fast operators and routines for manipulating these arrays.
  • OpenPyXL – A library for ready from and writing to Excel 2010 files and later.
  • Pandas – Provides easy to use data structures and data analysis tools.
  • Psutil (process and system utilities) – A cross-platform library for retrieving information on running processes and system utilisation (CPU, memory, disks, network, sensors).
  • PyAutoGUI – Provides a means to programmatically control the mouse and keyboard.
  • PyCrypto – Cryptographic modules for Python.
  • PyGame – A cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries.
  • PyPDF – A toolkit for creating and manipulating PDF documents, including splitting, merging together, cropping, and transforming the pages of PDF files, as well as retrieving text and meta data from PDFs.
  • PyMongo – A Python driver for the MongoDB database.
  • PyMySQL – A pure-Python MySQL client library, based on PEP 249.
  • Python-Docx – Allows for the creation and updating of Microsoft Word (.docx) files.
  • Python-Whois - A module which produces parsed WHOIS data for a given domain.
  • Requests – An HTTP library for interacting with APIs and crawling web pages.
  • Selenium – Provides a means to automate web browsers to carry out automated testing of web application and complete other repetitive web based tasks.
  • Simplejson – A JSON encoder and decoder.
  • SQLAlchemy – The Python SQL Toolkit and Object Relational Mapper.