# Sharing python code between (scripted loadable) modules

**URL:** https://discourse.slicer.org/t/sharing-python-code-between-scripted-loadable-modules/24921
**Category:** Development
**Created:** [August 25, 2022, 1:30pm UTC](https://discourse.slicer.org/t/sharing-python-code-between-scripted-loadable-modules/24921 "2022-08-25T13:30:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![shai-ikko](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shai-ikko/32/15765_2.png) [@shai-ikko](https://discourse.slicer.org/u/shai-ikko)
#### Post date: [August 25, 2022, 1:30pm UTC](https://discourse.slicer.org/t/sharing-python-code-between-scripted-loadable-modules/24921/1 "2022-08-25T13:30:27Z")

</div>

Hi,

I’m trying to add some functionality to Slicer, and I want to make a couple of separate modules which will use the same underlying libraries. It’s not clear to me if there’s a nice way to do this without explicit `sys.path` manipulation.

I’ve seen [Importing custom python module into scripted extension](https://discourse.slicer.org/t/importing-custom-python-module-into-scripted-extension/11673) and [Python scripted module code organization](https://discourse.slicer.org/t/python-scripted-module-code-organization/6339) which seem to imply that code to be imported into a module needs to sit in a sub-folder in the module, but that is not suitable for sharing code between modules; and I’ve seen [Specifying dependencies of a custom extension in the .s4ext file - #2 by jamesobutler](https://discourse.slicer.org/t/specifying-dependencies-of-a-custom-extension-in-the-s4ext-file/21582/2) which seems to imply that modules can depend on other modules to provide importables, but the example module there does not seem to import anything from its named dependencies…

Am I missing anything?

My hope is to be able to include both the separate modules, and their common infrastructure, in a single extension, but that is not a hard requirement.

Thanks!

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [August 25, 2022, 2:38pm UTC](https://discourse.slicer.org/t/sharing-python-code-between-scripted-loadable-modules/24921/2 "2022-08-25T14:38:27Z")

</div>

If one module provides a set of libraries it should be available to other modules that are loaded in the same Slicer session (e.g. import the Logic class from another module). You should never need to duplicate code or manipulate any python path variables manually.

---

<div class="post-metadata">

### Author: ![talmazov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/talmazov/32/3966_2.png) [@talmazov](https://discourse.slicer.org/u/talmazov)
#### Post date: [August 25, 2022, 11:07pm UTC](https://discourse.slicer.org/t/sharing-python-code-between-scripted-loadable-modules/24921/3 "2022-08-25T23:07:48Z")

</div>

Hey,  
I should chime-in since you referenced a topic I posted previously. In my case I have a custom library that was shared by two separate applications - a TCP/IP socket class. Let’s call it `sock.py`  
to use sock.py in a slicer scripted module I had to setup the directory structure as so

```auto
WidgetRootDir
 |
 | - slicerWidget.py
 | - subdirectory
      | - __init__.py
      | - sock.py

```

` __init__.py` contains

> from .sock import \*

And slicerWidget.py imports as so

> from subdirectory import sock

If you need to access another slicer module this is a clean way of doing so [Setting widget variable from --python-code on Slicer.exe start - #13 by talmazov](https://discourse.slicer.org/t/setting-widget-variable-from-python-code-on-slicer-exe-start/23908/13)

I hope this helps

~ Georgi
