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
sys.path.append("/path/to/my/utility/code")
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!!
Cheers,
Ahmad

You can put such initialization code in .slicerrc.py file in your HOME folder. See details here: https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_to_systematically_execute_custom_python_code_at_startup_.3F

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 = slicer.app.moduleManager().factoryManager()
moduleFactory.registerModule(qt.QFileInfo(‘c:/D/SlicerDebuggingToolsExtension/PyDevRemoteDebug/PyDevRemoteDebug.py’))
moduleFactory.loadModules([‘PyDevRemoteDebug’])

1 Like

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

1 Like