Slicer-Python Packages Use and Install

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 https://github.com/pypa/pip/issues/5599 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