Does the cell magic `%%cell_to_module`support sing...
# hamilton-help
v
Does the cell magic `%%cell_to_module`support single-python-file imports without
__init__
file? e.g. I have a Jupyter Lab cell, that does an
*from* dag_common *import* multiline_cleaning
, and dag_common.py is merely a single file in the current directory? I can make
*from* dag_common *import* *
work, but not the explicit function as above.
I spoke too soon, even the
*
doesn't work.
t
hmm, I don't think there's any reason
*from* dag_common *import* multiline_cleaning
or
import *
shouldn't work. I'll try to repro and investigate! Thanks for submitting the issue
Here's the check I made: 1-define
foo
in
my_module.py
Copy code
# my_module.py
def foo() -> int:
   return 13
2-define notebook this notebook that imports
foo
and defines
bar
Copy code
%load_ext hamilton.plugins.jupyter_magic

## cell 2
%%cell_to_module
from my_module import foo

def bar(foo: int) -> int:
  return foo
3-define
my_module2.py
that imports
foo
and defines
bar
(like the notebook)
Copy code
from my_module import foo

def bar(foo: int) -> int:
    return foo + 1
4-create a
Driver
that imports
my_module2
Copy code
if __name__ == "__main__":
    from hamilton.driver import Builder

    import my_module2

    dr = Builder().with_modules(my_module2).build()
    dr.display_all_functions("my_module2.png")
Both for the notebook and module, I obtain the same visualization / DAG
From these checks, the behavior seems to be consistent. What was the expected behavior? I assumed that you were expecting this to behaver as if
foo
and
bar
were defined in the same notebook cell (see picture). Maybe I didn't understand the issue properly?
v
Weird, I'm doing almost exactly the same for the cell, and am getting the error ImportError: cannot import name 'multiline_cleaning' from 'dag_common' (/home/jovyan/work/development/dag_common.py) I'm using Python 3.11.9 and Jupyter Lab 4.2.5
The only difference I can see it, I'm adding the -m modulename, too %%cell_to_module -m parameters_ingress
t
are you able to import the function ìn a cell that doesn't use
%%cell_to_module
-m
is no longer required, can you try
%%cell_to_module my_module
?
v
you're right. That doesn't work, either. I should have tried that before bothering you. Now I need to investigate. Thank you 🙂
t
No problem, let me know if you need anything!
v
It was a missing empty last line. Problem solved. Thank you again.