Slicer-Python Packages Use and Install


#1

Hi,

There are python packages (e.g. scipy) that I would like to use but not installed under
"Slicer 4.6.2\lib\Python\Lib\site-packages".

  1. If I already have Python 2.7 and all packages I want installed on my computer, can I add path in Slicer so it knows where to find those packages?

  2. I tried to copy and paste all the installed packages into the “Slicer 4.6.2\lib\Python\Lib\site-packages” folder, but Slicer ended up not running at all.

  3. Does anyone know how to install packages through the python interactor? I tried to install pip, but keep getting errors.

Thanks,
Stephen


Loading a deep learning model to a scripted module
New 3D Slicer Module that Connects to a MySQL DB
(Jean Christophe Fillion Robin) #2

Since Slicer bundles its own python, it is generally NOT possible to simply copy python packages (especially the one including binaries) from your own site-packages into the one of Slicer.

That said, depending on the platform there is way around it.

First, I suggest you download the latest nightly build of Slicer.

There are a lot of improvement regarding the python infrastructure. It for example includes an updated setuptools as well as pip.

Second, on linux (and most likely macOS), it is indeed possible to pip install official packages (even the including binaries like scipy, tensorflow, …).

On windows, it will currently work only for pure python wheels because the official package for python 2.7 are built with a compiler than the one used for the official wheels. Note that this will change as soon as we standardize on Visual Studio 2015 and switch to python >= 3.5.

As mentioned above, the good news is that pip is available in Slicer.

build tree

From a build tree, you can do:

./Slicer --launch ../python-install/bin/pip install scipy

or something like this on windows (only for pure python wheels)

Slicer.exe --launch ..\python-install\bin\pip.exe install name-of-pure-python-wheel

install tree or python interactor

Since we do not (yet) package the pip executable, you will have to use a different approach.

from the terminal
./Slicer --launch ./bin/python-real -c "import pip; pip.main(['install', 'scipy'])"

or something like this on windows

Slicer.exe --launch bin\python-real.exe -c "import pip; pip.main(['install', 'scipy'])"
from python interactor
import pip
pip.main(['install', 'scipy'])

python-real different from python interpreter
Rpy2 pip installation fails
#4

Hi,

I downloaded and installed the 3D-Slicer nightly built on a window 64bit machine.

I tried to install the scipy whl using pip, but got a window error [Error 5] Access in denied: …Slicer 4.7.0\Lib\Python\Lib\site-packages\scipy

I also tried pip.main([‘install’,‘scipy’]), but got a failed building wheel for scipy feedback.
Downloading the scipy wheel, then install doesn’t work either.

Does anyone know how to fix that?

Please also see attached

Thanks,
Stephen


(Jean Christophe Fillion Robin) #5
  1. Even if we manage to install the wheel, there is a chance it doesn’t work at all. Scipy is a binary wheel, it includes compiled code.

  2. That said, since running pip install attempt to update packages located in C:\Program Files\Slicer ..., try to run Slicer as administrator.


#6

I am sorry. How do I run Slicer as an administrator? Thanks!


#7

I tried this
Slicer.exe --launch …\python-install\bin\pip.exe install name-of-pure-python-wheel
Seems like Slicer started to load, but then nothing happened.
Also, is the python-install/bin folder supposed to be inside the Slicer directory? I am not able to find it nor the pip.exe
Thanks so much!
Stephen


(Jean Christophe Fillion Robin) #8

To run as admin, you need to do right click -> run as admin

Other since you installed Slicer, there is no pip exe.


(Andras Lasso) #9

I’ve tried this on the latest nightly version and it all works well for me!

  1. Start Slicer as administrator: in the start menu, instead of left-click on the Slicer icon, use right-click, then select More... and Run as Administrator.

  2. Install a package by using pip. For example, installing of requests package:

Enter this into the Python interactor:

import pip
pip.main(['install', 'requests'])

The package is successfully downloaded and installed:

Collecting requests
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting idna<2.7,>=2.5 (from requests)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
Installing collected packages: idna, urllib3, certifi, chardet, requests
  Found existing installation: chardet 2.3.0
    Uninstalling chardet-2.3.0:
      Successfully uninstalled chardet-2.3.0
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
0

Dependent packages are updated, therefore before using the installed requests package, you need to restart Slicer (you can use the restart() command in the Python console).


Requests: HTTP for Humans
Error when installing scikit-image via Python Interactor
(Jean Christophe Fillion Robin) #10

Exactly. For pure python packages, that will work great.


#12

Hi,

Thank you very much for all the help!

While the installation worked well for some packages (e.g. PIL and matplotlib wheels), it still doesn’t work for “scipy”, “sckimage”, etc. Does anyone know what happened? Thank you!

  • I did uninstall numpy and installed numpy+mkl since scipy complained numpy didn’t have mkl.

In particular, I get the following error,

path=r"C:\Program Files\Slicer 4.7.0-2017-09-05\packages\scipy-0.19.1-cp27-cp27m-win_amd64.whl"
>>> pip.main(['install',path])
Processing c:\program files\slicer 4.7.0-2017-09-05\packages\scipy-0.19.1-cp27-cp27m-win_amd64.whl
Requirement already satisfied: numpy>=1.8.2 in c:\program files\slicer 4.7.0-2017-09-05\lib\python\lib\site-packages\numpy-1.13.1-py2.7-win-amd64.egg (from scipy==0.19.1)
Exception:
Traceback (most recent call last):
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\site-packages\pip\basecommand_.py", line 215, in main
status = self.run(options, args)
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\site-packages\pip\commands\install_.py", line 335, in run
wb.build(autobuilding=True)
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\site-packages\pip\wheel_.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\site-packages\pip\req\req_set.py", line 666, in _prepare_file
check_dist_requires_python(dist)
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\site-packages\pip\utils\packaging_.py", line 48, in check_dist_requires_python
feed_parser.feed(metadata)
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\email\feedparser_.py", line 177, in feed
self._input.push(data)
File "C:\Program Files\Slicer 4.7.0-2017-09-05\lib\Python\Lib\email\feedparser_.py", line 99, in push
parts = data.splitlines(True)
AttributeError: 'NoneType' object has no attribute 'splitlines'

#13

I am trying to install scikit-image with

./Slicer --launch ./bin/python-real -c “import pip; pip.main([‘install’, ‘scikit-image’])”

In the install tree I get the error

/usr/bin/gcc-4.6 -pthread -Wall -Wstrict-prototypes -fno-strict-aliasing -fwrapv -g -fPIC -I/Data-work/BuildDirs/Slicer-4.7.0-2017-09-24-linux-amd64/lib/Python/include/python2.7 -c _posixsubprocess.c -o build/temp.linux-x86_64-2.7/_posixsubprocess.o
_posixsubprocess.c:3:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command '/usr/bin/gcc-4.6' failed with exit status 1

(Andras Lasso) #14

Slicer’s pip does not yet support packages that need compilation. To get access to all Python packages, you have to build Slicer with your Python distribution. See some information here: https://www.slicer.org/wiki/Documentation/Labs/SlicerCondaIntegration


(Andreas Kleinöder) #15

Hi all,

a quick questions concerning this topic. I am developing with Slicer on Windows and was also trying to install a module with precompiled data, namely pywin32. After some try and error I figured out that pywin32 also uses precompiled data. Since Slicers Python is build with VS2013 but the module has precompiled with VS2008 I assume the runtimes collide here.
My plan was now to build Slicer on my own such that I also have a Python.exe build with VS2013. Afterwards I would go to pywin32 gitlab page and build the module manually as well. Building pywin32 requires the Python.exe file, thats why I need to build Slicer as well.

Could this work?

Thanks!


(Andras Lasso) #16

If you build Slicer according to build instructions and you build the Python package using the same compiler and compiler settings then it should all work. If you need a Python.exe, you can use Slicer’s SlicerPython.exe.

Note that Slicer includes Qt library, which provides access to lots of Windows APIs (and Linux and macOS APIs), so you might be able to do without pywin32.


(Andreas Kleinöder) #17

Thanks! I think you are right and I should use the Qt library. Haven’t thought about this. Anyways it’s good to know that it should work like this.


(Isaiah Norton) #18

If the API is not available in Qt, or you need code to work outside the Slicer environment too, then you could also try pywincffi. That uses the CFFI module, which can construct calls at runtime via ctypes/libffi (in CPython), and avoid the need for dealing with compiled shim libraries.


(saima safdar) #19

WHy pip.main not working for me. I run as administrator even then its not working for me
Python 2.7.13 (default, Aug 31 2018, 23:24:30) [MSC v.1900 64 bit (AMD64)] on win32
AttributeError: ‘module’ object has no attribute ‘main’


(James Butler) #20

Details can be found Pip in nightly build not working


(saima safdar) #21

how did you install matplotlib and other packages you mentioned.
For me scipy is installed like this.
from pip._internal import main as pipmain
pipmain([‘install’,‘scipy’])

Collecting scipy

Downloading https://files.pythonhosted.org/packages/d2/a7/0d698589a3c6c44f81078a52518c8e64c4ed579a862105b2bff5a1f14ff4/scipy-1.1.0-cp27-none-win_amd64.whl (31.5MB)

Requirement already satisfied: numpy>=1.8.2 in d:\slicer 4.9.0-2018-08-31\lib\python\lib\site-packages\numpy-1.13.1-py2.7-win-amd64.egg (from scipy) (1.13.1)

Installing collected packages: scipy

Successfully installed scipy-1.1.0
>>> pipmain([‘install’,‘scipy’])

Collecting scipy

Downloading https://files.pythonhosted.org/packages/d2/a7/0d698589a3c6c44f81078a52518c8e64c4ed579a862105b2bff5a1f14ff4/scipy-1.1.0-cp27-none-win_amd64.whl (31.5MB)

Requirement already satisfied: numpy>=1.8.2 in d:\slicer 4.9.0-2018-08-31\lib\python\lib\site-packages\numpy-1.13.1-py2.7-win-amd64.egg (from scipy) (1.13.1)

Installing collected packages: scipy

Successfully installed scipy-1.1.0

>>> pipmain([‘install’,‘scipy’])

Collecting scipy

Downloading https://files.pythonhosted.org/packages/d2/a7/0d698589a3c6c44f81078a52518c8e64c4ed579a862105b2bff5a1f14ff4/scipy-1.1.0-cp27-none-win_amd64.whl (31.5MB)

Requirement already satisfied: numpy>=1.8.2 in d:\slicer 4.9.0-2018-08-31\lib\python\lib\site-packages\numpy-1.13.1-py2.7-win-amd64.egg (from scipy) (1.13.1)

Installing collected packages: scipy

Successfully installed scipy-1.1.0


(saima safdar) #22

Why I cant install this package

pipmain([‘install’,‘scikit-fuzzy’])

Collecting scikit-fuzzy

Using cached https://files.pythonhosted.org/packages/fb/79/71a79d2663ed662d30461aca261baff4fc87ddfd23f3e9baeaee86917f6b/scikit-fuzzy-0.3.1.tar.gz

Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: ‘c:\users\22374464\appdata\local\temp\pip-req-tracker-v9vnky\6fa5e984c9f7c4180f5cbc9dc5bc4378cf0a090dcb740963746419f9’


Rpy2 pip installation fails