Download scripts/install_dependencies.py from Monyzik/framework-coursework-text: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/datasets/Monyzik/framework-coursework-text/resolve/main/scripts/install_dependencies.py
- Command line
-
hf download hf://datasets/Monyzik/framework-coursework-text/scripts/install_dependencies.py
-
curl -L -o install_dependencies.py https://huggingface.co/datasets/Monyzik/framework-coursework-text/resolve/main/scripts/install_dependencies.py
1.88 kB
| from importlib import invalidate_caches, metadata | |
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| def main() -> None: | |
| if sys.version_info < (3, 11): | |
| raise RuntimeError('Для проекта нужен Python 3.11 или новее.') | |
| import tomllib | |
| root = Path(__file__).resolve().parents[1] | |
| config = tomllib.loads((root / 'pyproject.toml').read_text(encoding='utf-8')) | |
| try: | |
| metadata.version('packaging') | |
| except metadata.PackageNotFoundError: | |
| subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'packaging>=23']) | |
| invalidate_caches() | |
| from packaging.requirements import Requirement | |
| to_install = [] | |
| to_update = [] | |
| for specification in config['project']['dependencies']: | |
| requirement = Requirement(specification) | |
| if requirement.marker and not requirement.marker.evaluate(): | |
| continue | |
| try: | |
| installed = metadata.version(requirement.name) | |
| except metadata.PackageNotFoundError: | |
| to_install.append(specification) | |
| else: | |
| if not requirement.specifier.contains(installed, prereleases=True): | |
| to_install.append(specification) | |
| to_update.append(requirement.name) | |
| if not to_install: | |
| print('Все зависимости уже установлены в подходящих версиях.') | |
| return | |
| print('Устанавливаем:', ', '.join(to_install), flush=True) | |
| subprocess.check_call([sys.executable, '-m', 'pip', 'install', *to_install]) | |
| print('Зависимости установлены.') | |
| if to_update: | |
| print('Обновлены:', ', '.join(to_update)) | |
| print('Если эти библиотеки уже были импортированы, перезапустите ядро.') | |
| if __name__ == '__main__': | |
| main() | |