2

I am trying to work with jupyterlab on a remote server that I don't manage, and I want to add my custom libraries to the path so that I can import and use them. Normally, I would go into .bashrc and add to PYTHONPATH there using

export PYTHONPATH="/home/username/path/to/module:$PYTHONPATH"

but this hasn't worked. I have tried this in .bashrc and .bash_profile to no fortune. I have also tried

export JUPYTER_PATH="/home/username/path/to/module:$JUPYTER_PATH"

as I read that somewhere else, and tried it in both the files named above.

What else can I try?

Ideally I'd like to put in some line in jupyterlab that returns the file it is using to add to the path, is that possible?

Or perhaps there is some command I can type directly into a terminal that I can access through jupyterlab that would allow me to add things to my path perminantley. I know that I can use os.path.insert (or similar) at the start of a notebook but as there are certain things I will want to use in every notebook this is a less than ideal solution for me.

Thanks

Steven Thomas
  • 352
  • 2
  • 9
  • You are missing a closing `"`. `PYTHONPATH` is the correct variable to modify. This answer might be of your interest: https://stackoverflow.com/a/69399208/6646912 – krassowski Apr 05 '22 at 19:18
  • Sorry, the missing ```"``` is a typo in the question by me, I have that correct in my files. Changing ```PYTHONPATH``` hasn't worked, and I've tried changing it in ```.bashrc``` and ```bash_profile```, is there a different file that I should change, or a way to print out what file jupyterlab is using? Your suggested thread doesn't solve my issue entirely, as I have a few different files in different locations, so changing to one working directory doesn't really work for me, I really want to be able to multiple things to pythonpath, and don't want to insert to the path for each new notebook. – Steven Thomas Apr 06 '22 at 08:32
  • Maybe your `/home/username/path/to/module` is off? Have you tried running jupyterlab with `PYTHONPATH=/path/to/your/modules:$PYTHONPATH jupyter lab` (instead of using export)? – krassowski Apr 06 '22 at 08:42
  • Looking more at your question: if this is a remote server that you do not manage, your `export` has no effect because the `PYTHONPATH` has to be set prior to starting JupyterLab. You could potentially have a serve extension which modifies the PYTHONPATH for the server/all kernels in flight, but that would again require you to be able to load the extension prior to server startup. It is not clear from the question how much control you have. – krassowski Apr 06 '22 at 08:45
  • Unfortunatley I don't understand how it works well enough to be able to say straight away how much control I do or don't have. Can I just replace my lines without the ```export``` line before hand and try that? Is there an ```ipython_profile``` file that maybe I can use the ```insert``` command in to add to the pythonpath that way? – Steven Thomas Apr 06 '22 at 08:50

1 Answers1

2

In a Specific Notebook

Manually append the path to sys.path in the first cell of the notebook

import sys
extra_path = ... # whatever it is
if extra_path not in sys.path:
    sys.path.append(extra_path)

As a System Configuration

Modify ~/.ipython/profile_default/ipython_config.py using the shell functionality so that the path gets modified for every notebook.

If that file does not exist, create it by using ipython profile create.

Then insert the modification to sys.path into it by modifying the c.InteractiveShellApp.exec_lines variable, e.g.

c.InteractiveShellApp.exec_lines = [
 'import sys; sys.path.append(<path to append>)'
]

Partially stolen from this answer, which has a different enough context to warrant being a different question.

Dave
  • 7,555
  • 8
  • 46
  • 88