How to change a virtual environment while using jupyter lab?


  1. How can I change my environment from the base (root) environment to newly created environment while using jupyter lab?
  2. How can I change the ‘root’ environment, where the jupyter lab is activated?

More in Detail

  • I haven’t used virtual environments much, so I am novice. Please bear with me even if my question sounds stupid.
  • Let me tell you the process of what I did, so that you can find out which step in the process is problematic for me.

1) Creation of a virtual environment in Anaconda Navigator

  • I created another virtual environment called testing because some libraries have crushed with each other in the base (root) environment.
  • I created the testing environment in the anaconda navigator, because it is much simpler to create a virtual env on anaconda navigator than console, because anaconda navigator is GUI.

2) After opening up jupyter lab, I cannot change the virtual environment

  • After I open up jupyter lab by typing jupyter lab in cmd, it seems like there is no way to change my environment, from base (root) to testing, which I created.
  • How can I change the virtual environment after executing the jupyter lab?

3) What to do if I want to start the jupyter lab from the testing environment?

  • How can I change the root environment, so that the testing environment is a basic environment that the jupyter lab starts with?

@krassowski Any suggestion, please?

  1. How can I change my environment from the base (root) environment to newly created environment while using jupyter lab?

You may be interested in GitHub - Anaconda-Platform/nb_conda_kernels: Package for managing conda environment-based kernels inside of Jupyter

As for other questions these look like basic questions about using conda; I would recommend reading through the conda User Guide (User guide — conda 4.10.3.post47+e6936a3d documentation). As for Anaconda Navigator - I never used it myself so cannot help here; it is a commercial product and they offer some support - see Help and support — Anaconda documentation.

However, there is some exciting work on JupyterLab Desktop support for virtual environments. Stay tuned.


Thank you @krassowski . However, the nb_conda_kernels does not seem to relate to my question, because it is about kernels, not the environment. The github repo of the library says as follows
This extension enables a Jupyter Notebook or JupyterLab application in one conda environment to access kernels for Python, R, and other languages found in other environments.

What I wanted to do was, when my root environment and another environment are both python kernels, I wanted to change the environment so that each environment can be used for a single purpose. For example, environment A is for data analysis while the environment B is for developing a web-page. Both A and B are using Python kernels.

Even in my example above, do you think the nb_conda_kernels has something to do with my question?

You can either have JupyterLab installed in a single environment and swap kernels coming from different environments, or have JupyterLab installed in each environment and spawn it from each environment separately, but hot swapping environments with JupyterLab itself is not supported, nor can I see any advantage over the other options available.

1 Like

Perhaps the below link offers something different? I came across the same issue, and found this helpful. Creating and Using Virtual Environment on Jupyter Notebook with Python | by Cornellius Yudha Wijaya | Towards Data Science

Also, GitHub - jupyterlab/jupyterlab-desktop: JupyterLab desktop application, based on Electron. allows swapping environments.

There is also this interactive approach:

As for *naconda… I can’t really recommend that installation much anymore aside from the very specific case where you are taking a class that requires an exact version of that distribution.

But for the most part, I generally recommend:

  • start with Mambaforge
    • never do anything in that environment, aside from updating mamba and its dependencies (e.g. security updates, ssl cert)
  • write and check in a solid environment.yml that describes your environment
    • include jupyterlab and your “object” packages here together
    • don’t install conda or mamba in “sub” environments
    • if need be, do include pip and any pip-only leaf packages
  • only ever use mamba env upgrade --file environment.yml --prefix ~/some/path