Introduction to Python
davidbpython.com
Modules in Python
importing built-in modules
Python comes with hundreds of preinstalled modules.
import sys # find and import the sys module
import json # find and importa the json module
print(sys.copyright) # the .copyright attribute points
# to a string with the copyright notice
# Copyright (c) 2001-2023 Python...
obj = json.loads('{"a": 1, "b": 2}') # the .loads attribute points to
# a function that reads str JSON data
print(type(obj)) # <class 'dict'>
- modules are files that contain Python code for use in our programs
- modules are made available through an import statement
- we often import several modules in a given script
- following import, we can access the module code through its attributes
- each attribute points to a function, string, list, etc. that is a global variable in the module
other module import patterns
These patterns are purely for convenience when needed.
Abbreviating the name of a module:
import json as js # 'json' is now referred to as 'js'
obj = js.loads('{"a": 1, "b": 2}')
Importing a module variable into our program directly:
from json import loads # making the 'loads' function part of the global namespace
obj = loads('{"a": 1, "b": 2}')
Please note that this does not import only a part of the module: the entire module code is still evaluated and imported.
built-in module examples
Each module has a specific focus.
- The sys module has functions that let us work with python's interpreter, and how it works with the operating system
- The os module has functions that let us work with the operating system's files, folders and other processes
- The datetime module has functions that let us easily calculate a date into the future or past, or compare two dates
- The urllib2 module has functions that let us easily make HTTP requests over the internet
the python standard distribution of modules
Modules included with Python are installed when Python is installed -- they are always available.
Python provides hundreds of supplementary modules to perform myriad tasks. The modules do not need to be installed because they come bundled in the Python distribution -- that is, they are installed at the time that Python itself is installed.
The documentation for the standard library is part of the official Python documentation (search for "Python documentation").
Python modules cover a wide range of tasks and specialized purposes:
- various string-related services
- specialized containers (type-specific lists and dicts, pseudohashes, etc.)
- math calculations and number generation
- file and directory manipulation
- persistence (saving data on disk)
- data compression and archiving (e.g., creating zip files)
- encryption
- networking and interprocess (program-to-program) communication
- internet tasks: web server, web client, email, file transfer, etc.
- XML and HTML parsing
- multimedia: audio and image file manipulation
- GUI (graphical user interface) development
- code testing
- etc...
finding third-party modules
Take some care when installing modules -- it is possible to install malicious code.
- We generally find third-party modules by doing web searches, or from colleagues
- When we find a module that meets our needs, we should do some research to make sure it's the one we want and need -- check online for references to it, examine the project's home page, make sure it is in active development
- Although it's very rare, sometimes bad actors have created modules designed to pass viruses. These modules may be named similarly to popular modules
- We must always be careful when selecting a module to install
- [demo: searching for powerpoint module, verifying ]
installing modules
Third-party modules must be downloaded and installed into your Python distribution.
command to use to install a 3rd party module:
david@192 ~ % pip install pandas
or, use this command if you have installed Anaconda or Miniconda Python:
david@192 ~ % conda install pandas # installs pandas
- third-party modules are not part of the Python distribution but have been created for the public to use
- thousands of these modules are free and available on demand
- the pip utility is installed along with Python
- if you have installed Anaconda or Miniconda, try the conda utility first, because you may find a module that is quality-controlled to work with your distribution
PyPI: the python package index
This index contains links to all modules ever added by anyone to the index.
Search for any module's home page at the PyPI website:
https://pypi.python.org/pypi
- PyPI is the definitive index of modules written in Python
- there are more than 70,000 projects uploaded there, from serious modules used by millions of developers, to half-baked ideas that someone decided to share prematurely
- usually, we will not search for modules here; we will hear about a module from an online article or through a search
user-defined modules
A module of our own design may be saved as a .py file.
messages.py: a simple Python module that prints messages
import sys
def print_warning(msg):
print(f'Warning! {msg}')
test.py: a Python script that imports messages.py
import messages
# accessing the print_warning() function
messages.print_warning('Look out!') # Warning! Look out!
- we can also build our only modules; one way is by creating a .py file with module code
- upon import, the entire module is read, compiled and executed
- global variables in the module then become attributes of the module
module search path
Python must be told where to find our own custom modules.
To view the currently used module search paths, we can use sys.path
import sys
print(sys.path) # shows a list of strings, each a directory
# where modules can be found
- the sys.path list contains all paths that your distribution of Python uses to store modules
- to add our own folders (i.e., containing modules we'd like to import) we can augment this list with the PYTHONPATH environment variable (next)
setting the PYTHONPATH system environment variable
Like the PATH for programs, this variable tells Python where to find modules.
- when we import a module, Python needs to find it
- modules may be located in any of several directories (stored in sys.path)
- to extend this list and add our own directories, we add them to the PYTHONPATH
- when Python starts up to run a program, it looks for the PYTHONPATH variable and if found, adds the paths specified there to sys.path
- for more specific instructions, please see your supplementary documents.
[pr]