Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Python 3.6 runtime raising relative import error for multi-file python kubeless function through zip file #590

Closed
swaroop-iquanti opened this issue Feb 13, 2018 · 2 comments

Comments

@swaroop-iquanti
Copy link

Is this a BUG REPORT or FEATURE REQUEST?:
BUG REPORT

What happened:
Tried to deploy multi file python serverless function on python 3.6 runtime. Organized my code into a handler file and a utils file in the parent directory. Deployed the kubeless function onto minikube kubernetes cluster on my workstation. Which ever configuration I tried with the packaging of the serverless function folder, I keep bumping into relative imports failure. Please suggest a way to organize my codebase

What you expected to happen:
Able to organize my functions codebase into multiple files, with some common utilities in separate file and different functions in different handler files.

How to reproduce it (as minimally and precisely as possible):

  1. kubeless function deploy hello-kubeless-python --runtime python3.6 --from-file hello-kubeless.zip --handler handler.run --trigger-http
  2. kubectl logs hello-kubeless-python-<pod> gave following traceback
    Traceback (most recent call last): File "/kubeless.py", line 11, in <module> '/kubeless/%s.py' % os.getenv('MOD_NAME')) File "/opt/bitnami/python/lib/python3.6/imp.py", line 172, in load_source module = _load(spec) File "<frozen importlib._bootstrap>", line 684, in _load File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/kubeless/handler.py", line 1, in <module> from .utils import random_utility ImportError: attempted relative import with no known parent package

Please find attached the zip file with kubeless function.
hello-kubeless.zip

Anything else we need to know?:
Python 3.6.4

Environment:

  • Kubernetes version (use kubectl version):
    Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T10:09:24Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"", Minor:"", GitVersion:"v1.9.0", GitCommit:"925c127ec6b946659ad0fd596fa959be43f0cc05", GitTreeState:"clean", BuildDate:"2018-01-26T19:04:38Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"linux/amd64"}
    With minikube version: v0.25.0
  • Kubeless version (use kubeless version):
    Kubeless version: v0.3.4 (d16de3f6)
  • Cloud provider or physical cluster:
    Local workstation
@andresmgot
Copy link
Contributor

andresmgot commented Feb 13, 2018

I am able to reproduce the issue. You can obtain the same error when executing python locally:

▶ python handler.py
Traceback (most recent call last):
  File "handler.py", line 1, in <module>
    from .utils import random_utility
ValueError: Attempted relative import in non-package

We should revisit the way in which we call the Python wrapper but in the meantime you can try adding the function directory to the sys.path module:

import sys
from os import path
sys.path.append( '/kubeless' )
from utils import random_utility

def run(request):
    print('Hello to handler')
    print(random_utility('Hello Ping'))
    return "Finally. Got to work with Kubeless"

@swaroop-iquanti
Copy link
Author

Thanks @andresmgot
This solution works

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants