From 8beda89c2a76dd44eae79adbd407e1e1d6425c44 Mon Sep 17 00:00:00 2001 From: Alex Alvarez Date: Mon, 30 Jul 2018 04:10:07 -0400 Subject: [PATCH] BUGFIX: Extending PYTHONPATH to fix local imports (#863) * BUGFIX: Adding PYTHONPATH to fix local imports TODO: necessary for 2.7? How do we test that this is ok with respect to the mount point? * Revert "BUGFIX: Adding PYTHONPATH to fix local imports" This reverts commit 6da1fbae355b57d8f406466c8504d9730b9a4539. * fix: Extending PYTHONPATH to contain user files * fixup -- wrong usage of string.Join * Extending deps example/test to import foo from a local file * fixup * Deploy zip instead of deploying single python file * fixup path * fixup * Fix handler name in test setup * Changing command so it zips w/o folder --- examples/Makefile | 3 ++- examples/python/hellowithdeps.py | 8 +------- examples/python/hellowithdepshelper.py | 7 +++++++ pkg/langruntime/langruntime.go | 3 ++- pkg/utils/kubelessutil_test.go | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 examples/python/hellowithdepshelper.py diff --git a/examples/Makefile b/examples/Makefile index 212371611..c06f1f34a 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -15,7 +15,8 @@ get-python-update-verify: kubeless function call get-python |egrep hello.world.updated get-python-deps: - kubeless function deploy get-python-deps --runtime python2.7 --handler helloget.foo --from-file python/hellowithdeps.py --dependencies python/requirements.txt + cd python && zip hellowithdeps.zip hellowithdeps.py hellowithdepshelper.py && cd .. + kubeless function deploy get-python-deps --runtime python2.7 --handler hellowithdeps.foo --from-file python/hellowithdeps.zip --dependencies python/requirements.txt get-python-deps-verify: kubeless function call get-python-deps |egrep Google diff --git a/examples/python/hellowithdeps.py b/examples/python/hellowithdeps.py index b930bcdb5..353de7a27 100644 --- a/examples/python/hellowithdeps.py +++ b/examples/python/hellowithdeps.py @@ -1,7 +1 @@ -from bs4 import BeautifulSoup -import urllib2 - -def foo(event, context): - page = urllib2.urlopen("https://www.google.com/").read() - soup = BeautifulSoup(page, 'html.parser') - return soup.title.string +from hellowithdepshelper import foo \ No newline at end of file diff --git a/examples/python/hellowithdepshelper.py b/examples/python/hellowithdepshelper.py new file mode 100644 index 000000000..b930bcdb5 --- /dev/null +++ b/examples/python/hellowithdepshelper.py @@ -0,0 +1,7 @@ +from bs4 import BeautifulSoup +import urllib2 + +def foo(event, context): + page = urllib2.urlopen("https://www.google.com/").read() + soup = BeautifulSoup(page, 'html.parser') + return soup.title.string diff --git a/pkg/langruntime/langruntime.go b/pkg/langruntime/langruntime.go index e589f945a..3e9e013e6 100644 --- a/pkg/langruntime/langruntime.go +++ b/pkg/langruntime/langruntime.go @@ -291,9 +291,10 @@ func (l *Langruntimes) GetBuildContainer(runtime, depsChecksum string, env []v1. func (l *Langruntimes) UpdateDeployment(dpm *v1beta1.Deployment, depsPath, runtime string) { switch { case strings.Contains(runtime, "python"): + pythonPaths := []string{path.Join(depsPath, "lib/python"+l.getVersionFromRuntime(runtime)+"/site-packages"), depsPath} dpm.Spec.Template.Spec.Containers[0].Env = append(dpm.Spec.Template.Spec.Containers[0].Env, v1.EnvVar{ Name: "PYTHONPATH", - Value: path.Join(depsPath, "lib/python"+l.getVersionFromRuntime(runtime)+"/site-packages"), + Value: strings.Join(pythonPaths, ":"), }) case strings.Contains(runtime, "nodejs"): dpm.Spec.Template.Spec.Containers[0].Env = append(dpm.Spec.Template.Spec.Containers[0].Env, v1.EnvVar{ diff --git a/pkg/utils/kubelessutil_test.go b/pkg/utils/kubelessutil_test.go index 2212d012e..d1961aa33 100644 --- a/pkg/utils/kubelessutil_test.go +++ b/pkg/utils/kubelessutil_test.go @@ -538,7 +538,7 @@ func TestEnsureDeployment(t *testing.T) { }, { Name: "PYTHONPATH", - Value: "/kubeless/lib/python2.7/site-packages", + Value: "/kubeless/lib/python2.7/site-packages:/kubeless", }, }, VolumeMounts: []v1.VolumeMount{