Dependency Management | Python

Session Overview

  • Modules, Packages, Libraries, Oh My!
  • Install Libraries/Packages
  • Importing Libraries/Packages
  • Managing Libraries/Packages
  • Environment Management
  • Python Library Ecosystem

Python Modules

Module:

  • Related code saved in a single .py file.
  • Can be written and shared internally or externally
  • Can be imported:
# hello.py
def hello_function(name: str)-> None:
    print(f"Hello {name}!")
# program.py

from hello import hello_function

hello_function("ESDS")

Packages & Libraries

Packages:

  • Collection of modules

Libraries:

  • Collection of packages.

In practice, Packages and Libraries can be used interchangeably.

Installing Packages

Sources:


Focus today on pip

Let’s Play!

Installing Packages

Command Line

pip install polars


Console

!pip install polars

Polars Package

Session Overview

  • Install Libraries/Packages
  • Importing Libraries/Packages
  • Managing Libraries/Packages
  • Environment Management
  • Python Library Ecosystem

Importing Libraries

What is going on here?

import pandas as pd
from os import environ
import polars

Importing Libraries

Namespaces!

  • Scope
  • What is available where & when
  • Do not clutter your Global Namespace

Namespace Example

global_variable = 34

def calculate_difference(input_number: int) -> int:
  
  local_variable = 36
  
  difference_value = local_variable - global_variable
  
  return(difference_value)

local_variable

calculate_difference(input_number = global_variable)

Importing Libraries

What is going on here?

import pandas as pd
from os import environ
import polars


pd.DataFrame({'A': [1, 2, 3]})

environ['ENVIRONMENT_VARIABLE'] = 'ESDS'

os.listdir('/')

pl.Series() #???

Importing Libraries

How do we import our own functions from other files?

Session Overview

  • Install Libraries/Packages
  • Importing Libraries/Packages
  • Managing Libraries/Packages
  • Environment Management
  • Python Library Ecosystem

Managing Libraries

What happens if we need different versions of packages for different projects?

Environment Management

Python Environment Managers:

Environment Management

In the Terminal:

python -m venv venv



source venv/bin/activate


Test importing pandas… What happens?



deactivate


What else do we notice about the prompt?

Managing Environments

How do we add our new environment to our REPL?


source venv/bin/activate

pip install jupyter

ipython kernel install --name "local-venv" --user


Check for kernel options in the IDE…

Session Overview

  • Install Libraries/Packages
  • Importing Libraries/Packages
  • Managing Libraries/Packages
  • Environment Management
  • Python Library Ecosystem

Python Library Ecosystem

Important Data Science Packages: