Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Then, we add to the script hook:

module purge
source /home/lcurs000${TEACHER_DIR}/JHL_installations/venvs/my_env/bin/activate
python -m ipykernel install --user --name=my_env

IMPORTANT: we purge all modules here, because otherwise the ipykernel install will pick up on the global installation of ipykernel and fail to install the local kernel spec. Since purging temporarily changes the environment, we advise to put this section of code all the way at the bottom of the script hook, so that other parts of the script hook are not affected by the purge.

...

If we start the 'my_env' kernel, we will be able to import the 'values' Python package.

Using Conda virtual environments

You can use Conda virtual environments through Jupyter Notebooks, but a couple of steps are needed.

  • Create a Conda virtual environment in the JHL_installations directory
  • Install the kernel you would like to use (e.g. 'ipykernel' for a Python kernel) in the virtual environment
  • Install any packages in the virtual environment (as you normally would)
  • Set Unix permissions so that files/folders within the virtual environment are group readable
  • Create a hook that installs the ipython kernel for each student, so that your Jupyter Notebook server will find it

After these steps, you will see the Conda virtual environment as an additional item if you go the the 'New' menu in a Jupyter Notebook server.

Creating a Conda virtual environment in the JHL_installations directory

To create a Conda virtual environment called 'my_env' in the JHL_installations directory, open a terminal from the Jupyter Notebook environment (New → Terminal) and run:

conda create --prefix ${TEACHER_DIR}/JHL_installations/conda/envs/my_env

(N.B. if the conda command is not available, contact us via the servicedesk to help you. We are working on making this available in the default environment)

NOTE: If you get the error "NotWritableError: The current user does not have write permissions to a required path.", simply run it again once or twice. This is a known bug in conda.

Installing packages in the Conda virtual environment

The first time, you may first need to run

conda init bash

(only needed if your prompt doesn't show '(base)' before your user name). If you had to run this command, close your terminal, and open a new terminal from the Jupyter Notebook environment. You should now see the '(base)' before your username.

Then, activate the conda virtual environment with the full path and install the ipykernel package, and any additional conda packages you want to install. Finally, in the last command, we make sure that the permissions are set correctly so that all students can read these files as well.

conda activate ${TEACHER_DIR}/JHL_installations/conda/envs/my_env
conda install ipykernel
conda install ...
find ${TEACHER_DIR}/JHL_installations/conda/envs/my_env -not -perm -g=rX -exec chmod g+rX {} \;

Create hook that installs the conda environment for each student

Finally, we add the following to the script hook the following section of code:

# If kernel is not installed yet, install kernel for this user
if [ ! -f .local/share/jupyter/kernels/my_env/kernel.json ]; then
# Make sure conda environment is initialized
conda init bash
source .bashrc
# Clean the module environment, so that ipykernel install picks up the ipython from the conda environment
module purge
# Activate the conda environment
conda activate ${TEACHER_DIR}/JHL_installations/conda/envs/my_env
# Install the kernel for this user, with the name 'my_env'
python -m ipykernel install --user --name=my_env
# Insert a "-E" argument in the startup
# Makes sure the kernel only uses Python packages from the conda environment, not from the module environment
sed -i '/"-m",/i \ \ "-E",' ~/.local/share/jupyter/kernels/my_env/kernel.json
fi