How can I modify Slicer's PYTHONPATH variable?

Hi all,

afaik, Slicer does not inherit the system’s environment variables at startup, does it? If so, where does the python kernel in Slicer read its PYTHONPATH environment variable from? I have a few convenience functions which I currently have to import manually in the python console after every startup via:
import sys
For my native python kernel (anaconda env) in my OS (Mac), I put that info into ~/.bash_profile:
export PYTHONPATH=/path/to/my/utility/code:$PYTHONPATH
in order to access my custom code in all projects.
So, two questions:

  1. Is there an equivalent I can do for Slicer, such that my utility paths are loaded at startup?
  2. I regularly replace my Slicer nightly build with the latest version. Apart from “losing” all my extensions, I would then lose this path as well. Is there a way to make ideally both (extensions and PYTHONPATH) persistent?

Many thanks in advance!!

You can put such initialization code in file in your HOME folder. See details here:

That’s great, many thanks for the quick response! :slight_smile:
About question 2: Is there any way to restore my extensions and my additional module paths after installing a new version of Slicer (e.g. a new nightly build), i.e. making them persistent? If not, I will keep re-loading/re-entering them manually.
Again, many thanks for your help!

In recent nightly builds, in the extension manager you can reinstall all previously installed extension by a few clicks.

If you want to add local modules (not installed through the extension manager) then you can do it manually in the slicerrc file. For example:

moduleFactory =

1 Like

OK, that’s excellent… many thanks for these tips!

1 Like