Skip to content

Commit 5eb1101

Browse files
Completes UNB-2896 - Add consistency check between module versions used in the Jupyter notebook and the ones specified in the requirements file
1 parent f41d0c7 commit 5eb1101

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
* Warnings if the dependencies from the `requirement_txt_file` and current environment are inconsistent.
13+
1014
### Changed
1115

1216
* Migrated package name from [openlayer](https://pypi.org/project/openlayer/) to [openlayer](https://pypi.org/project/openlayer/) due to a company name change.

openlayer/__init__.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import tempfile
66
import traceback
77
import uuid
8+
import warnings
89
from typing import Callable, List, Optional
910

1011
import marshmallow as ma
1112
import pandas as pd
13+
import pkg_resources
1214
from bentoml.saved_bundle import bundler
1315
from bentoml.utils import tempdir
1416

@@ -536,6 +538,7 @@ def add_model(
536538
raise exceptions.OpenlayerResourceError(
537539
f"File `{requirements_txt_file}` does not exist. \n"
538540
) from None
541+
self._check_dependencies(requirements_txt_file)
539542

540543
# Setup script
541544
if setup_script and not os.path.isfile(os.path.expanduser(setup_script)):
@@ -1208,7 +1211,7 @@ def add_dataframe(
12081211

12091212
@staticmethod
12101213
def _format_error_message(err) -> str:
1211-
"""Formats the error messaeges from Marshmallow"""
1214+
"""Formats the error messages from Marshmallow"""
12121215
error_msg = ""
12131216
for input, msg in err.messages.items():
12141217
if input == "_schema":
@@ -1221,3 +1224,53 @@ def _format_error_message(err) -> str:
12211224
temp_msg = list(msg.values())[0][0].lower()
12221225
error_msg += f"- `{input}` contains items that are {temp_msg} \n"
12231226
return error_msg
1227+
1228+
@staticmethod
1229+
def _check_dependencies(requirements_txt_file: str):
1230+
"""Checks the modules specified in the `requirements_txt_file` against
1231+
the ones installed in the current enviromnent
1232+
"""
1233+
# Read requirements file
1234+
with open(requirements_txt_file) as f:
1235+
lines = f.readlines()
1236+
1237+
# Parse the requirements file
1238+
dependencies = pkg_resources.parse_requirements(lines)
1239+
1240+
for requirement in dependencies:
1241+
requirement = str(requirement)
1242+
try:
1243+
pkg_resources.require(requirement)
1244+
except pkg_resources.VersionConflict as err:
1245+
try:
1246+
warnings.warn(
1247+
"There is a version discrepancy between the current "
1248+
f"environment and the dependency `{requirement}`. \n"
1249+
f"`requirements_txt_file` specifies `{err.req}`, but the current "
1250+
f"environment contains `{err.dist}` installed. \n"
1251+
"There might be unexpected results once the model is in the platform. "
1252+
"Use at your own discretion.",
1253+
category=Warning,
1254+
)
1255+
return None
1256+
except AttributeError:
1257+
warnings.warn(
1258+
"There is a version discrepancy between the current "
1259+
f"environment and the dependency `{requirement}`. \n"
1260+
f"`requirements_txt_file` specifies `{requirement}`, but the current "
1261+
f"environment contains an incompatible version installed. \n"
1262+
"There might be unexpected results once the model is in the platform. "
1263+
"Use at your own discretion.",
1264+
category=Warning,
1265+
)
1266+
return None
1267+
except pkg_resources.DistributionNotFound as err:
1268+
warnings.warn(
1269+
f"The dependency `{requirement}` specified in the `requirements_txt_file` "
1270+
"is not installed in the current environment. \n"
1271+
"There might be unexpected results once the model is in the platform. "
1272+
"Use at your own discretion.",
1273+
category=Warning,
1274+
)
1275+
return None
1276+
return None

0 commit comments

Comments
 (0)