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

fixing cannot import name 'metadata' from 'importlib' #65

Merged
merged 14 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
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.0
codecov>=2.1
pytest>=6.0
pytest-cov
pytest-timeout
pytest>=7.0.0
pytest-cov>=3.0.0
pytest-timeout>=2.0.0
otaj marked this conversation as resolved.
Show resolved Hide resolved
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__ = "pytorch@lightning.ai"
__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