glenn-jocher commited on
Commit
2b329b0
1 Parent(s): 1bf9365

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

Browse files

* 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

Files changed (1) hide show
  1. utils/general.py +15 -5
utils/general.py CHANGED
@@ -1,4 +1,4 @@
1
- # General utils
2
 
3
  import glob
4
  import logging
@@ -86,10 +86,20 @@ def check_git_status():
86
 
87
  def check_requirements(file='requirements.txt', exclude=()):
88
  # Check installed dependencies meet requirements
89
- import pkg_resources
90
- requirements = [f'{x.name}{x.specifier}' for x in pkg_resources.parse_requirements(Path(file).open())
91
- if x.name not in exclude]
92
- pkg_resources.require(requirements) # DistributionNotFound or VersionConflict exception if requirements not met
 
 
 
 
 
 
 
 
 
 
93
 
94
 
95
  def check_img_size(img_size, s=32):
 
1
+ # YOLOv5 general utils
2
 
3
  import glob
4
  import logging
 
86
 
87
  def check_requirements(file='requirements.txt', exclude=()):
88
  # Check installed dependencies meet requirements
89
+ import pkg_resources as pkg
90
+ prefix = colorstr('red', 'bold', 'requirements:')
91
+ file = Path(file)
92
+ if not file.exists():
93
+ print(f"{prefix} {file.resolve()} not found, check failed.")
94
+ return
95
+
96
+ requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(file.open()) if x.name not in exclude]
97
+ for r in requirements:
98
+ try:
99
+ pkg.require(r)
100
+ except Exception as e: # DistributionNotFound or VersionConflict if requirements not met
101
+ print(f"{prefix} {e.req} not found and is required by YOLOv5, attempting auto-install...")
102
+ print(subprocess.check_output(f"pip install '{e.req}'", shell=True).decode())
103
 
104
 
105
  def check_img_size(img_size, s=32):