vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.31k stars 793 forks source link

pkg_resources is deprecated #3132

Closed binste closed 1 year ago

binste commented 1 year ago

It's used in import_pyarrow_interchange in data.py. Currently generates this deprecation warning in our CI

../../../../Application Support/hatch/env/virtual/altair/Ac6cCVwn/altair/lib/python3.10/site-packages/pkg_resources/__init__.py:121
  /Users/stefanbinder/Library/Application Support/hatch/env/virtual/altair/Ac6cCVwn/altair/lib/python3.10/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
    warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)

We can replace pkg_resources with the packaging package and which I introduce in #3118 anyway and importlib.metadata. This is also the recommended way to do it in https://setuptools.pypa.io/en/latest/pkg_resources.html. I can look at this.

sacundim commented 1 year ago

Nice, I actually just encountered this in the wild and started writing a ticket over it. For reference, I'll just paste the ticket I was going to file.


I was playing around with how to minimize the size of Docker images by uninstalling some of the Python tools that shouldn't be needed at runtime:

RUN pip uninstall --yes pip setuptools wheel

...and it works fine with (a) Vega-Altair 5.0.1 + Pandas 1.5.3, (b) my experimental Vega-Altair 5.1.0 + Pandas 2.0 w/PyArrow backend branch; but not in (c) my experimental Altair 5.1.0 + Polars branch (fuller stack trace at bottom):

  File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/utils/data.py", line 352, in import_pyarrow_interchange
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

Searching for more info on this module I find this page with a deprecation note:

Attention

Use of pkg_resources is deprecated in favor of importlib.resources, importlib.metadata and their backports (importlib_resources, importlib_metadata). Some useful APIs are also provided by packaging (e.g. requirements and version parsing). Users should refrain from new usage of pkg_resources and should work to port to importlib-based solutions.

Stack trace ``` File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/vegalite/v5/api.py", line 1066, in save result = save(**kwds) ^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/utils/save.py", line 189, in save perform_save() File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/utils/save.py", line 127, in perform_save spec = chart.to_dict(context={"pre_transform": False}) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/vegalite/v5/api.py", line 892, in to_dict copy.data = _prepare_data(original_data, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/vegalite/v5/api.py", line 114, in _prepare_data data = _pipe(data, data_transformers.get()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/toolz/functoolz.py", line 628, in pipe data = func(data) ^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/toolz/functoolz.py", line 304, in __call__ return self._partial(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/vegalite/data.py", line 23, in default_data_transformer return curried.pipe(data, limit_rows(max_rows=max_rows), to_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/toolz/functoolz.py", line 628, in pipe data = func(data) ^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/toolz/functoolz.py", line 304, in __call__ return self._partial(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/utils/data.py", line 109, in limit_rows pi = import_pyarrow_interchange() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/covid-19-puerto-rico/venv/lib/python3.11/site-packages/altair/utils/data.py", line 352, in import_pyarrow_interchange import pkg_resources ModuleNotFoundError: No module named 'pkg_resources' ```
binste commented 1 year ago

Good point, thanks for the write up! We didn't have pkg_resourced as an explicit dependency of Altair which was an unknown issue apart from the deprecation. Bug fix will be part of the next Altair release :)