Skip to content

Commit

Permalink
fixing cannot import name 'metadata' from 'importlib' (#65)
Browse files Browse the repository at this point in the history
* add testing
* importlib-metadata
* try imports
* 0.4.1

Co-authored-by: otaj <[email protected]>
  • Loading branch information
Borda and otaj authored Oct 31, 2022
1 parent c3bcba0 commit cdedd59
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-22.04, macos-12, windows-2022]
python-version: [3.8]
python-version: ["3.7", "3.10"]
requires: ['oldest', 'latest']

timeout-minutes: 35
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importlib-metadata>=4.0.0; python_version < '3.8'
8 changes: 4 additions & 4 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coverage>=5.0
coverage==6.5.0
codecov>=2.1
pytest>=6.0
pytest-cov
pytest-timeout
pytest==7.2.0
pytest-cov==4.0.0
pytest-timeout==2.1.0
25 changes: 18 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python

import glob
import os
from importlib.util import module_from_spec, spec_from_file_location

Expand All @@ -19,8 +19,21 @@ def _load_py_module(fname, pkg="lightning_utilities"):


about = _load_py_module("__about__.py")
with open(os.path.join(_PATH_REQUIRE, "cli.txt")) as fp:
requirements_cli = list(map(str, parse_requirements(fp.readline())))

# load basic requirements
with open(os.path.join(_PATH_REQUIRE, "base.txt")) as fp:
requirements = list(map(str, parse_requirements(fp.readline())))

# make extras as automated loading
requirements_extra = {}
for fpath in glob.glob(os.path.join(_PATH_REQUIRE, "*.txt")):
if os.path.basename(fpath) == "base.txt":
continue
name, _ = os.path.splitext(os.path.basename(fpath))
with open(fpath) as fp:
requirements_extra[name] = list(map(str, parse_requirements(fp.readline())))

# loading readme as description
with open(os.path.join(_PATH_ROOT, "README.md")) as fp:
readme = fp.read()

Expand All @@ -42,10 +55,8 @@ def _load_py_module(fname, pkg="lightning_utilities"):
keywords=["Utilities", "DevOps", "CI/CD"],
python_requires=">=3.7",
setup_requires=[],
install_requires=[],
extras_require={
"cli": requirements_cli,
},
install_requires=requirements,
extras_require=requirements_extra,
project_urls={
"Bug Tracker": "https://github.com/Lightning-AI/utilities/issues",
"Documentation": "https://dev-toolbox.rtfd.io/en/latest/", # TODO: Update domain
Expand Down
2 changes: 1 addition & 1 deletion src/lightning_utilities/__about__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

__version__ = "0.4.0"
__version__ = "0.4.1"
__author__ = "Lightning AI et al."
__author_email__ = "[email protected]"
__license__ = "Apache-2.0"
Expand Down
7 changes: 6 additions & 1 deletion src/lightning_utilities/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import importlib
import operator
from functools import lru_cache
from importlib import metadata
from importlib.util import find_spec
from typing import Callable

import pkg_resources
from packaging.requirements import Requirement
from packaging.version import Version

try:
from importlib import metadata
except ImportError:
# Python < 3.8
import importlib_metadata as metadata # type: ignore


@lru_cache()
def package_available(package_name: str) -> bool:
Expand Down
7 changes: 6 additions & 1 deletion tests/unittests/core/test_imports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import operator
import re
from importlib.metadata import PackageNotFoundError

import pytest

Expand All @@ -11,6 +10,12 @@
RequirementCache,
)

try:
from importlib.metadata import PackageNotFoundError
except ImportError:
# Python < 3.8
from importlib_metadata import PackageNotFoundError


def test_module_exists():
assert module_available("_pytest")
Expand Down

0 comments on commit cdedd59

Please sign in to comment.