Roland Tannous
Roland Tannous•6mo ago

Importing external files in the same directory

I have a question that might seem silly, but I couldn't find an answer through searching. I am currently running Marimo as a service using sysvinit on Ubuntu, with the root directory in the services file set to my home directory. When I create a Marimo project directory under my home directory, let's call the directory "experiment1", and try , from a marimo notebook in that same directory to import a utils.py file which is also in the experiment1 directory, I have to use the following syntax:
from home.mlengineer.experiment1.utils import X,Y,Z
from home.mlengineer.experiment1.utils import X,Y,Z
I have around 5-6 different notebooks, each importing from 2-3 external files using this method. As I duplicate the directories to run different experiments, it's becoming tedious to manually change the references in each notebook. Is there a way to import external files in Marimo notebooks without having to specify the full path every time, considering that the notebook and the external files are located in the same directory? Any hints or suggestions would be greatly appreciated!
11 Replies
Akshay
Akshay•6mo ago
Hey! Thanks for the question. Bear with me because I haven't run marimo (or jupyterhub) as a service before, so I have some basic questions/remarks in response. 1. When you say you're running marimo as a service: are you running marimo edit, which launches the homepage? Is that how you're opening notebooks? 2. When we start a notebook, we append the notebook directory to the Python path. So I'm surprised that marimo can't find modules colocated in the notebook directory. 3. Can you reproduce this issue without starting marimo as a service? Or is this issue specific to starting it as a service?
Roland Tannous
Roland TannousOP•6mo ago
1. yes I am basically setting up marimo to start as a service. This is the relevant section from the init configuration file for marimo:
PATH=/home/mlengineer/miniforge3/envs/deeplearning/bin/:/home/mlengineer/miniforge3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
dir="/home/mlengineer"
cmd="/home/mlengineer/miniforge3/envs/deeplearning/bin/marimo edit --headless --token --token-password="mytokenhere" --host 0.0.0.0 --port 8888"
user="mlengineer"
PATH=/home/mlengineer/miniforge3/envs/deeplearning/bin/:/home/mlengineer/miniforge3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
dir="/home/mlengineer"
cmd="/home/mlengineer/miniforge3/envs/deeplearning/bin/marimo edit --headless --token --token-password="mytokenhere" --host 0.0.0.0 --port 8888"
user="mlengineer"
It starts marimo as :
/home/mlengineer/miniforge3/envs/deeplearning/bin/marimo edit --headless --token --token-password="mytokenhere"
/home/mlengineer/miniforge3/envs/deeplearning/bin/marimo edit --headless --token --token-password="mytokenhere"
i am using sysvinit .. so init.d is a directory for initialization scripts, used by SysVinit, an old init and service manager. I can't run systemctl/systemd manager cause the machine is containerized. That above is a snapshot of my /etc/init.d/marimo
Akshay
Akshay•6mo ago
If you start marimo edit from your home directory, but not as a service, do you run into the same problem? I don't, so I want to see if this is somehow specific to running it as a service
Roland Tannous
Roland TannousOP•6mo ago
I will check and let you know by tomorrow. pulling an all nighter to finish a project 😄
Akshay
Akshay•6mo ago
Sounds good -- good luck!
Roland Tannous
Roland TannousOP•6mo ago
what's the equivalent of %%time in marimo? 😄 still getting used to it shrugging at the keyword based search on the website
Akshay
Akshay•6mo ago
We don't have Jupyter magics, so it would just be the Python equivalent. You could use the timeit module: https://docs.python.org/3/library/timeit.html
Python documentation
timeit — Measure execution time of small code snippets
Source code: Lib/timeit.py This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps fo...
Akshay
Akshay•6mo ago
We have a RAG bot for our docs (thanks @vrtnis!) in #ask-docs-ai
Roland Tannous
Roland TannousOP•6mo ago
sounds good! oh nice . thank you. still new to marimo. I am a 1 week old.
Akshay
Akshay•6mo ago
Of course! No problem. Thanks for using marimo, and thanks for the feedback/questions!
Roland Tannous
Roland TannousOP•6mo ago
i hope to migrate all my work to it. I had a major problem with notebooks on jupyterhub dying off when we get disconnected , especially where some workloads can literaly take hours. talk soon. thank you @Akshay i actually solved this. it was a question of specifying PYTHONPATH="/home/discy/discy/utils:${PYTHONPATH}" in the environment statement under the [program:marimo] section of supervisor /home/discy/discy/utils basically contains the .py files i want to import now i can do:
import promptutils
import promptutils
without having to:
import discy.discy.utils.promptutils
import discy.discy.utils.promptutils
i am basically running marimo as a server now