glenn-jocher commited on
Commit
7b1643b
1 Parent(s): 7316b78

Add `install=True` argument to `check_requirements` (#4512)

Browse files

* Add `install=True` argument to `check_requirements`

* Update general.py

Files changed (1) hide show
  1. utils/general.py +12 -8
utils/general.py CHANGED
@@ -172,7 +172,7 @@ def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=Fals
172
 
173
 
174
  @try_except
175
- def check_requirements(requirements='requirements.txt', exclude=()):
176
  # Check installed dependencies meet requirements (pass *.txt file or list of packages)
177
  prefix = colorstr('red', 'bold', 'requirements:')
178
  check_python() # check python version
@@ -188,13 +188,17 @@ def check_requirements(requirements='requirements.txt', exclude=()):
188
  try:
189
  pkg.require(r)
190
  except Exception as e: # DistributionNotFound or VersionConflict if requirements not met
191
- print(f"{prefix} {r} not found and is required by YOLOv5, attempting auto-update...")
192
- try:
193
- assert check_online(), f"'pip install {r}' skipped (offline)"
194
- print(check_output(f"pip install '{r}'", shell=True).decode())
195
- n += 1
196
- except Exception as e:
197
- print(f'{prefix} {e}')
 
 
 
 
198
 
199
  if n: # if packages updated
200
  source = file.resolve() if 'file' in locals() else requirements
 
172
 
173
 
174
  @try_except
175
+ def check_requirements(requirements='requirements.txt', exclude=(), install=True):
176
  # Check installed dependencies meet requirements (pass *.txt file or list of packages)
177
  prefix = colorstr('red', 'bold', 'requirements:')
178
  check_python() # check python version
 
188
  try:
189
  pkg.require(r)
190
  except Exception as e: # DistributionNotFound or VersionConflict if requirements not met
191
+ s = f"{prefix} {r} not found and is required by YOLOv5"
192
+ if install:
193
+ print(f"{s}, attempting auto-update...")
194
+ try:
195
+ assert check_online(), f"'pip install {r}' skipped (offline)"
196
+ print(check_output(f"pip install '{r}'", shell=True).decode())
197
+ n += 1
198
+ except Exception as e:
199
+ print(f'{prefix} {e}')
200
+ else:
201
+ print(f'{s}. Please install and rerun your command.')
202
 
203
  if n: # if packages updated
204
  source = file.resolve() if 'file' in locals() else requirements