reflex-dev/reflex · error · VarValueError

Cannot find package associated with import {instruction.argv

Error message

Cannot find package associated with import {instruction.argval} in {self.func!r}.

What it means

While scanning a cached var's bytecode for imports, Reflex handles `IMPORT_NAME` (recording the top module) then `IMPORT_FROM`. If an IMPORT_FROM appears without a preceding IMPORT_NAME (self._last_import_name is empty), Reflex cannot associate the imported name with a package and raises VarValueError.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/dep_tracking.py:469

                    type(self)(
                        func=instruction.argval,
                        state_cls=self.state_cls,
                        tracked_locals=self.tracked_locals,
                    )
                )
            elif instruction.opname == "IMPORT_NAME":
                if instruction.argval is None:
                    continue
                self.scan_status = ScanStatus.GETTING_IMPORT
                self._last_import_name = instruction.argval
                importlib.import_module(instruction.argval)
                top_module_name = instruction.argval.split(".")[0]
                self.tracked_locals[instruction.argval] = sys.modules[top_module_name]
                self.top_of_stack = instruction.argval
            elif instruction.opname == "IMPORT_FROM":
                if not self._last_import_name:
                    msg = f"Cannot find package associated with import {instruction.argval} in {self.func!r}."
                    raise VarValueError(msg)
                if instruction.argval in self._last_import_name.split("."):
                    # `import ... as ...` case:
                    # import from interim package, update tracked_locals for the last imported name.
                    self.tracked_locals[self._last_import_name] = getattr(
                        self.tracked_locals[self._last_import_name], instruction.argval
                    )
                    continue
                # Importing a name from a package/module.
                if self._last_import_name is not None and self.top_of_stack:
                    # The full import name does NOT end up in scope for a `from ... import`.
                    self.tracked_locals.pop(self._last_import_name)
                self.tracked_locals[instruction.argval] = getattr(
                    importlib.import_module(self._last_import_name),
                    instruction.argval,
                )
                # If we see a STORE_FAST, we can assign the top of stack to an aliased name.
                self.top_of_stack = instruction.argval
            elif (

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Replace relative imports inside the cached var with absolute imports (`from mypkg.helpers import helper`).
  2. Move the import to module top level instead of inside the cached var function.
  3. If you must import inside the function, import the module first (`import mypkg.helpers as h`) then use `h.helper`.

Example fix

# before
@rx.var(cached=True)
def data(self) -> list:
    from .utils import transform
    return transform(self.rows)

# after
from myapp.utils import transform

@rx.var(cached=True)
def data(self) -> list:
    return transform(self.rows)
Defensive patterns

Strategy: validation

Validate before calling

import dis

def imports_are_absolute(func) -> bool:
    return all(i.opname != "IMPORT_FROM" or True for i in dis.get_instructions(func)) and "." not in (func.__code__.co_names or ())

Prevention

When it happens

Trigger: Bytecode where IMPORT_FROM is not preceded by IMPORT_NAME for the same statement — most commonly relative imports (`from .module import x`) or unusual import forms inside a cached var function whose tracking state was reset.

Common situations: Using relative imports inside a cached var body in a package (`from .helpers import helper`); module-level import machinery differences across Python versions; code that mutates sys.modules or uses importlib tricks inside the var function.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/b4d641bbcf6d1127. Report an issue: GitHub.