Slicer-Python Packages Use and Install

For Slicer 4.10 I would probably suggest opening a terminal window to the location of PythonSlicer and doing PythonSlicer.exe -m pip install scipy. The error you are getting is because the “pip” package was updated and “main” was moved out. You could try some work around as was suggested earlier in this discussion such as

from pip._internal import main as pipmain

If using Slicer 4.11, Slicer is now using Python3. You could also use a helper function within Slicer’s python interactor. Try slicer.util.pip_install('scipy').
Reference: Use full power of Python in Slicer

Note: If you install scipy in Slicer 4.10, which is using python2, you will not be using the latest version of scipy (1.3.0). Scipy 1.3.0 is only available for Python3.

1 Like

Thank you so much for your response. it worked. I am also going to update Slicer to 4.11.

Thank you again


Collecting pynrrd

Using cached

Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: ‘C:\Users\22374464\AppData\Local\Temp\pip-req-tracker-de0bnyq2\038c22fe9d64f3b52795137322e50c0d971005e9da8841928046b4a7’

Any idea of how to install pynrrd

Since you had issues using pipmain and I’m assuming you are using Slicer 4.10, then you should try PythonSlicer.exe -m pip install pynrrd to install this pure python package. Don’t use pipmain if you’re using Slicer 4.11 nightly.

If you’re using Slicer 4.11 nightly, you can easily use slicer.util.pip_install('pynrrd')as stayed above.

i am using slicer 4.11.0 nightly

I followed and got
Collecting pynrrd
Using cached
Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: ‘C:\Users\22374464\AppData\Local\Temp\pip-req-tracker-de0bnyq2\038c22fe9d64f3b52795137322e50c0d971005e9da8841928046b4a7’

Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\bin\Python\slicer\”, line 2002, in pip_install
File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\bin\Python\slicer\”, line 1955, in logProcessOutput
raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command ‘[‘C:/Users/22374464/AppData/Local/NA-MIC/Slicer 4.11.0-2019-07-06/bin/…/bin\PythonSlicer.EXE’, ‘-m’, ‘pip’, ‘install’, ‘pynrrd’]’ returned non-zero exit status 1.

I see you are using windows, therefore you have installed Slicer into Programs files folder without writing permissions for slicer. You shall uninstall Slicer and reinstall in a writable folder like Documents, etc. This will solve your problems.

It seems that it is a recent Slicer Preview version, which installs itself to a writeable location by default, so write access should not be an issue.

pynnrd does not seem to be available on PyPI:

>>> pip_install('pynnrd')
Loading Slicer RC file [C:/Users/andra/]
Collecting pynnrd
  Could not find a version that satisfies the requirement pynnrd (from versions: )
No matching distribution found for pynnrd
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\andra\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-15\bin\Python\slicer\", line 2002, in pip_install
  File "C:\Users\andra\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-15\bin\Python\slicer\", line 1955, in logProcessOutput
    raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command '['C:/Users/andra/AppData/Local/NA-MIC/Slicer 4.11.0-2019-07-15/bin/../bin\\PythonSlicer.EXE', '-m', 'pip', 'install', 'pynnrd']' returned non-zero exit status 1.

On your computer, there seem to be some cached pynrrd package, which is probably not compatible.

You should not need to import basic functionalities such as nrrd file reading from python.

You can read a nrrd file using vol=slicer.util.loadVolume('something.nrrd'), if you want to access it as a numpy array, call a=slicer.util.arrayFromVolume(vol). This approach has the advantage that you can directly manipulate the voxels that Slicer displays.

1 Like

@lassoan The output error you posted happened because you tried to install pynnrd instead pynrrd. Just a simple typo.

I was able to successfully install this pynrrd package using slicer.util.pip_install('pynrrd'). I do think the previous errors have been due to some messed up cache of the download. Deleting that cache and then retrying should work.


Good catch, thank you!

1 Like

thankyou so much yes it was a typo

Hi Andras,
gmsh-api is I need it within slicer. its succesfully installed but there is problem in using it within slicer. could you please help how to resolve the issue:


Requirement already satisfied: gmsh-api in c:\users\22374464\appdata\local\na-mic\slicer 4.11.0-2019-07-06\lib\python\lib\site-packages (0.1.5)

Requirement already satisfied: pandas in c:\users\22374464\appdata\local\na-mic\slicer 4.11.0-2019-07-06\lib\python\lib\site-packages (from gmsh-api) (0.25.1)

Requirement already satisfied: numpy in c:\users\22374464\appdata\local\na-mic\slicer 4.11.0-2019-07-06\lib\python\lib\site-packages\numpy-1.16.2-py3.6-win-amd64.egg (from gmsh-api) (1.16.2)

Requirement already satisfied: python-dateutil>=2.6.1 in c:\users\22374464\appdata\local\na-mic\slicer 4.11.0-2019-07-06\lib\python\lib\site-packages (from pandas->gmsh-api) (2.8.0)

Requirement already satisfied: pytz>=2017.2 in c:\users\22374464\appdata\local\na-mic\slicer 4.11.0-2019-07-06\lib\python\lib\site-packages (from pandas->gmsh-api) (2019.2)

Requirement already satisfied: six>=1.5 in c:\users\22374464\appdata\local\na-mic\slicer 4.11.0-2019-07-06\lib\python\lib\site-packages (from python-dateutil>=2.6.1->pandas->gmsh-api) (1.12.0)

but when I import the gmsh and try to run a script it gives me error. I am posting error below: The api works well on anaconda. I have used it before.
File “C:/Users/22374464/AppData/Local/NA-MIC/Slicer 4.11.0-2019-07-06/Mesher/MeshCreator/”, line 165, in run
import gmsh_api.gmsh as gmsh
File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\lib\Python\Lib\site-packages\gmsh_api\”, line 39, in
lib = CDLL(libpath)
File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\lib\Python\Lib\”, line 348, in init
self._handle = _dlopen(self._name, mode)
TypeError: LoadLibrary() argument 1 must be str, not None

Python wrapping of gmsh is quite messy. There are many python wrappers, they all work slightly differently and a few of those that I tried fail in various ways.

pygmsh is a quite simple one, which uses an installed gmsh executable and it seems to work for me. For example, this generates a mesh:

import pygmsh

geom = pygmsh.opencascade.Geometry(

rectangle = geom.add_rectangle([-1.0, -1.0, 0.0], 2.0, 2.0)
disk1 = geom.add_disk([-1.2, 0.0, 0.0], 0.5)
disk2 = geom.add_disk([+1.2, 0.0, 0.0], 0.5)
union = geom.boolean_union([rectangle, disk1, disk2])

disk3 = geom.add_disk([0.0, -0.9, 0.0], 0.5)
disk4 = geom.add_disk([0.0, +0.9, 0.0], 0.5)
flat = geom.boolean_difference([union], [disk3, disk4])

geom.extrude(flat, [0, 0, 0.3])

mesh = pygmsh.generate_mesh(geom, gmsh_path="c:/Users/andra/util/gmsh/gmsh.exe", msh_filename="c:/Users/andra/tmp/test.vtk", mesh_file_type="vtk")

Hi Andras,
Sorry for asking a question about pygmsh. What if you want to load a geometry from file and not create it using the opencascade. I searched much in the google but couldnt find anything on this? Could you guide me please. any related documentation which can help me.

Saima Safdar

Simplest is probably to write the model to file and run gmsh on that. Then read the output file to Slicer.

1 Like

Hi andras,
I am getting the error for installing
import pip

pip.main([‘install’, ‘numpy-stl’])

Traceback (most recent call last):

File “”, line 1, in

AttributeError: module ‘pip’ has no attribute ‘main’

what am i missing???

Saima Safdar

resolved I used this.

Didnt understand why main not worked.

pip has not been exposing main for a really long time (since pip 9.x; while current pip version is 19.x). You can use the current pip API or the slicer.util helper function that you have found.

By the way, you should not need numpy-stl for loading/saving models. You can use slicer.util.loadModel('something.stl') and slicer.util.saveNode('something2.stl') instead.

1 Like

Hi everyone,
I tried to install matplotlib in slicer 5.3.0 and 5.1.0, returns a lot error like this, too long to paste

import pip
pip.main([‘install’, ‘matplotlib’])
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see ImportError in system pip wrappers after an upgrade · Issue #5599 · pypa/pip · GitHub for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with ‘-m pip’ instead of running pip directly.
— Logging error —
Traceback (most recent call last):
File “”, line 60, in emit
KeyError: 15
Call stack:
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\”, line 258, in push
more = self.runsource(source, self.filename)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\”, line 74, in runsource
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\”, line 90, in runcode
exec(code, self.locals)
File “”, line 1, in
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\”, line 13, in main
return wrapper(args)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\utils\”, line 43, in wrapper
return main(args)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\cli\”, line 55, in main
cmd_name, cmd_args = parse_command(args)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\cli\”, line 79, in parse_command
general_options, args_else = parser.parse_args(args)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\”, line 1371, in parse_args
values = self.get_default_values()
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\cli\”, line 279, in get_default_values

packages\pip_internal\cli\", line 216, in main
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\cli\”, line 190, in handle_pip_version_check
pip_self_version_check(session, options)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\”, line 238, in pip_self_version_check
logger.warning(“There was an error checking the latest version of pip.”)
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 1458, in warning
self.log(WARNING, msg, args, **kwargs)
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 1589, in log
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 1599, in handle
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 1661, in callHandlers
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 952, in handle
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\utils\”, line 179, in emit
Message: ‘There was an error checking the latest version of pip.’
Arguments: ()

Please try using the slicer.util helper function, i.e.

1 Like

2 posts were split to a new topic: Cannot import gmsh Python package