Python Environment
This page gives a simple overview of the Slicer Python environment: what it is and the main ways to access it. For detailed questions, see the Python FAQ and the Script Repository.
You can consider each Slicer installation as a virtual Python environment - the same way as you create virtual environments using python or conda. It bundles its own Python interpreter and a set of preinstalled packages (such as VTK, ITK, NumPy, and Qt bindings) that are readily available to any script running in Slicer.
There are several ways to run Python code in this environment:
Python Console - the interactive Python shell built into the Slicer user interface. Use this to access the running application state, including the current scene (
slicer.mrmlScene), loaded modules (slicer.modules), and the application instance (slicer.app).PythonSlicerexecutable - a standalone Python interpreter, provided in thebindirectory of the Slicer installation, for using the Slicer Python environment outside of a running application (e.g. batch processing, IDE integration). It does not have access to a running application state.--python-scriptcommand line argument - runs a Python script file in the Slicer environment when starting the application.slicerrc.pystartup file - a script that is automatically executed each time Slicer starts, useful for customizing the environment or running setup code.Scripted modules - Python modules that integrate into Slicer’s user interface like any built-in module, for packaging reusable functionality.