Open
Description
Description:
I have a project structured with a central pkg
directory and a tools
directory containing Knative functions that depend on code within the pkg
directory. Specifically, I'm utilizing the Flask function template. In my func.py
file, I attempt to perform a relative import as follows:
import pathlib
import sys
from flask import Flask, request
# https://stackoverflow.com/questions/16981921/relative-imports-in-python-3
DIR = pathlib.Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(DIR))
print("DIR", DIR)
from pkg.core.func_creator import FunctionCreator
from pkg.domain.models import Runtime
Executing func.py
directly with python func.py
works without issues. However, running the function using func run
results in an import error:
Building function image
Still building
🙌 Function built: index.docker.io/sdf/create:latest
Running on host port 8080
[2025-03-23 19:02:07 +0000] [1] [INFO] Starting gunicorn 22.0.0
[2025-03-23 19:02:07 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2025-03-23 19:02:07 +0000] [1] [INFO] Using worker: sync
[2025-03-23 19:02:07 +0000] [22] [INFO] Booting worker with pid: 22
DIR /
[2025-03-23 19:02:07 +0000] [22] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
worker.init_process()
File "/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/layers/paketo-buildpacks_pip-install/packages/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/layers/paketo-buildpacks_cpython/cpython/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/workspace/func.py", line 11, in <module>
from pkg.core.func_creator import FunctionCreator
ModuleNotFoundError: No module named 'pkg'
[2025-03-23 19:02:07 +0000] [22] [INFO] Worker exiting (pid: 22)
[2025-03-23 19:02:07 +0000] [1] [ERROR] Worker (pid:22) exited with code 3
[2025-03-23 19:02:07 +0000] [1] [ERROR] Shutting down: Master
[2025-03-23 19:02:07 +0000] [1] [ERROR] Reason: Worker failed to boot.
Expected Behavior:
The function should execute successfully, resolving the relative imports without errors
Actual Behavior:
The function fails to run, producing an import error indicating that the pkg
module cannot be found
Metadata
Metadata
Assignees
Labels
No labels