# How to hide the code of the script module?

**URL:** <https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135>\
**Category:** Development\
**Tags:** python, scripted\
**Created:** [November 8, 2022, 1:20pm UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135 "2022-11-08T13:20:00Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![MJamal](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@MJamal](https://discourse.slicer.org/u/MJamal)\
**Post date:** [February 10, 2024, 11:59am UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135/21 "2024-02-10T11:59:31Z")

</div>

Hello @jcfr,

I appreciate your suggested hints.

I’m wondering if your solution will work when generating the installer. Specifically, after creating the installer from Visual Studio and when a user installs slicer.exe, I want the installer directory to contain only .pyc files in the qt-scripted-directory.

I aim to automate this process to ensure that, in the end, the user only sees bytecode files. Is this achievable?

---

<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:** [February 10, 2024, 4:55pm UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135/22 "2024-02-10T16:55:34Z")

</div>

Yes, this solution will work when generating the installer: qt-scripted-modules folders will not contain any .py files.

---

<div class="post-metadata">

**Author:** ![MJamal](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@MJamal](https://discourse.slicer.org/u/MJamal)\
**Post date:** [February 11, 2024, 4:36am UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135/23 "2024-02-11T04:36:30Z")

</div>

Great.

So, @jcfr and @lassoan, since this is my first time contributing to open source, like ctk, I will need your guidance throughout the process of making these changes.

Following @jcfr’s suggested hints, here’s what I did:

Firstly, I’ve used [this](https://github.com/commontk/CTK/blob/master/CMake/ctk_compile_python_scripts.cmake.in) version of `ctk_compile_python_scripts.cmake.in`. I’ve also defined an option at the top of the file `ctkMacroCompilePythonScript.cmake`:

> [@jcfr](#):
>
> The `ctkMacroCompilePythonScript.cmake` CMake module could also be updated to define an option for controlling the behavior globally (e.g `CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY`).

```auto
# For default behavior
set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY FALSE)

```

  

Then,

> [@jcfr](#):
>
> To properly support “compiling” source file exclusively to `.pyc`, the CMake function `ctkFunctionAddCompilePythonScriptTargets` should be improved to accept an new parameter option (e.g `SKIP_SCRIPT_COPY`).

In `ctkFunctionAddCompilePythonScriptTargets` function , I’ve made the following changes:

```auto
if(NOT MY_GLOBAL_TARGET)
    ctkFunctionAddCompilePythonScriptTargets(${target} ${CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY})
  endif()

```

```auto
function(ctkFunctionAddCompilePythonScriptTargets target SKIP_SCRIPT_COPY)
  if (NOT ${SKIP_SCRIPT_COPY})
    _ctk_add_copy_python_files_target(${target} Script ${ARGN})
  endif()
  _ctk_add_copy_python_files_target(${target} Resource ${ARGN})
  _ctk_add_compile_python_directories_target(${target})
endfunction()

```

and in Slicer/CmakeLists.txt:

```auto
# Create targets CopySlicerPython{Resource, Script}Files, CompileSlicerPythonFiles
if(Slicer_USE_PYTHONQT)
  slicerFunctionAddPythonQtResourcesTargets(SlicerPythonResources)
  ctkFunctionAddCompilePythonScriptTargets(
    ${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}
    ${CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY}
    DEPENDS SlicerPythonResources
    )
....

```

  
Finally,

> [@jcfr](#):
>
> This new option should be added to `SlicerConfig.cmake.in`, that way extension build against Slicer would also leverage it.

In `SlicerConfig.cmake.in` , I’m setting up the flag of `CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY` to be `TRUE` :

```auto
set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY TRUE)

```

  
If I understood correctly, when the `SKIP_SCRIPT_COPY` argument is set to TRUE, I don't think the compilation step will work, since `CompileSlicerPythonFiles` depends on `CopySlicerPythonScriptFiles` target. Is this correct, or did I make a mistake in the changes?

---

<div class="post-metadata">

**Author:** ![MJamal](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@MJamal](https://discourse.slicer.org/u/MJamal)\
**Post date:** [February 13, 2024, 6:18am UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135/24 "2024-02-13T06:18:21Z")

</div>

> [@jcfr](#):
>
> aims to ensure that Slicer scripts are consistently compiled as legacy `.pyc` files.

@jcfr, I’d like to provide an update on this. I’ve observed that all scripts are compiled as `.pyc` files except for the `SegmentEditorEffects/ __init__.py` script.

---

<div class="post-metadata">

**Author:** ![MJamal](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@MJamal](https://discourse.slicer.org/u/MJamal)\
**Post date:** [March 14, 2024, 4:07am UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135/25 "2024-03-14T04:07:05Z")

</div>

@jcfr and @cpinter, What I have found is that the CMake file:

`slicersources-src\Modules\Loadable\Segmentations\EditorEffects\Python\CMakeLists.txt`

does not include an entry for the `SegmentEditorEffects/ __init__.py` file when setting `SegmentEditorEffects_PYTHON_SCRIPTS`, but instead uses the template `SegmentEditorEffects. __init__.py.in` for configuration.

As per my understanding, we have two ways to handle the compilation for the ` __init__.py` file:

1. We can configure `SegmentEditorEffects. __init__.py.in` to be in the same directory `slicersources-src\Modules\Loadable\Segmentations\EditorEffects\Python\` instead of the scripted modules directory and add an entry for it.
2. Or we can completely replace the template file with the ` __init__.py` file and add the entry directly, as I see there is nothing to be configurable in the template file.

What are your thoughts on this?

Also, following up the PR: [https://github.com/commontk/CTK/pull/1192](https://github.com/commontk/CTK/pull/1192).

To completely support ‘.pyc’ for the scripted module `SubjectHierarchy`,  
the file `slicersources-src\Modules\Loadable\SubjectHierarchy\Widgets\Python\ __init__.py` should also be updated to check the file extention:

From:  
`if fileExtension == '.py' and fileNameNoExtension != ' __init__'`  
To:  
`if fileExtension in ('.py', '.pyc') and fileNameNoExtension != ' __init__'`

@jcfr, Do we have any update for the PR?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [March 20, 2024, 6:54pm UTC](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135/26 "2024-03-20T18:54:59Z")

</div>

Corresponding approach was revisited by instead introduce the option `CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC` to support removing `*.py` script once the corresponding `*.pyc` file has been generated in the destination directory.

See [https://github.com/commontk/CTK/pull/1192](https://github.com/commontk/CTK/pull/1192)

@MJamal At your convenience, consider reviewing and testing the proposed changes 🙏

[Previous page](https://discourse.slicer.org/t/how-to-hide-the-code-of-the-script-module/26135.md?page=1)
