From 6207da275157d6099ec4dca1bbc3927ac5148b9d Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 24 Mar 2021 00:37:12 +0100 Subject: [PATCH 1/5] Update check_requirements() with auto-install This PR builds on an idea I had to automatically install missing dependencies rather than simply report an error message. YOLOv5 should now 1) display all dependency issues and not simply display the first missing dependency, and 2) attempt to install/update each missing/VersionConflict package. --- utils/general.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/utils/general.py b/utils/general.py index 621df64c6cf1..adbd4d04273d 100755 --- a/utils/general.py +++ b/utils/general.py @@ -1,4 +1,4 @@ -# General utils +# YOLOv5 general utils import glob import logging @@ -87,9 +87,15 @@ def check_git_status(): def check_requirements(file='requirements.txt', exclude=()): # Check installed dependencies meet requirements import pkg_resources + prefix = colorstr('red', 'bold', 'requirements:') # red bold requirements = [f'{x.name}{x.specifier}' for x in pkg_resources.parse_requirements(Path(file).open()) if x.name not in exclude] - pkg_resources.require(requirements) # DistributionNotFound or VersionConflict exception if requirements not met + for r in requirements: + try: + pkg_resources.require(r) # DistributionNotFound or VersionConflict exception if requirements not met + except Exception as e: + print(f"{prefix} {e.req} not found and is required by YOLOv5, attempting auto-install...") + print(subprocess.check_output(f"pip install '{e.req}'", shell=True).decode()) def check_img_size(img_size, s=32): From ad272ccd93ccc3955d8413761cd4cffda0a5c7ce Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 24 Mar 2021 00:43:38 +0100 Subject: [PATCH 2/5] cleanup --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index adbd4d04273d..5755c0f495fb 100755 --- a/utils/general.py +++ b/utils/general.py @@ -87,13 +87,13 @@ def check_git_status(): def check_requirements(file='requirements.txt', exclude=()): # Check installed dependencies meet requirements import pkg_resources - prefix = colorstr('red', 'bold', 'requirements:') # red bold requirements = [f'{x.name}{x.specifier}' for x in pkg_resources.parse_requirements(Path(file).open()) if x.name not in exclude] for r in requirements: try: pkg_resources.require(r) # DistributionNotFound or VersionConflict exception if requirements not met except Exception as e: + prefix = colorstr('red', 'bold', 'requirements:') # red bold print(f"{prefix} {e.req} not found and is required by YOLOv5, attempting auto-install...") print(subprocess.check_output(f"pip install '{e.req}'", shell=True).decode()) From 4a6191281e976d30f52fa2389925ea72313af1d1 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 24 Mar 2021 00:44:13 +0100 Subject: [PATCH 3/5] cleanup 2 --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 5755c0f495fb..f4c5f2bc3deb 100755 --- a/utils/general.py +++ b/utils/general.py @@ -93,7 +93,7 @@ def check_requirements(file='requirements.txt', exclude=()): try: pkg_resources.require(r) # DistributionNotFound or VersionConflict exception if requirements not met except Exception as e: - prefix = colorstr('red', 'bold', 'requirements:') # red bold + prefix = colorstr('red', 'bold', 'requirements:') print(f"{prefix} {e.req} not found and is required by YOLOv5, attempting auto-install...") print(subprocess.check_output(f"pip install '{e.req}'", shell=True).decode()) From 09982f34224a90c3c12929e36dc1274a1db32227 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 24 Mar 2021 00:55:22 +0100 Subject: [PATCH 4/5] Check requirements.txt file exists --- utils/general.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/utils/general.py b/utils/general.py index f4c5f2bc3deb..45085464ef26 100755 --- a/utils/general.py +++ b/utils/general.py @@ -86,14 +86,18 @@ def check_git_status(): def check_requirements(file='requirements.txt', exclude=()): # Check installed dependencies meet requirements - import pkg_resources - requirements = [f'{x.name}{x.specifier}' for x in pkg_resources.parse_requirements(Path(file).open()) - if x.name not in exclude] + import pkg_resources as pkg + prefix = colorstr('red', 'bold', 'requirements:') # red bold + file = Path(file) + if not file.exists(): + print(f"{prefix} {file.resolve()} not found, check failed.") + return + + requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(file.open()) if x.name not in exclude] for r in requirements: try: - pkg_resources.require(r) # DistributionNotFound or VersionConflict exception if requirements not met - except Exception as e: - prefix = colorstr('red', 'bold', 'requirements:') + pkg.require(r) + except Exception as e: # DistributionNotFound or VersionConflict if requirements not met print(f"{prefix} {e.req} not found and is required by YOLOv5, attempting auto-install...") print(subprocess.check_output(f"pip install '{e.req}'", shell=True).decode()) From 6e777ec78cfd2a8bb7ba1e8bd0ac967a43351a35 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 24 Mar 2021 00:56:21 +0100 Subject: [PATCH 5/5] cleanup 3 --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 45085464ef26..ef89ea3a0f03 100755 --- a/utils/general.py +++ b/utils/general.py @@ -87,7 +87,7 @@ def check_git_status(): def check_requirements(file='requirements.txt', exclude=()): # Check installed dependencies meet requirements import pkg_resources as pkg - prefix = colorstr('red', 'bold', 'requirements:') # red bold + prefix = colorstr('red', 'bold', 'requirements:') file = Path(file) if not file.exists(): print(f"{prefix} {file.resolve()} not found, check failed.")