Way to get jupyter server root directory

Viewed 8222

Consider following:

$ cd /home/mydir
$ jupyter notebook --port=8888

In plain English, I am running jupyter server from /home/mydir directory.

Is there a simple way to get this directory from within a notebook regardless if it's a R notebook or a Python notebook or whatever? Maybe there is some magic command or variable?

NOTE: getwd() is not an answer as it returns directory of a current notebook but not the jupyter server root.

5 Answers

I have a similar problem and found your post, although I don't see a viable solution yet. Eventually I did found a solution, although it works only because I only care about Linux and I only care about Python. The solution is this magic line:

J_ROOT = os.readlink('/proc/%s/cwd' % os.environ['JPY_PARENT_PID'])

(I put it in a module in my PYTHONPATH so that I can easily use it in any Python notebook.) See if it is good for you.

Frankly I'm surprised that all this time later there is still no built-in way to do this. I have used Isaac To's solution on Linux but recently had to make a jupyter notebook portable to Windows as well. Simply using os.getcwd() is fragile because a cell using it to set your JUPYTER_ROOT_DIRECTORY can potentially be called again, after you have changed your working directory.

Here is what I came up with:

try:
    JUPYTER_ROOT_DIRECTORY
except NameError: 
    JUPYTER_ROOT_DIRECTORY = os.getcwd()

I put it in one of the first couple cells with the initial import statements. If the cell gets called again, it will not re-set the variable value because the variable exists and does not throw an exception.

It should be noted that unlike Isaac To's solution it sets the value to the directory the current .ipynb was run from, which is not necessarily the same directory as the top-level dir the user can access in the left hand file pane.

My suggestion is to use an intuitive approach.

  1. Create a new folder within the Jupyter environment with a very unique name, for example, T246813579.
  2. You can now locate the Jupyter working path by searching in your file explorer. For example, you can use the Windows Explorer in order to locate your new folder. The expected result should look something like this: C:\Users\my_user_name\JupyterHome\T246813579

The answer from @Isaac works well for Linux, but not all systems have /proc. For a solution that works on macOS and Linux, we can use shell commands, taking advantage of the ! shell assignment syntax in Jupyter:

import os

JPY_ROOT = ! lsof -a -p {os.environ['JPY_PARENT_PID']} -d cwd -F n | tail -1 | cut -c 2-
JPY_ROOT = JPY_ROOT[0]

print(JPY_ROOT)  # prints Jupyter's dir

Explanation:

  • Get the process ID (pid) of the current jupyter instance with os.environ['JPY_PARENT_PID']
  • Call lsof to list the process's open files, keeping only the current working dir (cwd)
  • Parse the output of lsof using tail and cut to keep just the directory name we want
  • The ! command returns a list, here having only one element

Alternate Version

To save the os import, we could also use shell commands to get the PID. We could also do the subsequent string wrangling in python, rather than calling tail and cut from the shell, as:

JPY_ROOT = ! lsof -a -p $(printenv | grep JPY_PARENT_PID | cut -d '=' -f 2) -d cwd -F n
JPY_ROOT = JPY_ROOT[2][1:]
Related