Dependency Management | Python

Session Overview

  • Modules, Packages, Libraries, Oh My!
  • Managing Libraries/Packages
  • Environment Management
  • Install Libraries/Packages
  • Importing Libraries/Packages
  • 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, from PyPi with a twist.

Let’s Play!

Managing Libraries

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

Environment Management

Python Environment Managers:

Environment Management

In the Terminal:

cd ~
mkdir dependency_management
cd dependency_management
uv init
uv add pandas


Test importing pandas… What happens?


source .venv/bin/activate

Now test importing pandas… What happens?


Notice the prompt has changed!



deactivate

Managing Environments

How do we add our new environment to our REPL?


source .venv/bin/activate

uv add ipykernel jupyter

python -m ipykernel install --user --name=coder --display-name="Python (UV Environment)"


Check for kernel options on the command line:

jupyter kernelspec list

# To remove a kernel:
# jupyter kernelspec remove <kernel-name>
# It will ask you to confirm the deletion.

Check for kernel options in the IDE…

Installing Packages

Command Line

uv add polars pandas

Polars Package Pandas Package


Also:

uv sync

Session Overview

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

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

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

Python Library Ecosystem

Important Data Science Packages: