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
pipmain(['install','scipy'])

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

pipmain([‘install’,‘pynrrd’])

Collecting pynrrd

Using cached https://files.pythonhosted.org/packages/75/db/d0b421a9cbf84ce2af6f95e433f472b6a49e27e9355c027c8680ca9163af/pynrrd-0.4.0-py2.py3-none-any.whl

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
slicer.util.pip_install(‘pynrrd’)
Collecting pynrrd
Using cached https://files.pythonhosted.org/packages/75/db/d0b421a9cbf84ce2af6f95e433f472b6a49e27e9355c027c8680ca9163af/pynrrd-0.4.0-py2.py3-none-any.whl
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\util.py”, line 2002, in pip_install
logProcessOutput(proc)
File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\bin\Python\slicer\util.py”, 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/.slicerrc.py]
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\util.py", line 2002, in pip_install
    logProcessOutput(proc)
  File "C:\Users\andra\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-15\bin\Python\slicer\util.py", 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.

3 Likes

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:

pip_install(“gmsh-api”)

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/MeshCreator.py”, 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\gmsh.py”, line 39, in
lib = CDLL(libpath)
File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\lib\Python\Lib\ctypes_init_.py”, 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(
  characteristic_length_min=0.1,
  characteristic_length_max=0.1,
  )

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.

Regards,
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???

Regards,
Saima Safdar

resolved I used this.
slicer.util.pip_install(‘numpy-stl’)

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\code.py”, line 258, in push
more = self.runsource(source, self.filename)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\code.py”, line 74, in runsource
self.runcode(code)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\code.py”, 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\pip_init_.py”, line 13, in main
return wrapper(args)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\utils\entrypoints.py”, line 43, in wrapper
return main(args)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\cli\main.py”, 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\main_parser.py”, 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\optparse.py”, 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\parser.py”, line 279, in get_default_values
self.config.load()


packages\pip_internal\cli\base_command.py", line 216, in main
self.handle_pip_version_check(options)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\cli\req_command.py”, 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\self_outdated_check.py”, 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
self.handle(record)
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 1599, in handle
self.callHandlers(record)
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 1661, in callHandlers
hdlr.handle(record)
File "D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\logging_init
.py", line 952, in handle
self.emit(record)
File “D:\Slicer 5.3.0-2022-12-16\lib\Python\Lib\site-packages\pip_internal\utils\logging.py”, line 179, in emit
self.handleError(record)
Message: ‘There was an error checking the latest version of pip.’
Arguments: ()
1

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

slicer.util.pip_install('matplotlib')
1 Like

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