Download testbed/open-mmlab__mmengine/mmengine/utils/package_utils.py from hc99/FEA-Bench: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/datasets/hc99/FEA-Bench/resolve/main/testbed/open-mmlab__mmengine/mmengine/utils/package_utils.py
- Command line
-
hf download hf://datasets/hc99/FEA-Bench/testbed/open-mmlab__mmengine/mmengine/utils/package_utils.py
-
curl -L -o package_utils.py https://huggingface.co/datasets/hc99/FEA-Bench/resolve/main/testbed/open-mmlab__mmengine/mmengine/utils/package_utils.py
2.03 kB
| # Copyright (c) OpenMMLab. All rights reserved. | |
| import importlib | |
| import os.path as osp | |
| import subprocess | |
| import pkg_resources | |
| from pkg_resources import get_distribution | |
| def is_installed(package: str) -> bool: | |
| """Check package whether installed. | |
| Args: | |
| package (str): Name of package to be checked. | |
| """ | |
| # refresh the pkg_resources | |
| # more datails at https://github.com/pypa/setuptools/issues/373 | |
| importlib.reload(pkg_resources) | |
| try: | |
| get_distribution(package) | |
| return True | |
| except pkg_resources.DistributionNotFound: | |
| return False | |
| def get_installed_path(package: str) -> str: | |
| """Get installed path of package. | |
| Args: | |
| package (str): Name of package. | |
| Example: | |
| >>> get_installed_path('mmcls') | |
| >>> '.../lib/python3.7/site-packages/mmcls' | |
| """ | |
| # if the package name is not the same as module name, module name should be | |
| # inferred. For example, mmcv-full is the package name, but mmcv is module | |
| # name. If we want to get the installed path of mmcv-full, we should concat | |
| # the pkg.location and module name | |
| pkg = get_distribution(package) | |
| possible_path = osp.join(pkg.location, package) | |
| if osp.exists(possible_path): | |
| return possible_path | |
| else: | |
| return osp.join(pkg.location, package2module(package)) | |
| def package2module(package: str): | |
| """Infer module name from package. | |
| Args: | |
| package (str): Package to infer module name. | |
| """ | |
| pkg = get_distribution(package) | |
| if pkg.has_metadata('top_level.txt'): | |
| module_name = pkg.get_metadata('top_level.txt').split('\n')[0] | |
| return module_name | |
| else: | |
| raise ValueError(f'can not infer the module name of {package}') | |
| def call_command(cmd: list) -> None: | |
| try: | |
| subprocess.check_call(cmd) | |
| except Exception as e: | |
| raise e # type: ignore | |
| def install_package(package: str): | |
| if not is_installed(package): | |
| call_command(['python', '-m', 'pip', 'install', package]) | |