Bug: draw.py hard-imports cv2/vsketch/rasterio at top-level — missing dep = opaque crash
Bug: draw.py hard-imports cv2/vsketch/rasterio at top-level — missing dep = opaque crash
Describe the bug
draw.py imports all heavy dependencies at module level (lines 31–42). If any single dependency is missing, import prettymaps fails completely with a bare ModuleNotFoundError that doesn't indicate which package is needed or whether it's optional.
To reproduce
pip install prettymaps --no-deps
python -c "import prettymaps"
# → ModuleNotFoundError: No module named 'cv2'
# (install cv2, retry)
# → ModuleNotFoundError: No module named 'vsketch'
# (install vsketch, retry)
# → ModuleNotFoundError: No module named 'rasterio'Expected behavior
Basic matplotlib rendering should work without requiring cv2, rasterio, vsketch, elevation, scikit-image, etc. Optional dependencies should be imported lazily inside the functions that need them.
Root cause
# draw.py lines 31-42 (all top-level, all unconditional):
import cv2 # opencv-python-headless — only needed for postprocessing
import geopandas as gp
import matplotlib
import numpy as np
import osmnx as ox
import pandas as pd
import shapely.affinity
import shapely.ops
import vsketch # only needed for vsketch mode
from matplotlib import pyplot as plt
from matplotlib.colors import LightSourcerasterio, rioxarray, scikit-image, elevation are also imported but only needed for elevation/terrain features.
Suggested fix
Lazy-import heavy/optional dependencies inside the functions that need them:
def plot(..., mode="matplotlib"):
if mode == "vsketch":
import vsketch
# ...This lets users render basic matplotlib maps without installing the full dependency tree. It also improves import time (currently 10+ seconds on first import due to matplotlib font cache scan when MPLCONFIGDIR is unset).
Environment
- prettymaps v1.4.2
- Python 3.10.18 (Homebrew)
- macOS 15, ARM64
Source: marceloprates/prettymaps