Enhanced check_requirements() with auto-install (#2575)

* 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.

* cleanup

* cleanup 2

* Check requirements.txt file exists

* cleanup 3
This commit is contained in:
Glenn Jocher 2021-03-24 01:05:59 +01:00 committed by GitHub
parent 1bf9365280
commit 2b329b0945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# General utils # YOLOv5 general utils
import glob import glob
import logging import logging
@ -86,10 +86,20 @@ def check_git_status():
def check_requirements(file='requirements.txt', exclude=()): def check_requirements(file='requirements.txt', exclude=()):
# Check installed dependencies meet requirements # Check installed dependencies meet requirements
import pkg_resources import pkg_resources as pkg
requirements = [f'{x.name}{x.specifier}' for x in pkg_resources.parse_requirements(Path(file).open()) prefix = colorstr('red', 'bold', 'requirements:')
if x.name not in exclude] file = Path(file)
pkg_resources.require(requirements) # DistributionNotFound or VersionConflict exception if requirements not met 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.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())
def check_img_size(img_size, s=32): def check_img_size(img_size, s=32):