ben-pr-p
ben-pr-p4mo ago

Relative Imports

I'm having trouble figuring out how to do relative imports (I'm not too familiar with Python modules). My folder structure is: /notebooks -- /public ---- my-notebook.py -- /private -- util.py /src -- app.py In my-notebook.py, which I'm running from the project root via marimo edit notebooks/public/my-notebook.py, I have:
from .notebooks.util import warehouse
from .notebooks.util import warehouse
But I get: ImportError: attempted relative import with no known parent package I imagine this is an obvious fix for someone with experience here! Just looking to set up re-use of shared setup functions across notebooks
4 Replies
Akshay
Akshay4mo ago
marimo edit notebooks/public/my-notebook.py
marimo edit does the same thing as python when it comes to sys.path. In particular notebooks/public/ will be on sys.path, but the directory from which you're running marimo won't be. That's why notebooks can't be found. You could manually set PYTHONPATH before running marimo, which I think will work. We could also consider appending the current directory to sys.path but I think that might break Python convention and lead to other issues, not sure
ben-pr-p
ben-pr-pOP4mo ago
I see. Is there a way to use modules to get around it so it’s not a relative import and I don’t need to think about that?
Akshay
Akshay4mo ago
I wish I had a straightforward answer for you, but if you google this you'll find a lot of confusion on the topic. If possible, you could organize your project into a Python package, and do an editable install of it. But that feels kind of hacky too. I can see if there's any way we can do something that just works for our users ...
ben-pr-p
ben-pr-pOP3mo ago
I have been googling and finding a lot of confusion :/ I'm just trying to make it so I can do:
from utils import warehouse
from utils import warehouse
Where warehouse is just a preconnected Clickhouse object, maybe with some common dataframes ready to initialize across. Just a function and variable or two 🙂