Skip to content

Commit

Permalink
feat: use importlib instead of pkg_resources (#6)
Browse files Browse the repository at this point in the history
* fix: calls to deprecated conda api

* ci: test on python 3.8-3.12

* fix: capture package metadata using importlib

capture package names, versions and paths using importlib, not
pkg_resources which is deprecated.

* fix: support older versions of importlib

* ci: install line_profiler on all platforms
  • Loading branch information
alubbock authored Aug 16, 2024
1 parent 474f743 commit 4d1b2b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
20 changes: 2 additions & 18 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand All @@ -28,23 +28,7 @@ jobs:
- name: Install dependencies except line_profiler
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest pandas psutil
# if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install line_profiler (*nix)
if: matrix.os != 'windows-latest'
run: python -m pip install line_profiler
- name: Install line_profiler (Windows 3.6)
if: matrix.os == 'windows-latest' && matrix.python-version == '3.6'
run: python -m pip install line-profiler@https://alex.lolab.xyz/wheels/line_profiler-3.1.0-cp36-cp36m-win_amd64.whl
- name: Install line_profiler (Windows 3.7)
if: matrix.os == 'windows-latest' && matrix.python-version == '3.7'
run: python -m pip install line-profiler@https://alex.lolab.xyz/wheels/line_profiler-3.1.0-cp37-cp37m-win_amd64.whl
- name: Install line_profiler (Windows 3.8)
if: matrix.os == 'windows-latest' && matrix.python-version == '3.8'
run: python -m pip install line-profiler@https://alex.lolab.xyz/wheels/line_profiler-3.1.0-cp38-cp38-win_amd64.whl
- name: Install line_profiler (Windows 3.9)
if: matrix.os == 'windows-latest' && matrix.python-version == '3.9'
run: python -m pip install line-profiler@https://alex.lolab.xyz/wheels/line_profiler-3.1.0-cp39-cp39-win_amd64.whl
python -m pip install flake8 pytest pandas psutil line_profiler
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
24 changes: 11 additions & 13 deletions microbench/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
from collections.abc import Iterable
import os
import importlib
import inspect
import types
import pickle
Expand All @@ -15,10 +16,6 @@
import threading
import signal
import warnings
try:
import pkg_resources
except ImportError:
pkg_resources = None
try:
import line_profiler
except ImportError:
Expand Down Expand Up @@ -301,23 +298,24 @@ def capture_conda_packages(self, bm_data):


class MBInstalledPackages(object):
""" Capture installed Python packages using pkg_resources """
""" Capture installed Python packages using importlib """
capture_paths = False

def capture_packages(self, bm_data):
if not pkg_resources:
raise ImportError(
'pkg_resources is required to capture package names, which is '
'provided with the "setuptools" package')

bm_data['package_versions'] = {}
if self.capture_paths:
bm_data['package_paths'] = {}

for pkg in pkg_resources.working_set:
bm_data['package_versions'][pkg.project_name] = pkg.version
for pkg in importlib.metadata.distributions():
try:
pkg_name = pkg.name
except AttributeError:
# Python <3.9
pkg_name = pkg.metadata['Name']
bm_data['package_versions'][pkg_name] = pkg.version
if self.capture_paths:
bm_data['package_paths'][pkg.project_name] = pkg.location
bm_data['package_paths'][pkg_name] = os.path.dirname(
pkg.locate_file(pkg.files[0]))


class MBLineProfiler(object):
Expand Down
2 changes: 1 addition & 1 deletion microbench/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def noop():
assert results['package_versions'][0]['pandas'] == pandas.__version__


def test_capture_packages_pkg_resources():
def test_capture_packages_importlib():
class PkgBench(MicroBench, MBInstalledPackages):
capture_paths = True

Expand Down

0 comments on commit 4d1b2b9

Please sign in to comment.