Custom environments
Adding Python Packages and Creating Custom Environments
Although many important packages are preinstalled into the images, you can install additional packages by yourself.
If you want to add python packages, you have two options:
1. Using Pip
You can install Python packages directly within a Jupyter notebook by running the following command in a code cell:
!pip install package_name
This installs the package_name in your current environment. Pip is useful when the package is not available in conda or for simpler installations.
2. Using Conda
Many default system directories are read-only, so you won’t be able to directly install packages into the base environment. Instead, you can create a new conda environment (see the section below for instructions) where you can install and manage your own packages.
Create new conda environment as a kernel in JupyterHub
Prerequisites
Before installing new packages, you need to initialize your shell for conda. This step ensures that conda commands work properly in your terminal. Open the terminal and execute the following commands:
conda init
bash
Installation
To create a new conda environment with Python 3.11 and the pandas package, follow these steps. The ipykernel package is also required to use this environment as a kernel in Jupyter.
- Open a terminal in JupyterHub.
- Create the environment by running:
conda create --name py311 python=3.11 ipykernel pandas -y
- Activate the environment:
conda activate py311
- Install the environment as a Jupyter kernel so you can use it in notebooks:
python -m ipykernel install --user --name py311 --display-name "Python (py311)"
Final Steps
After completing the above steps, reload the JupyterHub page. Your new environment should now be available as a selectable kernel when you open or create new notebooks.
Key Points to Note:
- Conda init is required only once to set up your shell for conda.
- You can replace py311, pandas, or the Python version with your desired environment name, packages, or Python version.
- The kernel name “Python (py311)” is the name that will appear in Jupyter when selecting the kernel. You can customize it to any name you prefer.
Running Commands and GUI Applications in the Terminal
You can also run any command or script by opening a terminal in JupyterHub. As a user, you have permissions to run files with your user privileges.
JupyterHub NRW also provides an X11 terminal (Konsole) that allows you to access GUI-based applications as well. This enables you to run programs that require a graphical interface, such as scientific software, visual tools, or data visualization applications.