# Slicer-Python Packages Use and Install

**URL:** <https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984>\
**Category:** Support\
**Created:** [August 31, 2017, 9:27pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984 "2017-08-31T21:27:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssyip](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@ssyip](https://discourse.slicer.org/u/ssyip)\
**Post date:** [August 31, 2017, 9:27pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/1 "2017-08-31T21:27:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [August 31, 2017, 9:46pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/2 "2017-08-31T21:46:06Z")

</div>

> [@ssyip](#):
>
> 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.

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.

> [@ssyip](#):
>
> 1. Does anyone know how to install packages through the python interactor? I tried to install pip, but keep getting errors.

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

#### build tree

From a build tree, you can do:

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

```

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

```bash
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

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

```

or something like this on windows

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

```

##### from python interactor

```python
import pip
pip.main(['install', 'scipy'])

```

---

<div class="post-metadata">

**Author:** ![ssyip](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@ssyip](https://discourse.slicer.org/u/ssyip)\
**Post date:** [September 1, 2017, 4:13pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/4 "2017-09-01T16:13:31Z")

</div>

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

 ![](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/1X/897958c561d0fb9ffd5b892c4ea5c0c33e67ef3a.JPG)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [September 1, 2017, 4:21pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/5 "2017-09-01T16:21:08Z")

</div>

> [@ssyip](#):
>
> 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
> 
> Does anyone know how to fix that?

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.

---

<div class="post-metadata">

**Author:** ![ssyip](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@ssyip](https://discourse.slicer.org/u/ssyip)\
**Post date:** [September 1, 2017, 4:38pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/6 "2017-09-01T16:38:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssyip](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@ssyip](https://discourse.slicer.org/u/ssyip)\
**Post date:** [September 1, 2017, 4:42pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/7 "2017-09-01T16:42:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [September 1, 2017, 5:00pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/8 "2017-09-01T17:00:58Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 1, 2017, 10:49pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/9 "2017-09-01T22:49:15Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [September 1, 2017, 11:05pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/10 "2017-09-01T23:05:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssyip](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@ssyip](https://discourse.slicer.org/u/ssyip)\
**Post date:** [September 5, 2017, 6:22pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/12 "2017-09-05T18:22:28Z")

</div>

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,

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![codey](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@codey](https://discourse.slicer.org/u/codey)\
**Post date:** [September 26, 2017, 11:38pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/13 "2017-09-26T23:38:14Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 27, 2017, 12:29am UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/14 "2017-09-27T00:29:24Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Labs/SlicerCondaIntegration)

---

<div class="post-metadata">

**Author:** ![mcfly3001](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mcfly3001/32/6437_2.png) [@mcfly3001](https://discourse.slicer.org/u/mcfly3001)\
**Post date:** [July 12, 2018, 9:52am UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/15 "2018-07-12T09:52:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 12, 2018, 12:47pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/16 "2018-07-12T12:47:50Z")

</div>

If you build Slicer according to [build instructions](https://www.slicer.org/wiki/Documentation/Nightly/Developers/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.

---

<div class="post-metadata">

**Author:** ![mcfly3001](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mcfly3001/32/6437_2.png) [@mcfly3001](https://discourse.slicer.org/u/mcfly3001)\
**Post date:** [July 12, 2018, 12:58pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/17 "2018-07-12T12:58:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [July 12, 2018, 3:41pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/18 "2018-07-12T15:41:39Z")

</div>

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](http://pywincffi.readthedocs.io/en/0.4.0/). 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.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 5, 2018, 9:29am UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/19 "2018-09-05T09:29:48Z")

</div>

> [@jcfr](#):
>
> stall tree or python interactor

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’

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [September 5, 2018, 12:15pm UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/20 "2018-09-05T12:15:02Z")

</div>

Details can be found [Pip in nightly build not working](https://discourse.slicer.org/t/pip-in-nightly-build-not-working/3325/5)

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 6, 2018, 3:23am UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/21 "2018-09-06T03:23:18Z")

</div>

> [@ssyip](#):
>
> ell for some packages (e.g. PIL and matplotlib wheel

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](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](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](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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 6, 2018, 3:40am UTC](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984/22 "2018-09-06T03:40:48Z")

</div>

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](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’

[Next page](https://discourse.slicer.org/t/slicer-python-packages-use-and-install/984.md?page=2)
