Get help from the marimo community

Updated 4 months ago

Relative Imports

At a glance

The community member is having trouble with relative imports in their Python project, which has the following structure:

/notebooks -- /public ---- my-notebook.py -- /private-- util.py/src-- app.py

When running my-notebook.py from the project root, they get an ImportError when trying to import warehouse from util.py. The community members discuss potential solutions, such as manually setting PYTHONPATH or organizing the project into a Python package, but there is no clear consensus on a straightforward answer.

The community members express confusion around the topic of relative imports in Python and are looking for a way to share common setup functions across their notebooks without having to deal with the relative import issues.

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:
Plain Text
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
A
b
6 comments
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
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?
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 ...
I have been googling and finding a lot of confusion :/
I'm just trying to make it so I can do:
Plain Text
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 🙂
Add a reply
Sign up and join the conversation on Discord