Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pkg_resources, use importlib metadata instead #594

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions eta/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
import numbers
import os
from packaging.requirements import Requirement
import pkg_resources

try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
import py7zr
import pytz
import random
Expand Down Expand Up @@ -669,7 +673,7 @@ def ensure_package(
):
"""Ensures that the given package is installed.

This function uses `pkg_resources.get_distribution` to locate the package
This function uses `importlib.metadata.version` to locate the package
by its pip name and does not actually import the module.

Therefore, unlike `ensure_import()`, `requirement_str` should refer to the
Expand Down Expand Up @@ -721,9 +725,9 @@ def _get_package_version(requirement_str):
req = Requirement(requirement_str)

try:
version = pkg_resources.get_distribution(req.name).version
version = metadata.version(req.name)
error = None
except pkg_resources.DistributionNotFound as e:
except importlib.PackageNotFoundError as e:
version = None
error = e

Expand Down
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
voxel51.com
"""
import os
from pkg_resources import DistributionNotFound, get_distribution

try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
import re
from setuptools import setup, find_packages
from wheel.bdist_wheel import bdist_wheel
Expand Down Expand Up @@ -64,10 +68,10 @@ def choose_requirement(mains, secondary):
for main in mains:
try:
name = re.split(r"[!<>=]", main)[0]
get_distribution(name)
metadata.version(name)
chosen = main
break
except DistributionNotFound:
except metadata.PackageNotFoundError:
pass

return str(chosen)
Expand Down