# Calling C++ from Python

**URL:** https://discourse.slicer.org/t/calling-c-from-python/3998
**Category:** Support
**Created:** [September 6, 2018, 3:44am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998 "2018-09-06T03:44:53Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 6, 2018, 3:44am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/1 "2018-09-06T03:44:53Z")

</div>

Hi Andras,  
is it possible to call external c++ customised functions using slicer python.

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [September 6, 2018, 4:32am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/2 "2018-09-06T04:32:56Z")

</div>

(I moved this to a new topic – please start a new topic for new questions, rather than replying to the old one)

---

<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: [September 6, 2018, 5:03am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/3 "2018-09-06T05:03:50Z")

</div>

> [@Saima](#):
>
> is it possible to call external c++ customised functions using slicer python.

Yes. Most of Slicer core is implemented in C++, so you usually call C++ functions from Slicer’s Python interpreter. If you implement a C++ loadable module then you can call the methods implemented in them, too.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 6, 2018, 8:32am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/4 "2018-09-06T08:32:08Z")

</div>

how to call any examples available

---

<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: [September 6, 2018, 6:21pm UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/5 "2018-09-06T18:21:37Z")

</div>

For example, MRML nodes are implemented in C++. You can call their methods as you would do it for any native Python class:

```
node = slicer.mrmlScene.GetFirstNodeByName("Red")
print(node.GetName())
```

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 15, 2018, 9:49am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/6 "2018-09-15T09:49:21Z")

</div>

Hi Andras,  
If I have an external implementation of an algortihm in c++ and want to call it from python. how to do that. the implementation is outside the slicer enviornment but is in c++

---

<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: [September 15, 2018, 1:17pm UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/7 "2018-09-15T13:17:55Z")

</div>

The easiest is to create a CLI module in C++ and call that from Python. First (and hardest) step is to build Slicer, then follow Slicer developer tutorials.

---

<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: [September 16, 2018, 1:43pm UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/8 "2018-09-16T13:43:31Z")

</div>

If you already have another executable that reads and writes Slicer compatible data like nifti files, and if you want to avoid having to build slicer from source, then another option is to make a wrapper so that the other program looks like a Slicer CLI.

Here’s an example:

> **[GitHub - ouyangming/DRAMMS\_in\_Slicer: dramms integrated into slicer](https://github.com/ouyangming/DRAMMS_in_Slicer)**
>
> dramms integrated into slicer. Contribute to ouyangming/DRAMMS\_in\_Slicer development by creating an account on GitHub.

Note that if the other program uses shared libraries or python packages you may need to manipulate the environment’s path variables to make sure the program resolves correctly.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 18, 2018, 1:23am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/9 "2018-09-18T01:23:08Z")

</div>

Hi Andras,  
Can you please post the tutorials. I build the slicer previously. when I run slicer.exe it opens up slicer but it also show me text below: what does this mean? is it not successfully build.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/a/8a11ae2db3f4646c4fcccf733626d96b0858e484.png)

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 18, 2018, 1:24am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/10 "2018-09-18T01:24:23Z")

</div>

Hi Andras,  
Can you please refer to a good and easy to understand example for creating a CLI module in c++ and call from python.

---

<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: [September 18, 2018, 3:33am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/11 "2018-09-18T03:33:16Z")

</div>

> [@Saima](#):
>
> I build the slicer previously. when I run slicer.exe it opens up slicer but it also show me text below: what does this mean? is it not successfully build.

It seems that the build partially failed. I would recommend to build latest master version of Slicer (it’ll become stable within a few weeks) and following all recommendations in build instructions. For example, build might have failed because the build folder name is too long. It has to be really short, up to a few characters - for example C:\S4 for source and C:\S4D / C:\S4R for debug/release mode binary works.

> [@Saima](#):
>
> example for creating a CLI module in c++

These should help:

- [Slicer4\_Programming\_Tutorial](https://www.slicer.org/wiki/Documentation/Nightly/Training#Slicer4_Programming_Tutorial)
- [How to run a CLI module from Python](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_to_run_CLI_module_from_Python.3F)
- [Developing and contributing extensions for 3D Slicer](https://www.slicer.org/wiki/Documentation/Nightly/Training#Developing_and_contributing_extensions_for_3D_Slicer)
- [Create CLI module](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Modules#Command_Line_Interface_.28CLI.29)
- [Developer manual](https://www.slicer.org/wiki/Documentation/Nightly/Developers)

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 15, 2019, 3:03am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/12 "2019-07-15T03:03:27Z")

</div>

Hi peiper,  
I would need to know how to make a wrapper for externall c++ code. Could be please refer me to any good websites or examples.

Thank you

---

<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: [July 15, 2019, 12:47pm UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/13 "2019-07-15T12:47:33Z")

</div>

The typical way is to use something like SWIG, as done in [ITK](https://github.com/InsightSoftwareConsortium/ITKModuleTemplate).

Or you could do [something in python](https://docs.python.org/3/extending/extending.html).

Or as Andras points out above, pretty much any C++ code in Slicer that extends VTK or Qt gets wrapped for python automatically.

Hope that helps.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 16, 2019, 1:24am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/14 "2019-07-16T01:24:35Z")

</div>

is boost.python ok to use instead of swig or are there any limitations.

---

<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: [July 16, 2019, 11:48am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/15 "2019-07-16T11:48:54Z")

</div>

I believe anything that works for normal python would be okay. I’ve never tried boost.python but I suppose it would work. Just keep in mind that if you plan to redistribute your code there can be lots of issues making sure the libraries are all packaged and (on mac) fixed up correctly. The safest course is usually just to follow the exact pattern of something that is already maintained and known to work.

---

<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 16, 2019, 12:21pm UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/16 "2019-07-16T12:21:35Z")

</div>

> [@Saima](#):
>
> is boost.python ok to use instead of swig or are there any limitations.

Is there a specific reason you would not like to implement a Slicer CLI module? You can run it in Slicer using GUI and from Python and C++, or you can even using without Slicer, just running it as a standalone executable from the command line.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 18, 2019, 1:05am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/17 "2019-07-18T01:05:35Z")

</div>

Hi Andras,  
I could not find good documentation for CLI modules. Although there are good documentations and presentation for scripting modules but I need some good documentation for working on CLI modules. I am a just a beginner.

Could you please provide me with helpful documentation?

Regards,  
Saima Safdar

---

<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 18, 2019, 2:18am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/18 "2019-07-18T02:18:52Z")

</div>

Would you like to implement CLI modules or run them from Python? Would you like to implement in CLI module in Python or in C++?

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 18, 2019, 2:42am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/19 "2019-07-18T02:42:50Z")

</div>

I have an external c++ project. I need an interface to interact through slicer python with that project without disturbing the c++ project code itself.

Any suggestions please? Sorry if I confuse you with what I want to explain. Previously you told to make c++ loadable modules and then call it though slicer python.

then you suggested to build CLI module in c++ and call it from python. I am confused with what to follow.

I will be using slicer extensions as well the segment mesher and skull stipper. Along with that I need an interface to call the external c++ project.

---

<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 19, 2019, 12:56am UTC](https://discourse.slicer.org/t/calling-c-from-python/3998/20 "2019-07-19T00:56:56Z")

</div>

Implement a CLI module in C++ is probably the best way to go, as it is simpler, processing can run in the background, etc. I would only recommend implementing a loadable module instead, if you need to re-run the processing very frequently (many times per second) and so passing data to the CLI would become the bottleneck; or you need to pass many or very complex data structures that are not readily supported by the CLI interface.
