# Private Extension my own python code

**URL:** https://discourse.slicer.org/t/private-extension-my-own-python-code/27285
**Category:** Development
**Tags:** python, extensions
**Created:** [January 17, 2023, 5:54am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285 "2023-01-17T05:54:54Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 17, 2023, 5:54am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/1 "2023-01-17T05:54:54Z")

</div>

Operating system: window 11  
Slicer version: 4.13.0

Hello,  
I have two questions regarding ‘private extension’.

Q1. Is it correct that the extension created through the extension wizard works locally by default and becomes public only when it is submitted to Slicer’s index(?)

 ![Question](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/5/5582779b3c1905a2e12814b76ced2d991a336ad9.png)

- Create an extension file through ‘create extension’ of the extension wizard. ( I got it )

- Add Module to Extention

Q2) What is ‘Add Module to Extension’ ?  
Since a Python code file (.py) is usually called a module, I thought it was to add the function file I created in this part. However, I think I am wrong. Where should I add the source code files that I have implemented and other files necessary for source code operation?

---

<div class="post-metadata">

### Author: ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)
#### Post date: [January 17, 2023, 8:53am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/2 "2023-01-17T08:53:58Z")

</div>

> Q1. Is it correct that the extension created through the extension wizard works locally by default and becomes public only when it is submitted to Slicer’s index(?)

That is correct. The `Extension Wizard` module will create the local files that make up an extension. Once you have developed your extension, you need to make a PR to the `ExtensionsIndex` ([GitHub - Slicer/ExtensionsIndex: Slicer extensions index](https://github.com/slicer/extensionsindex)).

> Q2) What is ‘Add Module to Extension’ ?  
> Since a Python code file (.py) is usually called a module, I thought it was to add the function file I created in this part. However, I think I am wrong. Where should I add the source code files that I have implemented and other files necessary for source code operation?

An extension is made up of one or more modules (there are also different types of modules). You need to add at least one module for your extension to work, so in your case, you would probably want to add a `Python` module. This will generate the structure of a basic module, including a `.py` file where you can move your code.

It is worth mentioning that 3D slicer will ask you whether the path to your module should be added to the list of modules that Slicer will load. Doing it so, will have your module loaded in your current installation/build of 3D Slicer.

---

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 17, 2023, 9:46am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/3 "2023-01-17T09:46:15Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/9/b98ae4f71668c22b801f4eec16d82a126c1bf293.png)

I tried the method you told me before asking the question.

But I have qt module problems (I have included a key board shortcut custom function in my code.)

In my opinion, it seems to be a problem that occurs because the act of loading my custom module precedes the loading of the qt module. Should I do add Pyqt5 in my codes?

---

<div class="post-metadata">

### Author: ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)
#### Post date: [January 17, 2023, 1:44pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/4 "2023-01-17T13:44:38Z")

</div>

What is exactly the error that you get? Which version of Slicer are you  
using?

I’ve just created a new extension with a scripted module (all defaults)  
and it works flawlessly (3D Slicer git main branch).

---

<div class="post-metadata">

### Author: ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)
#### Post date: [January 17, 2023, 2:28pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/5 "2023-01-17T14:28:32Z")

</div>

> [@dsa934](#):
>
> Since a Python code file (.py) is usually called a module, I thought it was to add the function file I created in this part.

Note that “Slicer module” is a separate concept from “Python module”  
[https://slicer.readthedocs.io/en/latest/developer\_guide/module\_overview.html](https://slicer.readthedocs.io/en/latest/developer_guide/module_overview.html)

---

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 18, 2023, 12:53am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/6 "2023-01-18T00:53:37Z")

</div>

As I wrote above, I am using slicer -4.13.0-2021-08-24 version.

My code contains code to customize the ‘short cut’ of the 3d slicer.

If you delete the ‘short cut’ custom part and proceed with extension, the following error occurs.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/9/696d23e51b0b9e2b3834e60344c9995500443e5e.png)

If you don’t remove the ‘short cut’ part, you will get an error like below.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/1/01a2af5983cf68d0ceee18527f5413f16331707a.png)

The order in which I proceeded with the extension is as follows.

step 1) extension wizard - create extension - name( test )

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/0/c0d81325057ea9d2c6ff632e6edf926927f9f87b.png)

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/d/7dddfe977e8179ecb341f0fa05bafba3ffced1ae.png)

step 2) Add module to Extension (test\_my\_code)

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/9/393469ba7a36476fa6777adc94113a4cc6435874.png)

step 3) Add my custom code into test\_my\_code folder

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/5/756f390df7301132b6db768ccfba3c345ae6d3cf.png)

===============  
I thought adding the module in step 2) was adding the code file I created.  
However, when I click ‘add module to extension’, the screen that appears is as follows.

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/e/6e6ee4326200593edde64fa4e845e0a1f5affc4b.png)  
I think my understanding of this part is wrong, but since the Python code file itself is called a module, shouldn’t I be able to add the code I created in step 2?

---

<div class="post-metadata">

### Author: ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)
#### Post date: [January 18, 2023, 1:32pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/7 "2023-01-18T13:32:09Z")

</div>

Hello,

I would recommend you to switch to Slicer 5 (probably nothing to do with this issue, though).

Using Slicer 5, through the `ExtensionWizard` I got this structure:

```auto
/tmp/TestExtension/
├── CMakeLists.txt
├── TestExtension.png
└── TestModule
    ├── CMakeLists.txt
    ├── __pycache__
    │ └── TestModule.cpython-39.pyc
    ├── Resources
    │ ├── Icons
    │ │ └── TestModule.png
    │ └── UI
    │ └── TestModule.ui
    ├── Testing
    │ ├── CMakeLists.txt
    │ └── Python
    │ └── CMakeLists.txt
    └── TestModule.py

```

I added the following path to the Slicer module paths: `/tmp/TestExtension/TestModule`.

Maybe as a starting point use only the files generated by the extensions wizard.

You could verify that you have similar structure and your modules path  
looks similar.

---

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 18, 2023, 3:01pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/8 "2023-01-18T15:01:59Z")

</div>

Hello again

Unfortunately, since the slicer version has been unified( in my company), it cannot be changed.  
There is no problem with this configuration in my version either.  
The current problem situation is that the problem occurs from the moment the custom code is added.  
I temporarily solved the problem by replacing it with a method of adding code through the ‘Ctrl+r’ short cut key, but I’m still curious about the sure method for the extension.

> [@RafaelPalomar](#):
>
> ```auto
> /tmp/TestExtension/
> ├── CMakeLists.txt
> ├── TestExtension.png
> └── TestModule
> ├── CMakeLists.txt
> ├── __pycache__
> │ └── TestModule.cpython-39.pyc
> ├── Resources
> │ ├── Icons
> │ │ └── TestModule.png
> │ └── UI
> │ └── TestModule.ui
> ├── Testing
> │ ├── CMakeLists.txt
> │ └── Python
> │ └── CMakeLists.txt
> └── TestModule.py
> 
> ```

The above structure is automatically formed after the ‘Create Extension’ and ‘Add module to entension’ processes.  
If so, where is your custom code included in the structure above?  
In my slicer, I included my custom code directly in the directory after the above structure was created automatically.

like this

```auto
/tmp/TestExtension/
├── CMakeLists.txt
├── TestExtension.png
└── TestModule
    ├── CMakeLists.txt
    ├── __pycache__
    │ └── TestModule.cpython-39.pyc
    ├── Resources
    │ ├── Icons
    │ │ └── TestModule.png
    │ └── UI
    │ └── TestModule.ui
    ├── Testing
    │ ├── CMakeLists.txt
    │ └── Python
    │ └── CMakeLists.txt
    └── TestModule.py
    │
    └── my_custom_code.py (new !)

```

The part I found odd in the previous question is the ‘TestModule’ part.  
This part is the output of the ‘Add module to extension’ process, I thought I was adding my custom code here, but it actually seemed to create a dummy folder.  
Is it correct way to add custom code additions directly under these dummy modules?

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [January 18, 2023, 3:28pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/9 "2023-01-18T15:28:18Z")

</div>

Place your custom code in the template/example Slicer scripted loadable module that is TestModule.py. You must maintain the template structure contained in TestModule.py. It will not work to copy all code in my\_custom\_code.py and replace everything in TestModule.py.

---

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 18, 2023, 3:39pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/10 "2023-01-18T15:39:02Z")

</div>

@jamesobutler  
What you mean is not to add custom code inside TestModule, but to copy and add my CustomCode inside the TestModule.py code while keeping the contents of the existing TestModule.py?

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [January 18, 2023, 3:54pm UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/11 "2023-01-18T15:54:09Z")

</div>

TestModule.py is an example/template of a Slicer scripted loadable module that does some example functionality. You will replace that with your custom code, however you need to keep the framework of the classes in place (ScriptedLoadableModule/ScriptedLoadableModuleWidget/ScriptedLoadableModuleLogic). The framework makes sure the Slicer scripted loadable module is setup correctly upon starting Slicer.

---

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 19, 2023, 12:40am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/12 "2023-01-19T00:40:39Z")

</div>

@jamesobutler

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/b/5b8614223db0b505b7946e21b52bef1041becac8.png)

As you said, I think I can just copy-paste my custom code into the generated code file, but it’s frustrating because there are so many unknown errors. you’re driving me crazy 3D slicer~~~

edit :

I often use argparser in code, but I don’t know why it doesn’t work, so I just changed it to a global variable and solved it. but it doesn’t seem clean

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [January 19, 2023, 1:01am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/13 "2023-01-19T01:01:02Z")

</div>

The error you show looks like pure python issues rather than Slicer. Does your code take command line input arguments or is it built with GUI input/selection in mind? Are you creating a Slicer scripted module or a Slicer scripted CLI module? See module types at [Module Overview — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/module_overview.html#module-overview). A scripted CLI is an option in the extension wizard of the latest stable Slicer.

---

<div class="post-metadata">

### Author: ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)
#### Post date: [January 19, 2023, 6:49am UTC](https://discourse.slicer.org/t/private-extension-my-own-python-code/27285/14 "2023-01-19T06:49:48Z")

</div>

The problem didn’t occur in all cases except when pasting the custom code into the extension module file. I don’t think it’s a code problem. I’ll have to keep thinking about why argparser only gives an error in this case.
