# Automatic install of Python packages

**URL:** <https://discourse.slicer.org/t/automatic-install-of-python-packages/7078>\
**Category:** Development\
**Created:** [June 7, 2019, 11:33pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078 "2019-06-07T23:33:23Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [June 7, 2019, 11:33pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/1 "2019-06-07T23:33:23Z")

</div>

Continuing the discussion from [Python3 switch and python package install](https://discourse.slicer.org/t/python3-switch-and-python-package-install/6534/2):

Are there any updates on the automatic installation of Python packages required by extensions? The packages we’d like to use can be installed manually, but the user needs admin privileges.

@lassoan and @jcfr, do you know if automatic installation is likely to be available sometime soon? Thanks!

---

<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:** [June 8, 2019, 12:50am UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/2 "2019-06-08T00:50:04Z")

</div>

> do you know if automatic installation is likely to be available sometime soon?

To put things in context, are these packages you would like to have available after the user install [SlicerMorph](https://github.com/SlicerMorph/SlicerMorph/) extension ?

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [June 8, 2019, 4:31am UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/3 "2019-06-08T04:31:51Z")

</div>

Yes, thanks. For example, we plan to provide sample scripts to manipulate our module’s data output and would like to use the Pandas package in the scripts. It would be very convenient if we could have this package automatically downloaded with our extension.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 8, 2019, 7:46pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/4 "2019-06-08T19:46:16Z")

</div>

On mac (and probably linux) this works:

```auto
>>> import os
>>> os.system('PythonSlicer -m pip install pandas')
0
>>> import pandas

```

On windows, this process pops up the access control permission dialog:

```auto
import os
os.system('powershell.exe Start-Process cmd.exe -Verb runAs')

```

And then in the shell I could run this:

```auto
"c:\Program Files\Slicer 4.11.0-2019-06-02\bin\PythonSlicer.exe" -m pip install pandas

```

We should be able to streamline this and integrate it with the extension install process.

We discussed this on the developer call the other day and were thinking we should start collecting this information along with guidelines about which python packages should work and which might cause trouble.

---

<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:** [June 8, 2019, 7:54pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/5 "2019-06-08T19:54:40Z")

</div>

We plan to add a `slicer.util.pip` command that launches pip as admin (displays UAC popup) on Windows.

We could also add some higher-level helper function that would ask user confirmation in a popup before installing anything, explain what version of what package will be installed, and could display errors, etc.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [June 9, 2019, 7:41am UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/6 "2019-06-09T07:41:48Z")

</div>

So both of these answers assume user have admin rights?

---

<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:** [June 9, 2019, 12:20pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/7 "2019-06-09T12:20:54Z")

</div>

If Slicer is installed to a folder that requires admin rights then yes, currently you need admin rights to install Python packages. If this is a problem then there are many way to address this.

We can change Slicer install location to be in the user’s folder (e.g., AppData) instead of the shared “Program Files” folder. We could also allow users to choose the folder. This should be very easy (modification of a few lines).

We could try to make pip manage packages in two folders (core packages pre-installed with Slicer in “Program Files” and additional packages installed by extensions in user’s folder). We would do something unusual, so it may be hard to implement and maintain this solution.

We could install all extensions to the user’s folder. Probably we would need to copy core extensions from the Slicer core folder in Program Files on or before starting Slicer the first time. If would not be a very simple mechanism, but it could allow sharing a single Slicer installation to be used by multiple Python environments and it would be possible to reset the default environment (because it is stored in a read-only location).

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [June 10, 2019, 1:39am UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/8 "2019-06-10T01:39:46Z")

</div>

I think modifying the windows installer would be a good place to start. I just tried installing with a normal user account, and once UAC pop ups, it asks me for an admin account password. Hitting the cancel button, cancels the whole installation process. Instead, the behavior should be asking whether they want to install to their user space or somewhere else where admin rights is not required.

The whole getting the updates through new preview installs is really challenging to coordinate in corporate networks…

---

<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:** [June 10, 2019, 1:29pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/9 "2019-06-10T13:29:14Z")

</div>

It should be possible to ask if Slicer should be installed for all users or the current user.

If we put some work in the installer, it may be worth revisiting the use of NSIS and instead look at WIX.

> [@lassoan](#):
>
> two folders (core packages pre-installed with Slicer in “Program Files” and additional packages installed by extensions in user’s folder). We would do something unusual, so it may be hard to implement and maintain this solution.

This makes me think of what is done with virtualenv. That said, you are right it may be more cumbersome to implement

> [@lassoan](#):
>
> We could install all extensions to the user’s folder.

On windows and Linux, this is already the case. Extensions are installed in the user folder.

---

<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:** [June 11, 2019, 1:15pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/10 "2019-06-11T13:15:20Z")

</div>

I’ve submitted a [pull request](https://github.com/Slicer/Slicer/pull/1151/files) to change the default install location to AppData (c:\Users\<username\>\AppData\Local) and turned off requirement for an admin account.

Installing in user-specific folder may be a good thing, because then installing random Python packages would not impact other users.

The only drawback I can think of is that after this change it will be a bit more work to create an installation tree shared between all users on that computer. However, it is not that difficult to do: you need to manually select All Users\AppData as installation folder and move the start menu shortcuts to All Users\Start menu. We can just add these instructions to the documentation.

---

<div class="post-metadata">

**Author:** ![Sam\_Horvath](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sam_horvath/32/3092_2.png) [@Sam\_Horvath](https://discourse.slicer.org/u/Sam_Horvath)\
**Post date:** [June 11, 2019, 1:35pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/11 "2019-06-11T13:35:50Z")

</div>

Also, as an FYI, we are planning to work on the slicer.util.pip functionality and the automatic install of packages for extensions during the upcoming project week.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [June 11, 2019, 2:50pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/12 "2019-06-11T14:50:28Z")

</div>

Would the same slicer installation be usable by other other user accounts if the default it c:\Users\<username\>? I thought those folders are specific to the user, unless explicitly allowed.

Perhaps you can let user manually choose a folder that they can write to (e.g., c:\Slicer-4.11) which then can be shared by other users of the same computer?

---

<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:** [June 11, 2019, 3:07pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/13 "2019-06-11T15:07:43Z")

</div>

Visual Studio Code has a User Installer and a System Installer for [download](https://code.visualstudio.com/download). I know this program does this to allow easier frequent updates using the user install.

> VS Code provides both Windows user and system level setups. Installing the user setup does not require Administrator privileges as the location will be under your user Local AppData (LOCALAPPDATA) folder. User setup also provides a smoother background update experience.
> 
> The system setup requires elevation to Administrator privileges and will place the installation under Program Files.

[source](https://code.visualstudio.com/docs/setup/windows#_user-versus-system-setup)

---

<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:** [June 11, 2019, 3:53pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/14 "2019-06-11T15:53:13Z")

</div>

> [@muratmaga](#):
>
> Would the same slicer installation be usable by other other user accounts

Yes, everything is still doable (see my post [above](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/10)). However, we need to choose between which of these use cases we want to support by default:

- A. User (does not need to be administrator) installs Slicer and any extensions.
- B. Knowledgeable administrator sets up Slicer and all necessary extensions for all users on a shared computer.

Since use case A is much more common, I think we should make that easy for users, and let experts deal with the few required extra steps needed for a shared installation.

We could create two installers as @jamesobutler suggested above, but I am not sure if it is worth the extra effort (it would impact packaging and release download infrastructure, lead to more questions from users, etc.).

---

<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:** [June 12, 2019, 2:01pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/15 "2019-06-12T14:01:50Z")

</div>

I’ve integrated the [change](https://github.com/Slicer/Slicer/commit/25eea0d87aa41bf09097514262c2c1e450aae05a), from tomorrow, Slicer-4.11 install will note require admin rights on Windows and Python packages can be installed as regular user.

Slicer install tree can be accessed as `%LOCALAPPDATA%\NA-MIC` (usually it resolves to something like `C:\Users\<username>\AppData\Local\NA-MIC`).

Previous behavior (install to Program Files, require admin privileges) can be restored by adjusting `Slicer_CPACK_NSIS_INSTALL_ROOT` and `Slicer_CPACK_NSIS_INSTALL_REQUIRES_ADMIN_ACCOUNT` variables when configuring Slicer build).

---

<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:** [June 12, 2019, 10:26pm UTC](https://discourse.slicer.org/t/automatic-install-of-python-packages/7078/16 "2019-06-12T22:26:39Z")

</div>

I went ahead and [added a slicer.util.pip\_install function](https://github.com/Slicer/Slicer/commit/b26a7eefc2813d04bd1005be6dd6ccc41984154a) because @ungi will give a Slicer tutorial at CARS conference next week where he needs to walk through participants installing Python packages, and typing `pip_install("tensorflow")` in Slicer is much easier than locating Slicer’s Python, start a command terminal there, and type `PythonSlicer.exe -m pip install tensorflow`. We can refine the syntax and add features later.
