Adding repo's subdirectories to PATH for mybinder's jupyter kernel

I am using mybinder.org for a jupyter kernel in executable cells enabled by thebe(lab) in a website.

The option to start the kernel for a notebook in a selected folder given in the thebe config file as kernelOptions: { path: “src/code” } doesn’t seem to work, because the kernel starts in the root directory of the repo. Since this is a known issue in the executablebooks project, but it hasn’t been solved (thebe always starts in the repository root, instead of the current subdirectory ), I would like to discuss a potential solution using the postBuild or start file.

I tried various attempts using shell or bash syntax in postBuild and/or start file to add a subdirectory containing some python modules to PATH, so they can be imported in any cell on the website, but it seems that I am missing how mybinder’s jupyter kernel is deployed, because those scripts are ineffective:

Folder structure

The folder structure in the repo is: (in short repo’s_root/src/code)

repo
|_   src
     |_  code
         |_ sharedlib1.py
         |_ sharedlib2.py

if I use the following in any cell, sharedlib modules are imported correctly:

cd src/code

the output of pwd inside the cell is then

/home/jovyan/src/code

Attempts to add to PATH

So I thought the most promising approach would be a start script for the repo2docker deployment of mybinder using bash to simply add this folder to PATH:

#!/bin/bash

set - e

echo "export PATH=$PATH:${HOME}/src/code" >> /etc/profile

e exec "$@"

It went through but was ineffective.

I also made an attempt with an ipython_config.py but I couldn’t figure out where to place this file either or how to inject it using start file:

c.InteractiveShellApp.exec_lines = [
    'import sys; sys.path.append("/home/jovyan/src/code")'
]

A working but an impractical solution (too many cells to modify)

I could of course have the following in each cell, it works:

import sys
sys.path.append(‘/home/jovyan/src/code’)
from sharedlib1 import function1

Ideas?

What would be the best practice to make sharedlibs.py available for each cell by default in this situation?

Have you tried setting PYTHONPATH?

You could make an actual package, and pip install -e . it in postBuild. With flit, this is a single, short pyproject.toml.

So I managed to solve this problem without modifying the PATH - which seems to be blocked.

All my attempts to modify PATH, add/modify PYTHONPATH with various syntax and different approaches had failed, but I am not well versed with this procedure so it is possible that I just missed the correct way of doing this.

After all, a good-enough solution turned out to be possible with a start script configuration as per mybinder recommendation. Look up mybinder’s reference: (Configuration Files — Binder 0.1b documentation)

I placed the following start script in the directory “binder” that resides in the root of the repo:

#!/bin/bash

set -e

cd /home/jovyan/src/code

exec "$@"

The motivation for this is that the cwd is by default added to PATH, so starting the jupyter kernel in the directory with shared libraries solves the problem at hand for a test build.

For the final release, I would try making and installing an actual package, as suggested in: Adding repo's subdirectories to PATH for mybinder's jupyter kernel - #3 by bollwyvl