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".
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?
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.
Does anyone know how to install packages through the python interactor? I tried to install pip, but keep getting errors.
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.
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.
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
I’ve tried this on the latest nightly version and it all works well for me!
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.
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:
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).
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'
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.
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.
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.
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’
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’])
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’