How can I pass environment variabel PYTHONPATH to jupyter notebook?

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