# python script in slicer environment 

**URL:** <https://discourse.slicer.org/t/python-script-in-slicer-environment/36784>\
**Category:** Development\
**Created:** [June 14, 2024, 2:37pm UTC](https://discourse.slicer.org/t/python-script-in-slicer-environment/36784 "2024-06-14T14:37:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adham](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Adham](https://discourse.slicer.org/u/Adham)\
**Post date:** [June 14, 2024, 2:37pm UTC](https://discourse.slicer.org/t/python-script-in-slicer-environment/36784/1 "2024-06-14T14:37:35Z")

</div>

I’m running a python script using PythonSlicer myscript.py. Here is my script:

```auto
import slicer

directory = "/path/to/files"

loadedVolumeNode = slicer.util.loadVolume(directory, {"singleFile": False})

```

I got this error message:

```auto
No module named 'logic'
Traceback (most recent call last):
  File "/path/to/myscript.py", line 9, in <module>
    loadedVolumeNode = slicer.util.loadVolume(directory, {"singleFile": False})
AttributeError: module 'slicer' has no attribute 'util'

```

Based on slicer documentation, slicer has a module named util. Why it can’t find it?

My slicer version is: Slicer 5.6.2.

Could you please help with this issue? thanks in advance.

---

<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 14, 2024, 2:41pm UTC](https://discourse.slicer.org/t/python-script-in-slicer-environment/36784/2 "2024-06-14T14:41:34Z")

</div>

PythonSlicer application does not start the Slicer application and so no Slicer modules are instantiated. You may be able to instantiate module logic classes manually and use them, but it is simpler to run your script in the Slicer application’s Python environment

```
Slicer --no-main-window --python-script "path/to/myfile.py"

```

---

<div class="post-metadata">

**Author:** ![Adham](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Adham](https://discourse.slicer.org/u/Adham)\
**Post date:** [June 14, 2024, 7:25pm UTC](https://discourse.slicer.org/t/python-script-in-slicer-environment/36784/3 "2024-06-14T19:25:56Z")

</div>

Thank you for your reply. I’m trying to run an app which uses slicer’s modules in the back end for visualization without using slicer’s user interface. Is it possible to install Slicer modules as a python package so I can import them in my environment?

---

<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 15, 2024, 3:37am UTC](https://discourse.slicer.org/t/python-script-in-slicer-environment/36784/4 "2024-06-15T03:37:00Z")

</div>

We are working on making Slicer core available as pip-installable packages, but it is a long-term project (we need to get funding for it and then work on it for a few years).

But there are several almost as good solutions that you can use until then. The easiest method is to prevent any GUI from appearing is to used dockerized versions of Slicer. But you can also launch Slicer once and hide its window. While Slicer is running, you can use [Slicer’s web API](https://slicer.readthedocs.io/en/latest/user_guide/modules/webserver.html) to run any commands (e.g., from a web app or from the command line) or you can conveniently use the [slicerio Python package](https://slicer.readthedocs.io/en/latest/user_guide/modules/webserver.html) to launch Slicer from Python and run commands in Slicer from any Python environment.

---

<div class="post-metadata">

**Author:** ![Adham](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@Adham](https://discourse.slicer.org/u/Adham)\
**Post date:** [June 18, 2024, 3:04am UTC](https://discourse.slicer.org/t/python-script-in-slicer-environment/36784/5 "2024-06-18T03:04:19Z")

</div>

Thank you so much for your guidance. I used :

```auto
Slicer.exe --python-script "C:/Users/myscript.py"

```

I run it through subprocess module from my main app and it worked !!

Thanks again, your replies helped me a lot.

Best regards.
