Import function from local module
This may be a silly question, but can I add a module with my custom scripts/functions from my local working directory and then reference these python files in the marimo notebook? I currently get a "no module found" / "missing package" error when I add
from local_folder.my_python_script import my_function
What would be the best way to do this?
The problem I'm trying to solve is to not have to copy the same functions into my notebooks, but have the base functions as in import into my notebook.
Could I use UV to build a package out of my base modules, and then use UV to install from a local wheel file? I don't know how to do this (in UV or in general), so any tutorials or links would be helpful.Solution:Jump to solution
So you guided me down the right path. I ended up converting my folder
local_folder
into a package using UV init and adding all dependencies in that folder environment. UV automatically added the package to my marimo environment as a workspace tool.
I then used `uv pip install -e local_folder/.' to add my created package to marimo.
I restarted marimo and it picked up all my functions. ...5 Replies
The import path is relative to where you start marimo (or Python) from. So if you ran
marimo edit ...
in the folder containing local_folder
, it should work.
More generally, you could create a package and install it locally, for example with an editable install (pip install -e .
) in the root directory of the local package.
This question has been asked a few times (it's a good question!), makes me think we should write a sort doc on it and include on our site. Though the answer isn't specific to marimo.
You'd need to first create a package (no need to upload it anywhere): https://packaging.python.org/en/latest/tutorials/packaging-projects/. Then do an editable install in a local environment. I haven't practiced researched this workflow using uv so I don't have any tips or guides to link unfortunately.Thanks for the feedback. It's weird. I've got the files that you am trying to import into my notebook running in a sub-folder from the directory I am running marimo from.
I checked in the terminal and in the notebook using os.getcwd(), and both return the same folder as the working folder.
I just upgraded to marimo 0.9.20, as I thought it may be a version issue, and it still gives me the missing package error when I try to use:
I just upgraded to marimo 0.9.20, as I thought it may be a version issue, and it still gives me the missing package error when I try to use:
from local_folder.my_python_script import my_function
Is there a setting I need to enable?
I have the default package manager set to UV and I can see all the installed packages.
My notebook seems to see my local import from ... import ...
as if it needs to be evaluated as a UV package and then finds that it is not installed. Is there a way to swap out the import resolution to prioritise checking if the folder exists first?
I have added init.py to both the current working directory and within the folder itself.There's no setting to enable. If a module is not found, then marimo prompts you to install it for convenience. But that's just UI sugar. marimo doesn't do anything special about import resolution. So I assume if you replace the marimo notebook with a Python file, include
from local_folder.my_python_script import my_function
, and run the script from the same directory you run marimo
from, you will see the same import errorHere's an example that works. Note
marimo
is run from the directory containing local_folder
. Does this work for you? Is your directory tree different?Solution
So you guided me down the right path. I ended up converting my folder
local_folder
into a package using UV init and adding all dependencies in that folder environment. UV automatically added the package to my marimo environment as a workspace tool.
I then used `uv pip install -e local_folder/.' to add my created package to marimo.
I restarted marimo and it picked up all my functions.
UV really made my life so easy. I didn't have to configure anything else. Plus, now I know how to create python packages 👻