Packages

Packages contain code, data, and documentation in a standardized collection format that you can install to perform specific tasks or operations within your code.

Python Packages

Python is a general-purpose programming language. Install packages to perform specific tasks or operations within your code. For example, the Python package NumPy, or numpy in commands, can be used to perform operations on arrays. There are multiple methods you can use to install packages in Jupyter Notebooks.

How to Install Packages

Note

After installing packages in a notebook or console, restart the kernel to ensure the changes take effect. To restart the kernel, click on the Kernel menu and select Restart Kernel….

pip

Install packages directly within a Jupyter Notebook by running the pip install command in a code cell. To install the package numpy:

pip install --user numpy

conda

If you are using the Anaconda distribution of Python, you can use conda install to install packages. Conda is a package manager that simplifies the installation and management of packages and environments. To install the package numpy:

conda install numpy

Magic Commands

Magic commands are built-in Jupyter/IPython commands, you can learn more about magic commands in the IPython documentation. Use the %pip and %conda magic commands in a code cell to install packages. To install the package numpy with %pip or %conda:

%pip install --user numpy
%conda install numpy

Requirements File

If you have a requirements.txt file that lists all the packages you want to install, you can use the %pip magic command with the -r flag to install all the packages at once.

%pip install -r requirements.txt --user

Terminal

JupyterLab provides a terminal within the interface. Open a terminal in the File menu by selecting New and then Terminal. From the terminal, you can use pip install or conda install commands to install packages just like you would in a regular command prompt or terminal.

How to Install Custom Packages

Note

Before installing a custom package, ensure that you have access to the package source. Packages can be obtained from various sources such as Python Package Index (PyPI), GitHub repositories, or local files.

Ensure that any custom packages you install are compatible with your Python version and dependencies to avoid any conflicts or compatibility issues.

Follow these steps to install custom packages in Jupyter Notebooks:

  1. Install a custom package using one of the three methods below:

    • pip: The most common method for installing custom packages is using pip, the package installer for Python. In a Jupyter Notebook code cell, you can run the following command to install a custom package, such as numpy, from PyPI:

      pip install --user numpy
      
    • GitHub: If the package is hosted on a GitHub repository, you can use pip to install it directly from the repository. In the code cell, run the following command, replacing https://github.com/author/package.git@branch with the actual GitHub repository URL:

      pip install --user git+https://github.com/author/package.git@branch
      
    • local files: If you have the package stored locally, you can install it using pip by specifying the path to the package file. In the code cell, run the following command, replacing /path/to/package.whl with the actual path to the package file:

      pip install --user /path/to/package.whl
      
  2. Click on the Kernel menu and select Restart Kernel…, to ensure the changes take effect.

  3. Import the package into your Jupyter Notebook code cells using the standard Python import statement. The below example imports numpy:

    import numpy as np
    

List of Installed Packages

To view the list of installed packages, run the pip list command in a notebook, console, or terminal.

pip list Example (click to expand/collapse)
lhelms2@jupyter-lhelms2:~$ pip list
Package                       Version
----------------------------- ------------
alembic                       1.12.0
anyio                         4.0.0
argon2-cffi                   23.1.0
argon2-cffi-bindings          21.2.0
arrow                         1.3.0
asttokens                     2.4.0
async-generator               1.10
async-lru                     2.0.4
attrs                         23.1.0
Babel                         2.12.1
backcall                      0.2.0
backports.functools-lru-cache 1.6.5
beautifulsoup4                4.12.2
bleach                        6.0.0
blinker                       1.6.2
boltons                       23.0.0
Brotli                        1.1.0
cached-property               1.5.2
certifi                       2023.7.22
certipy                       0.1.3
cffi                          1.16.0
charset-normalizer            3.2.0
colorama                      0.4.6
comm                          0.1.4
conda                         23.7.4
conda-package-handling        2.2.0
conda_package_streaming       0.9.0
cryptography                  41.0.4
debugpy                       1.8.0
decorator                     5.1.1
defusedxml                    0.7.1
entrypoints                   0.4
exceptiongroup                1.1.3
executing                     1.2.0
fastjsonschema                2.18.1
fqdn                          1.5.1
greenlet                      2.0.2
idna                          3.4
importlib-metadata            6.8.0
importlib-resources           6.1.0
ipykernel                     6.25.2
ipython                       8.16.0
ipython-genutils              0.2.0
isoduration                   20.11.0
jedi                          0.19.0
Jinja2                        3.1.2
json5                         0.9.14
jsonpatch                     1.33
jsonpointer                   2.4
jsonschema                    4.19.1
jsonschema-specifications     2023.7.1
jupyter_client                8.3.1
jupyter_core                  5.3.2
jupyter-events                0.7.0
jupyter-lsp                   2.2.0
jupyter_server                2.7.3
jupyter_server_terminals      0.4.4
jupyter-telemetry             0.1.0
jupyterhub                    4.0.2
jupyterlab                    4.0.6
jupyterlab-pygments           0.2.2
jupyterlab_server             2.25.0
libmambapy                    1.5.1
Mako                          1.2.4
mamba                         1.5.1
MarkupSafe                    2.1.3
matplotlib-inline             0.1.6
mistune                       3.0.1
nbclassic                     1.0.0
nbclient                      0.8.0
nbconvert                     7.8.0
nbformat                      5.9.2
nest-asyncio                  1.5.6
notebook                      7.0.4
notebook_shim                 0.2.3
oauthlib                      3.2.2
overrides                     7.4.0
packaging                     23.2
pamela                        1.1.0
pandocfilters                 1.5.0
parso                         0.8.3
pexpect                       4.8.0
pickleshare                   0.7.5
pip                           23.2.1
pkgutil_resolve_name          1.3.10
platformdirs                  3.10.0
pluggy                        1.3.0
prometheus-client             0.17.1
prompt-toolkit                3.0.39
psutil                        5.9.5
ptyprocess                    0.7.0
pure-eval                     0.2.2
pycosat                       0.6.4
pycparser                     2.21
pycurl                        7.45.1
Pygments                      2.16.1
PyJWT                         2.8.0
pyOpenSSL                     23.2.0
PySocks                       1.7.1
python-dateutil               2.8.2
python-json-logger            2.0.7
pytz                          2023.3.post1
PyYAML                        6.0.1
pyzmq                         25.1.1
referencing                   0.30.2
requests                      2.31.0
rfc3339-validator             0.1.4
rfc3986-validator             0.1.1
rpds-py                       0.10.3
ruamel.yaml                   0.17.33
ruamel.yaml.clib              0.2.7
Send2Trash                    1.8.2
setuptools                    68.2.2
six                           1.16.0
sniffio                       1.3.0
soupsieve                     2.5
SQLAlchemy                    2.0.21
stack-data                    0.6.2
terminado                     0.17.1
tinycss2                      1.2.1
tomli                         2.0.1
toolz                         0.12.0
tornado                       6.3.3
tqdm                          4.66.1
traitlets                     5.10.1
types-python-dateutil         2.8.19.14
typing_extensions             4.8.0
typing-utils                  0.1.0
uri-template                  1.3.0
urllib3                       2.0.5
wcwidth                       0.2.7
webcolors                     1.13
webencodings                  0.5.1
websocket-client              1.6.3
wheel                         0.41.2
zipp                          3.17.0
zstandard                     0.21.0

R Packages

R is a programming language and software environment for statistical computing and graphics. R and its libraries implement a wide variety of statistical and graphical techniques, such as linear and non-linear modeling, classical statistical tests, time-series analysis, classification, and clustering.

R is easily extensible through functions and extensions. The R community is noted for its active contributions to developing R packages. R packages contain code, data, and documentation in a standardized collection format that R users can install. R and R packages are available via the Comprehensive R Archive Network (CRAN), a collection of sites that carry the R distribution(s), the contributed extensions, documentation for R, and binaries.

Available Packages

A list of available packages in CRAN can be found in the CRAN package repository. From the CRAN package repository, you can click on a package and learn more about it and access its documentation.

You can also view a list of available packages from your JupyterLab environment. To view the list of available packages in CRAN, run the available.packages() command in a notebook or console. If you want to view a subset of the list, you can append [first-row-number:last-row-number,] to the command. For example, to view the first 100 available packages, the command is:

available.packages()[1:100,]

How to Install Packages from CRAN

To install an available package from CRAN, use the install.packages('PACKAGE_NAME') command, replacing PACKAGE_NAME with the name of the package you want to install. For example, to install the package abc, the command is:

install.packages('abc')

How to Install Packages from GitHub

To install a package from GitHub that is not available in CRAN, you can use the CRAN githubinstall package. See the githubinstall reference manual for more details on how to use this package.

Installed Packages

To check if a single package is installed, use the below code, replacing PACKAGE_NAME with the name of the package you want to check. If the package is installed, the code will return TRUE; if it is not installed, it will return FALSE.

a<-installed.packages()
packages<-a[,1]
is.element('PACKAGE_NAME', packages)

Reference: UCLA OARC Stats

To view a list of installed packages, run the installed.packages() command in a notebook or console. If you want to view a subset of the list, you can append [first-row-number:last-row-number,], to the command. For example, to view the first 100 installed packages, the command is:

installed.packages()[1:100,]