Using Multiple Python Versions and Environments with Posit Workbench and Jupyter Notebooks

Workbench

Posit Workbench includes the ability to use Jupyter Notebooks and JupyterLab.

In addition to the Python version and environment that is used in the Posit Workbench configuration, you can configure additional Python versions and environments using the following steps.

Requirements

  • Posit Workbench version 2021.09.4 or higher:
    • configured with Jupyter and
    • at least one version of Python
  • Determine if you want to add a new Python version or environment for all users:
    • Method 1: requires administrator access, or
    • Method 2: is only for your user

Method 1 - Install additional Python version for all users

For Administrators

Administrators can use this method to install an additional version of Python that is available globally to all users.

Step 1. Install additional Python version

  • Install an additional version of Python on the server following the steps to Install Python.

    Note

    Our recommended installation instructions for Python allow you to make multiple versions of Python available and avoid replacing existing versions of Python when updating system packages.

Step 2. Install the ipykernel package

  • From the previous step, you should still have the PYTHON_VERSION environment variable defined with the version of Python that you installed. If not, you can define this environment variable before proceed by running the following command and replacing 3.11.5 with the new version of Python that you installed:

    Terminal
    $ export PYTHON_VERSION=3.11.5
  • Install the ipykernel Python package in the root of your newly installed Python environment using the following command:

    Terminal
    $ sudo /opt/python/${PYTHON_VERSION}/bin/pip install ipykernel

Step 3. Register additional Python version as a new kernel

  • Register the additional version of Python as a new kernel using the following command:

    Terminal
    $ sudo /opt/python/${PYTHON_VERSION}/bin/python -m ipykernel install --name py${PYTHON_VERSION} --display-name "Python ${PYTHON_VERSION}"

Step 4. Verify the new kernel in Jupyter

  • From your browser, navigate to the Workbench interface and log in.
  • Select New Session and do the following:
    1. Give your session a name.
    2. In the Editor field, select either as the IDE:
      • Jupyter Notebooks or
      • JupyterLab
    3. Click Start Session.

  • From the menu bar, click Kernel > Change Kernel and select the newly installed kernel.

    Install additional versions of Python

    Repeat the steps in this section to install additional Python versions and environments that will be available to all users.

Method 2 - Register an additional Python environment only for your user

For users

Users can use this method to register an additional Python environment that is available only to their user.

Step 1. Create virtual environment

  • Open a Terminal in Jupyter Notebook or Jupyter Lab and create a virtual environment.
  • Replace the following:
    • ${PYTHON_VERSION} with the version of Python you want to use (ex. 3.11.5)

    • ~/venvs/my_environment with the location you want to use for your virtual environment, this directory should be in your home directory

      Terminal
      $ /opt/python/${PYTHON_VERSION}/bin/python3 -m venv ~/venvs/my_environment

Step 2. Install the ipykernel package

  • Install the ipykernel Python package in the Python environment that you want to use from Jupyter Notebooks in Workbench using the following command:

    • Replace ~/venvs/my_environment/bin/pip with the path to pip in the environment that you want to use from Jupyter Notebooks in Workbench.
    Terminal
    $ ~/venvs/my_environment/bin/pip install ipykernel
  • Optionally install any other libraries you want to be available in that virtual environment:

    Terminal
    $ ~/venvs/my_environment/bin/pip install pandas matplotlib
  • Or if you manage dependencies using a requirements.txt file:

    Terminal
    $ ~/venvs/my_environment/bin/pip install -r requirements.txt

Step 3. Register additional Python version as a new kernel

  • Register the additional Python environment as a new kernel using the following command:
    • Replace ~/venvs/my_environment/bin/python with the path to Python in the environment that you want to use from Jupyter Notebooks in Workbench.

    • You can change my-python-env and Custom Python Environment in the command below to your desired kernel name and display name.

      Terminal
      $ ~/venvs/my_environment/bin/python -m ipykernel install --name my-python-env --display-name "Custom Python environment" --user

Step 4. Verify the new kernel in Jupyter

  • From your browser, navigate to the Workbench interface and log in.
  • Select New Session and do the following:
    1. Give your session a name.
    2. In the Editor field, select either as the IDE:
      • Jupyter Notebooks or
      • JupyterLab
    3. Click Start Session.

  • From the menu bar, click Kernel > Change Kernel and select the newly installed kernel.

    Additional Python environments

    You can repeat the steps in this section to install additional Python environments that will be available only to your user.

Publishing Jupyter Notebooks to Connect

  • If you are using the rsconnect_jupyter Python package to deploy content to Connect and using multiple Python environments you have to:
    • Install the rsconnect_jupyter Python package and enable the server extension in the global Python environment that Workbench is configured for:

      Terminal
      $ /opt/python/3.11.5/bin/pip install rsconnect_jupyter
      
      # Enable Python extension
      $ /opt/python/3.11.5/bin/jupyter-serverextension enable –sys-prefix –py rsconnect_jupyter
    • Install the rsconnect_jupyter Python package and enable the notebook extension on each environment:

      Terminal
      $ ~/venvs/my_environment/bin/pip install rsconnect_jupyter
      
      # Install rsconnect-jupyter as a jupyter extension
      $ ~/venvs/my_environment/bin/jupyter-nbextension install –sys-prefix –py rsconnect_jupyter
      
      # Enable JavaScript extension
      $ ~/venvs/my_environment/bin/jupyter-nbextension enable –sys-prefix –py rsconnect_jupyter

For more information refer to the documentation for rsconnect_jupyter.

Troubleshooting Workbench and Jupyter

Refer to the support article on troubleshooting Jupyter Notebooks in RStudio Server Pro for additional information on troubleshooting Workbench with Jupyter.

Additional Documentation

For more information on Workbench and Jupyter Notebooks, refer to the Jupyter Sessions section.

For more information on using Jupyter kernels for different environments and kernel specs, refer to the following reference documentation: