Modules, or code libraries that can be called by a script to increase its programming potential, are either built into Python, or are created by third parties, and added later to Python. Most of these are written in Python, but a number of them are also written in other programming languages, and then "wrapped" in Python to make them available within Python scripts. Wrappers are also used to make other software available to Python, such as the tools built into Microsoft Excel.
Important Python modules
The OS (operating system) module
The os module, part of the standard library, is very useful for a number of regular operations within Python. The most used part of the os module is the os.path method, which allows the script to control file paths, and to divide them into directory paths and base paths. There is also a useful method, os.walk, which will "walk" a directory and return all files within the folders and the subfolders.
The sys (Python system) module
The sys module, part of the standard library, refers to the Python installation itself. It has a number of methods that will get information about the version of Python installed, as well as information about the script, and any "arguments" supplied to the script, using the sys.argv method. The sys.path method is very useful for appending the Python file path; practically, this means that folders containing scripts can be referenced by other scripts to make the functions they contain importable.
The CSV, XLRD, and XLWT modules
The csv, xlrd, and xlwt modules are used to read and write data spreadsheets. They can be very useful for extracting data from the spreadsheets and converting them into data for GIS analysis, or for writing out analysis results as spreadsheets when an analysis is complete. The csv module (which creates text file spreadsheets using text delimiters like commas) is a built-in module, while xlrd and xlwt (which read and write Excel files respectively) are not part of the standard library, but are installed along with ArcGIS and Python 2.7.
Commonly used built-in functions
There are a number of built-in functions that we will use throughout the book. The main ones are listed as follows:
- str: The string function is used to convert any other type of data into a string.
- int: The integer function is used to convert a string or float into an integer. To avoid an error, any string passed to the integer function must be a number such as '1'.
- float: The float function is used to convert a string or an integer into a float, much like the integer function.
Standard library modules
Commonly used standard library modules that must be imported are as follows:
- datetime: The datetime module has date and time information, and can convert date data formats
- math: The math module is for higher level math functions, such as getting a value for Pi or squaring a number
- string: The string module is used for string manipulations
- csv: The csv module is used for creating, accessing, and editing text spreadsheets.
Check out https://docs.python.org/2/library/ for a complete list of the built-in modules.