[Python] Replace pkg_resources

The import time for pkg_resources is significant. As of Python 3.8, the
two commands we use can be replace by 'importlib.metadata.version' and the
lighter-weight 'packaging.version.parse'.
This commit is contained in:
Ray Speth
2023-01-22 12:33:25 -05:00
committed by Ray Speth
parent 3b6be9b4de
commit 6ddfc2239b
7 changed files with 27 additions and 18 deletions

View File

@@ -78,7 +78,10 @@ import subprocess
import re
import json
import textwrap
from pkg_resources import parse_version
try:
from packaging.version import parse as parse_version
except ImportError:
from pkg_resources import parse_version
import SCons
# ensure that Python and SCons versions are sufficient for the build process

View File

@@ -6,13 +6,13 @@ import os
import warnings
from cpython.ref cimport PyObject
import numbers
import pkg_resources
import importlib.metadata
import numpy as np
# avoid explicit dependence of cantera on scipy
try:
pkg_resources.get_distribution('scipy')
except pkg_resources.DistributionNotFound:
importlib.metadata.version('scipy')
except importlib.metadata.PackageNotFoundError:
_scipy_sparse = ImportError('Method requires a working scipy installation.')
else:
from scipy import sparse as _scipy_sparse
@@ -103,8 +103,8 @@ def hdf_support():
"""
out = []
try:
pkg_resources.get_distribution("h5py")
except pkg_resources.DistributionNotFound:
importlib.metadata.version("h5py")
except importlib.metadata.PackageNotFoundError:
pass
else:
out.append("h5py")

View File

@@ -6,24 +6,22 @@ from ._cantera import *
import numpy as np
from collections import OrderedDict
import csv as _csv
import importlib.metadata
def _import_h5py():
# avoid explicit dependence of cantera on h5py
import pkg_resources # local import to reduce overall import time
try:
pkg_resources.get_distribution('h5py')
except pkg_resources.DistributionNotFound:
importlib.metadata.version('h5py')
except importlib.metadata.PackageNotFoundError:
raise ImportError('Method requires a working h5py installation.')
else:
import h5py
return h5py
# avoid explicit dependence of cantera on pandas
import pkg_resources
try:
pkg_resources.get_distribution('pandas')
except pkg_resources.DistributionNotFound:
importlib.metadata.version('pandas')
except importlib.metadata.PackageNotFoundError:
_pandas = ImportError('Method requires a working pandas installation.')
else:
import pandas as _pandas

View File

@@ -42,6 +42,7 @@ include_package_data = True
install_requires =
numpy >= 1.12.0
ruamel.yaml >= 0.15.34
packaging
python_requires = >=@py_min_ver_str@
packages =
cantera

View File

@@ -42,6 +42,7 @@ include_package_data = True
install_requires =
numpy >= 1.12.0
ruamel.yaml >= 0.15.34
packaging
python_requires = >=@py_min_ver_str@
packages =
cantera

View File

@@ -11,7 +11,10 @@ import time
import shutil
import enum
from pathlib import Path
from pkg_resources import parse_version
try:
from packaging.version import parse as parse_version
except ImportError:
from pkg_resources import parse_version
import logging
from typing import TYPE_CHECKING
from collections.abc import Mapping as MappingABC
@@ -1352,7 +1355,10 @@ def get_pip_install_location(
root = quoted(root) if root is not None else None
install_script = textwrap.dedent(f"""
from pip import __version__ as pip_version
from pkg_resources import parse_version
try:
from packaging.version import parse as parse_version
except ImportError:
from pkg_resources import parse_version
import pip
import json
pip_version = parse_version(pip_version)

View File

@@ -1,7 +1,7 @@
import numpy as np
import re
import itertools
import pkg_resources
import importlib.metadata
import pytest
import cantera as ct
@@ -10,8 +10,8 @@ from .utilities import allow_deprecated
# avoid explicit dependence of cantera on scipy
try:
pkg_resources.get_distribution('scipy')
except pkg_resources.DistributionNotFound:
importlib.metadata.version('scipy')
except importlib.metadata.PackageNotFoundError:
_scipy_sparse = ImportError('Method requires a working scipy installation.')
else:
from scipy import sparse as _scipy_sparse