# How can I modify Slicer's PYTHONPATH variable?

**URL:** <https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069>\
**Category:** Support\
**Created:** [June 4, 2018, 4:37pm UTC](https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069 "2018-06-04T16:37:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [June 4, 2018, 4:37pm UTC](https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069/1 "2018-06-04T16:37:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 4, 2018, 5:57pm UTC](https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069/2 "2018-06-04T17:57:42Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_to_systematically_execute_custom_python_code_at_startup_.3F)

---

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [June 4, 2018, 8:42pm UTC](https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069/3 "2018-06-04T20:42:46Z")

</div>

That’s great, many thanks for the quick response! 🙂  
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!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 4, 2018, 9:33pm UTC](https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069/4 "2018-06-04T21:33:40Z")

</div>

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’])

---

<div class="post-metadata">

**Author:** ![mangotee](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mangotee](https://discourse.slicer.org/u/mangotee)\
**Post date:** [June 5, 2018, 6:30am UTC](https://discourse.slicer.org/t/how-can-i-modify-slicers-pythonpath-variable/3069/5 "2018-06-05T06:30:06Z")

</div>

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