3

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

5
  • You are missing a closing ". PYTHONPATH is the correct variable to modify. This answer might be of your interest: stackoverflow.com/a/69399208/6646912 Commented Apr 5, 2022 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. Commented Apr 6, 2022 at 8: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)? Commented Apr 6, 2022 at 8: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. Commented Apr 6, 2022 at 8: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? Commented Apr 6, 2022 at 8:50

1 Answer 1

4

In a Specific Notebook

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

import sys
extra_path = "/home/username/path/to/module" # whatever individual directory it is
if extra_path not in sys.path:
    sys.path.append(extra_path)

Note that sys.path is a list of individual directories.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.