# Import and use existing code/library in a new module

**URL:** https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262
**Category:** Support
**Created:** [May 3, 2022, 1:42pm UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262 "2022-05-03T13:42:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![llafitte007](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@llafitte007](https://discourse.slicer.org/u/llafitte007)
#### Post date: [May 3, 2022, 1:42pm UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262/1 "2022-05-03T13:42:15Z")

</div>

Hi,

in a previous work, I made a c++ library usable from a python program (using the cython tool). It works perfectly but has to be run from the command line.

Now I’m trying to develop a module that would allow to launch automatically some features of this library from the Slicer interface.

I manage to import the python code of my library (by adding the path in sys.path) but this code imports a .so/.pyx library compiled with cython and which does not work. (I don’t know if I’m making myself understood …)

The console error when I start Slicer :

```auto
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/llafitte007/Slicer-4.11.20210226-linux-amd64/lib/Python/lib/python3.6/imp.py", line 170, in load_source
    module = _exec(spec, sys.modules[name])
  File "<frozen importlib._bootstrap>", line 618, in _exec
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/llafitte007/ __CODE__ /primetimeire/SlicerExtensions/Primetime/NeedlesCoordinates/NeedlesCoordinates.py", line 23, in <module>
    from NeedlesExtraction import NeedlesExtraction, PrimetimeConfiguration, Image
  File "/home/llafitte007/ __CODE__ /primetimeire/NeedlesExtraction.py", line 6, in <module>
    from wrappers import PyIrenaUtils
  File "/home/llafitte007/ __CODE__ /primetimeire/wrappers/ __init__.py", line 1, in <module>
    from .PyIrena import PyIrena, PyIrenaPatient, PyIrenaUtils
  File "/home/llafitte007/ __CODE__ /primetimeire/wrappers/PyIrena/ __init__.py", line 8, in <module>
    from PyIrena import PyIrena, PyIrenaPatient, PyIrenaUtils, ptype, ConfigKey
ModuleNotFoundError: No module named 'PyIrena'

```

Someone could help me ?

To resume, I tried to use a .so/.pyx library imported from a file I imported in a Slicer module … 🤨

---

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [May 3, 2022, 2:44pm UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262/2 "2022-05-03T14:44:22Z")

</div>

> <https://stackoverflow.com/questions/10968309/how-to-import-python-module-from-so-file>

---

<div class="post-metadata">

### Author: ![llafitte007](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@llafitte007](https://discourse.slicer.org/u/llafitte007)
#### Post date: [May 3, 2022, 3:49pm UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262/3 "2022-05-03T15:49:41Z")

</div>

I tried but it doesn’t work and I can’t show you the error… because nothing happens !

Slicer tries to open (loading window) then nothing : no error in the terminal, nothing !

I continue the investigation and I will keep you informed

---

<div class="post-metadata">

### Author: ![llafitte007](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@llafitte007](https://discourse.slicer.org/u/llafitte007)
#### Post date: [May 4, 2022, 8:37am UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262/4 "2022-05-04T08:37:07Z")

</div>

After several tests, it seems when Slicer started and load the library this makes it crash (absolutely no errors in console)

I tried manually it in base python console and it’s work  
I tried manually in slicer python console and it makes crash slicer without output errors.

Any idea what could cause slicer to crash when loading a library?

edit : can it be due to a different version of python between slicer (3.6) and during the build of the lib (3.8) ?

---

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/alex_vergara/32/15205_2.png) [@Alex\_Vergara](https://discourse.slicer.org/u/Alex_Vergara)
#### Post date: [May 4, 2022, 9:50am UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262/5 "2022-05-04T09:50:25Z")

</div>

> [@llafitte007](#):
>
> edit : can it be due to a different version of python between slicer (3.6) and during the build of the lib (3.8) ?

This is the thing. Try using the last version of slicer with python 3.9 and please try to compile your library also in python 3.9

---

<div class="post-metadata">

### Author: ![llafitte007](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@llafitte007](https://discourse.slicer.org/u/llafitte007)
#### Post date: [May 5, 2022, 9:25am UTC](https://discourse.slicer.org/t/import-and-use-existing-code-library-in-a-new-module/23262/6 "2022-05-05T09:25:58Z")

</div>

After recompile the library with python 3.9 and use Slicer 5.1 (also with python3.9) all work fine.

Also, for information with python3.9 there is a clear warning message when loading a library with wrong version python.
