How can I pass environment variabel PYTHONPATH to jupyter notebook?

Dear forum,

My system is a Linux system running Debian 9. The machine is a TX 100 S1 (4 cpus).

I have implemented or better called jupyter notebook in a terminal session, no problem at all. The object is to share import (environment) files (python code that is in a “/home/pymystuff/ directory). What I have learned is that if I can start a program using the terminal, python will use the PYTHONPATH variable. However when using Notebook the environment is NOT setup by reading a .bashrc file. This means that PYTHONPATH=”/home/py/mystuff/" is not available so the ,py files in /home/py/mystuff/ can’t be imported .

I need to know were to add PYTHONPATH to the jupyter notebook environment so that everyone can use it anytime. I have looked at changing many of the configuration files but haven’t found the right one.

thanks for a “good” answer

Jörg

1 Like

Hi Jörg,

One approach that can be used is to extend your kernel specification’s configuration to include an env: element consisting of various environment variables that Notebook will include in the kernel’s environment. This element is added into the appropriate kernel.json file associated with the kernel. To locate the directory containing kernel.json issue the following command from a terminal:

jupyter kernelspec list

Locate the desired kernel in the left column to identify its specification directory via the column on the right. In that directory will be a kernel.json file. Assuming you’re using the standard Python kernel (ipykernel), your file should resemble the following:

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

Add an env: element consisting of the environment variables you wish to include in this kernel’s environment. Using your use case as an example, the contents should resemble the following:

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python",
 "env": {
   "PYTHONPATH": "/home/py/mystuff/"
 }
}

Since PYTHONPATH is a system variable and had you wanted to extend it with your customization, you could set the entry to:

"PYTHONPATH": "${PYTHONPATH}:/home/py/mystuff/"

This will then append your value to the value present in the notebook server’s environment.

6 Likes

Thank you dear Kevin
I made these changes, however after that I did not have any python console.

The terminal sequence is a bit cryptic for me, however I put it here:

(base) joerg@primergy:~$ jupyter notebook
[I 19:26:02.446 NotebookApp] JupyterLab extension loaded from /home/joerg/anaconda3/lib/python3.8/site-packages/jupyterlab
[I 19:26:02.446 NotebookApp] JupyterLab application directory is /home/joerg/anaconda3/share/jupyter/lab
[I 19:26:02.449 NotebookApp] Serving notebooks from local directory: /home/joerg
[I 19:26:02.449 NotebookApp] Jupyter Notebook 6.1.4 is running at:
*[I 19:26:02.450 NotebookApp] a link *
[…]
[I 19:26:02.450 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
*[C 19:26:02.479 NotebookApp] *

  • To access the notebook, open this file in a browser:*
  •    a link*
    
  • Or copy and paste one of these URLs:*
  •    a link*
    
  • or a link*
    

[W 19:26:06.094 NotebookApp] Error loading kernelspec ‘python3’
*** Traceback (most recent call last):***

  •  File "/home/joerg/anaconda3/lib/python3.8/site-packages/jupyter_client/kernelspec.py", line 257, in get_all_specs*
    
  •    spec = self._get_kernel_spec_by_name(kname, resource_dir)*
    
  •  File "/home/joerg/anaconda3/lib/python3.8/site-packages/jupyter_client/kernelspec.py", line 200, in _get_kernel_spec_by_name*
    
  •    return self.kernel_spec_class.from_resource_dir(resource_dir)*
    
  •  File "/home/joerg/anaconda3/lib/python3.8/site-packages/jupyter_client/kernelspec.py", line 46, in from_resource_dir*
    
  •    kernel_dict = json.load(f)*
    
  •  File "/home/joerg/anaconda3/lib/python3.8/json/__init__.py", line 293, in load*
    
  •    return loads(fp.read(),*
    
  •  File "/home/joerg/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads*
    
  •    return _default_decoder.decode(s)*
    
  •  File "/home/joerg/anaconda3/lib/python3.8/json/decoder.py", line 337, in decode*
    
  •    obj, end = self.raw_decode(s, idx=_w(s, 0).end())*
    
  •  File "/home/joerg/anaconda3/lib/python3.8/json/decoder.py", line 353, in raw_decode*
    
  •    obj, end = self.scan_once(s, idx)*
    
  • json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: line 11 column 2 (char 169)*
    [I 19:27:11.899 NotebookApp] Uploading file to /pathContent-checkpoint.ipynb
    *[I 19:27:41.626 NotebookApp] Creating new file in *
    [W 19:27:52.676 NotebookApp] Forbidden
    [W 19:27:52.677 NotebookApp] 403 GET /api/sessions?_=1609698365735 (::1) 1.71ms referer=a link
    [W 19:27:52.678 NotebookApp] Forbidden
    [W 19:27:52.679 NotebookApp] 403 GET /api/terminals?_=1609698365736 (::1) 1.80ms referer=a link
    [W 19:28:00.758 NotebookApp] Forbidden
    [W 19:28:00.759 NotebookApp] 403 POST /api/terminals (::1) 1.45ms referer=a link
    [I 19:28:00.761 NotebookApp] 302 GET /tree (::1) 1.06ms
    [W 19:28:07.404 NotebookApp] Forbidden
    [W 19:28:07.405 NotebookApp] 403 GET /api/sessions?_=1609698365737 (::1) 1.68ms referer=a link
    [W 19:28:07.405 NotebookApp] Forbidden
    [W 19:28:07.406 NotebookApp] 403 GET /api/terminals?_=1609698365738 (::1) 1.70ms referer=a link
    [W 19:28:14.706 NotebookApp] Forbidden
    [W 19:28:14.707 NotebookApp] 403 GET /api/contents?type=directory&_=1609698365739 (::1) 1.38ms referer=a link
    [W 19:28:23.561 NotebookApp] Forbidden
    [W 19:28:23.562 NotebookApp] 403 GET /api/contents?type=directory&_=1609698365740 (::1) 1.51ms referer=a link
    ^C[I 19:28:37.304 NotebookApp] interrupted
    Serving notebooks from local directory: /home/joerg
    0 active kernels
    Jupyter Notebook 6.1.4 is running at:
    a link
  • or a link*
    Shutdown this notebook server (y/[n])? y
    [C 19:28:39.967 NotebookApp] Shutdown confirmed
    [I 19:28:39.967 NotebookApp] Shutting down 0 kernels
    [I 19:28:39.968 NotebookApp] Shutting down 0 terminals

Moreover: after starting the console (a new one) I could not find the button for Python 3 … only “text” and two other entries.

the changed JSON-file as follows:

{
“argv”: [
“/home/joerg/anaconda3/bin/python”,
“-m”,
“ipykernel_launcher”,
“-f”,
“{connection_file}”
],
“display_name”: “Python 3”,
“language”: “python”
“env”: {
“PYTHONPATH”: “{PYTHONPATH}:{HOME}/kpdocs/AGW/Pierrehumbert”
}
}

Then I logged out and now: the so called “credentials” are not accepted. What are those?
Can you help me a bit to proceed further?

jörg

PS: after now restarting j notebook:

the screen in the upper right corner says:

  • Other:
  • [Text File]
  • [Folder]
  • [Terminal]

The notebook server is failing to load this particular kernelspec (kernel.json file) because it is not valid JSON. Please add a comma following the language entry. In addition, you need to add $ prior to the templated references. Once completed, your file should look like this…

{
  “argv”: [
    “/home/joerg/anaconda3/bin/python”,
    “-m”,
    “ipykernel_launcher”,
    “-f”,
    “{connection_file}”
  ],
  “display_name”: “Python 3”,
  “language”: “python”,
  “env”: {
    “PYTHONPATH”: “${PYTHONPATH}:${HOME}/kpdocs/AGW/Pierrehumbert”
  }
}

Because kernelspecs are automatically reloaded, there is no need to logout to get these changes to take effect. In fact, there’s really no need to stop the notebook server when applying changes to these files. You just need to force their reload by refreshing the browser.

Hurray - it works - thank you so much

I forgot: Happy New Year 2021 and keep a good health

a final (?) question: What does the “404 GET” mean? in the protocoll? normally within email traffic etc. “404” means "connection refused or so.

[I 20:35:09.377 NotebookApp] Saving file at /Untitled7.ipynb
[I 20:38:14.000 NotebookApp] Creating new notebook in
[I 20:38:17.776 NotebookApp] Kernel started: b54ae93b-9881-4f6b-986a-90d4e49e693e, name: python3
[W 20:38:49.579 NotebookApp] 404 GET /api/contents/NewNotebookPierrehumbert.ipynb?type=notebook&content=0&=1609702696286 (::1): No such file or directory: NewNotebookPierrehumbert.ipynb
[W 20:38:49.579 NotebookApp] No such file or directory: NewNotebookPierrehumbert.ipynb
[W 20:38:49.579 NotebookApp] 404 GET /api/contents/NewNotebookPierrehumbert.ipynb?type=notebook&content=0&
=1609702696286 (::1) 1.81ms referer=http://localhost:8888/notebooks/Untitled8.ipynb?kernel_name=python3
[I 20:38:49.616 NotebookApp] Uploading file to /NewNotebookPierrehumbert.ipynb
[I 20:39:00.003 NotebookApp] Saving file at /NewNotebookPierrehumbert.ipynb
[W 20:39:51.038 NotebookApp] 404 GET /api/contents/Pierrehumbert.ipynb?type=notebook&content=0&=1609702387574 (::1): No such file or directory: Pierrehumbert.ipynb
[W 20:39:51.038 NotebookApp] No such file or directory: Pierrehumbert.ipynb
[W 20:39:51.039 NotebookApp] 404 GET /api/contents/Pierrehumbert.ipynb?type=notebook&content=0&
=1609702387574 (::1) 1.72ms referer=http://localhost:8888/notebooks/Untitled7.ipynb?kernel_name=python3
[I 20:39:51.115 NotebookApp] Uploading file to /Pierrehumbert.ipynb

Thank you - same to you. I’m glad you’re moving forward.

404 is the HTTP error code for ‘Not found’ and the “No such file or directory” references will be relative to your notebook-dir option - which, if not set of configured - will be relative to the directory in which you started the notebook server.

It’s not clear to me why such messages are being produced since the file name is likely present in your browser in order to generate the reference in the API request in the first place.

Perhaps someone more familiar with the contents API can chime in?

Take care,
Kevin.

So let us wait for another expert who can tell about the 404 Error. I found still the following after switching to another console:

Invalid credentials

Token authentication is enabled

If no password has been configured, you need to open the notebook server with its login token in the URL, or paste it above. This requirement will be lifted if you enable a password .

This did not work, jupyter does not know my credentials …

1, What is a credential?
2. I normally can log in with my mail-address plus PW … which PW do I need here?

here a screenshot (does it upload automatically?):

I just happened to solve this “credential” problem:
I have to copy the awfully long string after “token” into the credential-field … and it works

jupyter seems to be very “unwieldy” — :smiley:

Normally, the browser should automatically start with the token in the initial URL and the copy/paste is not necessary. Since it sounds like that isn’t happening for you, I would agree with your “unwieldy” description. I’m not exactly sure when the auto-start doesn’t occur (unless the --no-browser option is provided), but you might be better off configuring a password instead. Here’s the section of the docs that covers the token usage (not sure why there isn’t a link between these two sections - seems like a helpful contribution - :wink:).

Thanks Kevin,
yesterday I had a strange experience with these particular things:

I could not restart the jupyter console and came to that “screen” I had posted . … Then I had the idea of creating a “base-password” via jupyter (do not know now the exact phrasing) - and using this “base-password” - well: it worked …

However I am not able to reproduce this at this moment …

I just posted another strange issue in another thread … something which has to do with the environment issue …

Evidently the documentation of jupyter needs an update for newbies … :grin: