Skip to content
Permalink
Browse files

Fix BentoService bundle pip package dependencies (#396)

* fix install_requires parsing

* add related tests

* hide pytest warning for local dev
  • Loading branch information
parano committed Nov 25, 2019
1 parent 791312c commit 94d9271a5eb5fc99ee7f00832b9250e36d7da85a
Showing with 28 additions and 21 deletions.
  1. +8 −16 bentoml/bundler/templates.py
  2. +2 −0 pytest.ini
  3. +15 −5 tests/test_pip_install_saved_bundle.py
  4. +3 −0 tox.ini
@@ -18,26 +18,18 @@


BENTO_SERVICE_BUNDLE_SETUP_PY_TEMPLATE = """\
import os
import pip
import logging
import pkg_resources
import setuptools
def _parse_requirements(file_path):
pip_ver = pkg_resources.get_distribution('pip').version
pip_version = list(map(int, pip_ver.split('.')[:2]))
if pip_version >= [6, 0]:
raw = pip.req.parse_requirements(file_path,
session=pip.download.PipSession())
else:
raw = pip.req.parse_requirements(file_path)
return [str(i.req) for i in raw]
try: # for pip >= 10
from pip._internal.req import parse_requirements
from pip._internal.download import PipSession
except ImportError: # for pip <= 9.0.3
from pip.req import parse_requirements
from pip.download import PipSession
try:
install_reqs = _parse_requirements("requirements.txt")
raw = parse_requirements('requirements.txt', session=PipSession())
install_reqs = [str(i.req) for i in raw]
except Exception:
logging.warning('Fail load requirements file, so using default ones.')
install_reqs = []
setuptools.setup(
@@ -0,0 +1,2 @@
[pytest]
addopts = -p no:warnings
@@ -4,19 +4,28 @@

import pandas as pd

from bentoml.configuration import get_bentoml_deploy_version


def test_pip_install_saved_bentoservice_bundle(bento_bundle_path, tmpdir):
import subprocess

install_path = str(tmpdir.mkdir("pip_local"))
bentoml_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

output = subprocess.check_output(
stdout = subprocess.check_output(
["pip", "install", "-U", "--target={}".format(install_path), bento_bundle_path]
).decode()
assert "Successfully installed TestBentoService" in output
).decode('utf-8')

assert "Processing {}".format(bento_bundle_path) in stdout
assert "Collecting bentoml=={}".format(get_bentoml_deploy_version()) in stdout
assert "Successfully built TestBentoService" in stdout

# ensure BentoML is installed as dependency
assert os.path.isfile(os.path.join(install_path, "bin/bentoml"))
assert os.path.isdir(os.path.join(install_path, "bentoml"))

sys.path.append(install_path)
sys.path.insert(0, install_path)
TestBentoService = __import__("TestBentoService")
sys.path.remove(install_path)

@@ -25,7 +34,8 @@ def test_pip_install_saved_bentoservice_bundle(bento_bundle_path, tmpdir):
assert res == 1

# pip install should place cli entry script under target/bin directory
cli_bin_path = os.path.join(install_path, "bin/TestBentoService")
cli_bin_path = os.path.join(install_path, "bin", "TestBentoService")
assert os.path.isfile(cli_bin_path)

# add install_path and local bentoml module to PYTHONPATH to make them
# available in subprocess call
@@ -10,3 +10,6 @@ deps =
imageio
!py27: fastai
commands = pytest

[pytest]
addopts = -p no:warnings

0 comments on commit 94d9271

Please sign in to comment.
You can’t perform that action at this time.