How to install python packages using jupyterlab?

Hi all,I am using the Slicer 4.11 and I am stucked with installing packages.
Here is the thing in detail:

I was trying to install some packages,say matplotlib in Slicer:
image

Also,I tried to install the package in Python Interactor:


Is there a way to manage the package installation quickly and easily?
By the way I was using Anaconda to launch the jupyter and select the Slicer as a kernel.
Thank you for any possible advice in advance.

Normal install in jupyterlab

import sys
import subprocess

def installPackage(package):
    p = subprocess.run([sys.executable, "-m", "pip", "install", "-U", package], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print(p.stdout.decode())

requirements = ["numpy", "scipy", "matplotlib", "chart_studio", "scikit-learn", "psutil", "openpyxl", "pandas", "pydicom","lmfit","numericalunits", "sqlalchemy"]
for requirement in requirements:
    installPackage(requirement)

Using Slicer kernel

from slicer.util import pip_install as installPackage

requirements = ["numpy", "scipy", "matplotlib", "chart_studio", "scikit-learn", "psutil", "openpyxl", "pandas", "pydicom","lmfit","numericalunits", "sqlalchemy"]
for requirement in requirements:
    installPackage(requirement)
1 Like

Thank you Alex,
This really works,so if I want to install other packages just add the package name in the requirements right?

Normally yes, but you may also try installPackage(requirement, "-U") if something fails

Okay,thank you Alex.