Python 3

home

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'>






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 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:






finding third-party modules

Take some care when installing modules -- it is possible to install malicious code.



- [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






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






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!


Ex. 12.19 - 12.21






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






setting the PYTHONPATH system environment variable

Like the PATH for programs, this variable tells Python where to find modules.



demo setting the variable





[pr]