Python Libraries

Distinguishing between modules, packages and libraries.

Meagan Rossi
2 min readOct 14, 2020
Image Source: nccde.org

Modules

Modules are simply any file that contains code. A helpful way to keep Jupyter Notebooks looking professional and easy to access is to create separate modules (modular programming) for definitions and other backend code in a separate file.

These modules can be created in an IDE or text editor. They must be .py files and they are a quick way to import functions and objects. When calling a module into your Jupyter Notebook, be sure to include the .py file in the same folder as your notebook, or refer to the path through sys.path.append('...')

Tips:

  • Keep your module updated whenever changes are made. There are two ways to do this: include the magic command to actively reload all imported modules or use a reload function.

Update modules in your notebooks as they change in Python

  • Jupyter Notebook will not accept magic commands or libraries from separate modules.
  • Drop the py suffix in your import statement when importing.
  • The Python Library Reference hosts a library of standard modules.
  • Use dir to identify what is located in your module

Packages

Contain one or more modules. Packages are a special type of module that includes using dotted module names and are initiated through __init__.py.

Libraries

Libraries are essentially packages, but are typically referred to as a larger collection of packages. This is the top level of the hierarchy, serving as containers for modules/packages.

Managing Packages/Libraries

If you’re running into an error with your library, check on the version type that you are working with and then update as necessary. A quick query should reveal the latest version of a particular package available.

To view installed packages, run the following in your terminal or notebook:

conda list (Terminal)
pip list (Notebook)

To install a package, run:

# Terminal
$ conda install [package name]
# Notebook
pip install [package name]

There an option to search for outdated packages and view the version type v. most updated version available:

# Notebook
pip list --outdated

Once these have been identified, here’s always the option to update all packages in your terminal:

# Terminal
$ conda update --all

--

--