# Calling slicer module from a python file

**URL:** https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720
**Category:** Support
**Tags:** python, module
**Created:** [July 24, 2020, 2:28pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720 "2020-07-24T14:28:27Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Anand\_Mulay](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/anand_mulay/32/6850_2.png) [@Anand\_Mulay](https://discourse.slicer.org/u/Anand_Mulay)
#### Post date: [July 24, 2020, 2:28pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/1 "2020-07-24T14:28:27Z")

</div>

i know we can call the slicer module with command line arguments or calling from a batch file, can i write a python file to call slicer with same same command?

---

<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: [July 24, 2020, 3:40pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/2 "2020-07-24T15:40:59Z")

</div>

Yes, of course, you can use standard Python functions to run a process. See for example how it is done in `slicer.util.launchConsoleProcess` method:

> <https://github.com/Slicer/Slicer/blob/524979a2e91ff25de690909f141e6afbe1b1c6e5/Base/Python/slicer/util.py#L2347-L2392>

---

<div class="post-metadata">

### Author: ![Anand\_Mulay](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/anand_mulay/32/6850_2.png) [@Anand\_Mulay](https://discourse.slicer.org/u/Anand_Mulay)
#### Post date: [July 24, 2020, 4:23pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/3 "2020-07-24T16:23:52Z")

</div>

i tried to run slicer using subprocess.Popen and Subprocess.run , but the thing is it is not recognizing the code after the slicer path like : slicer.exe --python-code “slicer.module…” , it just runs the slicer and now further action.  
i’ll give a try to the example you’ve sent and let you know, i think i’m missing something ☹

---

<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: [July 24, 2020, 5:47pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/4 "2020-07-24T17:47:13Z")

</div>

It is all just standard Python, so you can find thousands of examples and many tutorials, documentation on the web. If you still have trouble then copy your complete script here so that we can have a look.

---

<div class="post-metadata">

### Author: ![Anand\_Mulay](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/anand_mulay/32/6850_2.png) [@Anand\_Mulay](https://discourse.slicer.org/u/Anand_Mulay)
#### Post date: [July 24, 2020, 5:49pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/5 "2020-07-24T17:49:27Z")

</div>

yeah, and i found it 🙂

thank you so much

---

<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: [July 24, 2020, 6:07pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/6 "2020-07-24T18:07:39Z")

</div>

Was there anything that you learned that could help others who will have similar needs in the future? Can you share the code snippet that ended up working?

---

<div class="post-metadata">

### Author: ![Anand\_Mulay](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/anand_mulay/32/6850_2.png) [@Anand\_Mulay](https://discourse.slicer.org/u/Anand_Mulay)
#### Post date: [July 24, 2020, 6:29pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/7 "2020-07-24T18:29:27Z")

</div>

Yes here is my code snippet

> import os  
> import subprocess
> 
> os.chmod(‘C:\Program Files\Slicer 4.10.2\slicer.exe’,0o777)
> 
> args1= “‘C:\dicomfiles\study’”
> 
> arsg2= “‘any other args if any’”
> 
> args3 = 'C:\Program Files\Slicer 4.10.2\slicer.exe --python-code ’  
> args = args3 + args1 + args2  
> proc = subprocess.Popen(args)  
> proc.communicate()  
> returnCode = proc.poll()

the first thing is i dont know why without the os.chmod i got permission issue,  
also any easy way to pass the path as args, as if folder name start with A or T it didn’t work ☹

This community is awesome 😃

---

<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: [July 24, 2020, 9:36pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/8 "2020-07-24T21:36:52Z")

</div>

> [@Anand\_Mulay](#):
>
> if folder name start with A or T it didn’t work

Backslash `\` is an escape character in Python string literals. See in this post how you can use backslash characters:

> [@Subprocess.call does not work!](https://discourse.slicer.org/t/subprocess-call-does-not-work/2837/14):
>
> FYI, since backslash (\) is an escape character in Python, you cannot simply use it in a string literal in Python. The easiest is to indicate that you specify a raw string by prepending an r character before the string. This is correct: somePath = r"F:\someFolder\python.exe" If you need to use a regular string then backslash can be entered by typing double-backslash. This is correct: somePath = "F:\\someFolder\\python.exe" You may also use Unix-type separators. This is correct: somePath = …

---

<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 9, 2021, 12:52pm UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/9 "2021-06-09T12:52:59Z")

</div>

A post was split to a new topic: [Is Slicer’s Python environment complete?](https://discourse.slicer.org/t/is-slicers-python-environment-complete/18038)

---

<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: [August 16, 2024, 10:50am UTC](https://discourse.slicer.org/t/calling-slicer-module-from-a-python-file/12720/10 "2024-08-16T10:50:06Z")

</div>

A post was merged into an existing topic: [The external python program calls the slicer plug-in, or installs the plug-in in other software](https://discourse.slicer.org/t/the-external-python-program-calls-the-slicer-plug-in-or-installs-the-plug-in-in-other-software/37900/5)
