3

I want to use an environment variable as the location for the c.NotebookApp.notebook_dir parameter in jupyter_notebook_config.py but I have added

c.NotebookApp.notebook_dir = $JUPYTER_NOTEBOOKS

I have also created the variable

>export JUPYTER_NOTEBOOKS=/home/jupyter

but when I run Jupyter notebook I get the below error message and I can tell its not pulling the value of the variable and is trying to use the variable as a literal. Has anyone used environment variables successfully in jupyter notebook config?

[E 20:45:04.513 NotebookApp] Exception while loading config file /root/.jupyter/jupyter_notebook_config.py
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/site-packages/traitlets/config/application.py", line 562, in _load_config_files
        config = loader.load_config()
      File "/usr/local/lib/python3.6/site-packages/traitlets/config/loader.py", line 457, in load_config
        self._read_file_as_dict()
      File "/usr/local/lib/python3.6/site-packages/traitlets/config/loader.py", line 489, in _read_file_as_dict
        py3compat.execfile(conf_filename, namespace)
      File "/usr/local/lib/python3.6/site-packages/ipython_genutils/py3compat.py", line 198, in execfile
        exec(compiler(f.read(), fname, 'exec'), glob, loc)
      File "/root/.jupyter/jupyter_notebook_config.py", line 766
        c.NotebookApp.notebook_dir = $JUPYTER_NOTEBOOKS
                                                       ^
    SyntaxError: unexpected character after line continuation character
[I 20:45:04.523 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
Traceback (most recent call last):
  File "/usr/local/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "</usr/local/lib/python3.6/site-packages/decorator.py:decorator-gen-7>", line 2, in initialize
  File "/usr/local/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/notebook/notebookapp.py", line 1628, in initialize
    self.init_webapp()
  File "/usr/local/lib/python3.6/site-packages/notebook/notebookapp.py", line 1407, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/usr/local/lib/python3.6/site-packages/tornado/tcpserver.py", line 151, in listen
    sockets = bind_sockets(port, address=address)
  File "/usr/local/lib/python3.6/site-packages/tornado/netutil.py", line 174, in bind_sockets
    sock.bind(sockaddr)
OSError: [Errno 99] Cannot assign requested address

3 Answers 3

1

It isn't clear that you are using environment variables properly. I glanced at the docs and it looks like %env JUPYTER_NOTEBOOKS is the right way to do it. I checked this on my local system and it works (for a different env variable). Give that a try - good luck! :-) . Below are the docs.

https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-env

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

Comments

1

Thanks jimf your link helped. I was able to get things working because I was NOT using environment variables correctly. Instead of modifying the .py file I added the parameter to my command of starting jupyter notebook like this:

--NotebookApp.notebook_dir=$JUPYTER_NOTEBOOKS

I was then able to change the variable value and change where jupyter was looking for notebooks.

Comments

0

You can make use of environment variables in the jupyter_notebook_config.py file, but note that the file is a Python file, so (as you already figured out) bash syntax won't work. Instead you can use Python syntax. You can actually write any Python code you like in this file, so this would be valid syntax:

import os

c.NotebookApp.notebook_dir=os.getenv('JUPYTER_NOTEBOOKS')

This would allow you to do even more complex things if you needed.

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.