<@U06RKAMK0UC> Running a registered workflow resul...
# ask-ai
b
@ancient-wolf-19325 Running a registered workflow results in
ModuleNotFoundError: No module named 'src'
How do I debug the issue? Full stack trace below
Copy code
Traceback (most recent call last):

      File "/opt/conda/lib/python3.11/site-packages/flytekit/bin/entrypoint.py", line 111, in _dispatch_execute
    task_def = load_task()
               ^^^^^^^^^^^
      File "/opt/conda/lib/python3.11/site-packages/flytekit/bin/entrypoint.py", line 435, in load_task
    return resolver_obj.load_task(loader_args=resolver_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/opt/conda/lib/python3.11/site-packages/flytekit/core/utils.py", line 308, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
      File "/opt/conda/lib/python3.11/site-packages/flytekit/core/python_auto_container.py", line 262, in load_task
    task_module = importlib.import_module(name=task_module)  # type: ignore
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/opt/conda/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
j
This error typically means the Python module path (PYTHONPATH) inside your Flyte container does not include the directory where your
src
package lives, so imports like
import src.foo
fail. To debug, ensure that when you register/package your workflow (using
pyflyte package
or
pyflyte register
), you specify the correct source root with the
--source
flag if your code is under a
src
directory, e.g.,
pyflyte --pkgs workflows package --source ./src -f
. Also, make sure your Docker image or ImageSpec sets the correct
WORKDIR
and/or
PYTHONPATH
so that
src
is discoverable at runtime. See the Flyte docs on fast registration and packaging and troubleshooting ModuleNotFoundError. Would you like a step-by-step checklist to resolve this, or more detail on Flyte packaging and Docker image setup? Sources: - Flyte: Fast registration and packaging - Flyte: Registering workflows and troubleshooting ModuleNotFoundError 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@ancient-wolf-19325 Yes I would you like a step-by-step checklist to resolve this, or more detail on Flyte packaging and Docker image setup
j
Certainly! Here’s a step-by-step checklist to resolve
ModuleNotFoundError: No module named 'src'
in Flyte: 1. Project Structure: Ensure your project has a
src
directory with an
__init__.py
file (to make it a Python package), e.g.:
Copy code
my_project/
     src/
       __init__.py
       my_module.py
     workflows/
       my_workflow.py
2. Packaging with pyflyte: When packaging, specify the source root so Flyte can find your modules:
Copy code
pyflyte --pkgs workflows package --source ./src -f
This tells Flyte to include the
src
directory in the package, making
src
importable at runtime (source). 3. Docker Image/WORKDIR: If you use a custom Dockerfile, set
WORKDIR
to the project root (or wherever
src
is located), or add
src
to `PYTHONPATH`:
Copy code
WORKDIR /root
   ENV PYTHONPATH=/root/src:$PYTHONPATH
This ensures Python can import from
src
(source). 4. ImageSpec (if used): If you use Flyte’s
ImageSpec
, set the
source_root
parameter to point to your source directory:
Copy code
image_spec = ImageSpec(
       registry="ghcr.io/my-org",
       name="my-image",
       base_image="ghcr.io/flyteorg/flytekit:py3.11-latest",
       requirements="requirements.txt",
       source_root="src"
   )
5. Check Imports: Use absolute imports in your code, e.g.,
from src.my_module import foo
, and ensure all directories have
__init__.py
. 6. Register and Run: Register your packaged code and run the workflow as usual. If you still see the error, check the pod logs and verify the container’s
PYTHONPATH
and working directory. Following these steps should resolve most module import issues related to custom source directories in Flyte. Sources: - Flyte: Fast registration and packaging - Flyte: Registering workflows and troubleshooting ModuleNotFoundError 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.