Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using environment variable in jupyter_notebook_config.py

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
like image 277
Alex Brown Avatar asked Jun 27 '26 19:06

Alex Brown


1 Answers

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

like image 194
jimf Avatar answered Jul 03 '26 05:07

jimf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!