# What is the relationship between kernels and virtual environments?

**URL:** <https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293>\
**Category:** General\
**Created:** [August 8, 2021, 4:51pm UTC](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293 "2021-08-08T16:51:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orome](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/orome/32/4914_2.png) [@orome](https://discourse.jupyter.org/u/orome)\
**Post date:** [August 8, 2021, 4:51pm UTC](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293/1 "2021-08-08T16:51:56Z")

</div>

I’m confused (noob) about the relationship between IPython kernels and Python virtual environments (at least as they are used in JupyterLab).

For example, if I activate a virtual environment (in my case typically with `workon my_venv`) and then

```bash
python -m ipykernel install --user --name my_kernel --display-name "Python (my_kernel)"

```

what is the connection between the kernel `my_kernel` and the virtual environment `my_env`?

In my typical workflow, once both the virtual environment and the kernel are set up I

```bash
$ workon my_venv
$ [my_venv] jupyter lab

```

and end and end up in a Juyter session where

1. the terminal acts as if the `my_venv` has been activated;
2. all shell commands issued from any notebook, regardless of associated kernel, act as if `my_venv` has been activated; and
3. a notebook has access (only) to packages — e.g., via `import` — installed in the associated kernel running (which need not be `my_venv`).

Also, in addition to any kernels I have explcitly defined, I have a (local to `my_venv`?) kernel called “Python 3 (ipykernel)” which is associated with `my_venv`.

So my questions are:

1. is the above a reasonable summary of the relationship between IPython kernels and Python virtual environments? Am I missing something important?
2. Is it correct that I don’t really ever need to create my own kernel manually for a virtual environment, since one is created automatically, and is exactly the same as the manual one that’s created with the step above (except for name and visibility)?
3. Is there a way to name the automatically created kernel, and is there a way to customize that step (when and with what tools, e.g., to change its name).

---

<div class="post-metadata">

**Author:** ![Zsailer](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/zsailer/32/4872_2.png) [@Zsailer](https://discourse.jupyter.org/u/Zsailer)\
**Post date:** [August 9, 2021, 4:36pm UTC](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293/2 "2021-08-09T16:36:56Z")

</div>

@orome, great question!

Jupyter(Lab/Server/Notebook) “dynamically” creates a kernel(spec) for your current python environment—that kernel type only exists in that virtual environment. This ensures that you always have a python kernel available to you, and that it matches the environment where your current JupyterLab is running.

When you manually create a kernel spec, using the `python -m ipykernel install --user ...`, you’re creating a static kernel(spec) on disk that can be discovered from _other_ virtual environments. The advantage of this is that you can install JupyterLab in one virtual environment while running kernels from other virtual environments.

Many people use this feature to keep their kernel environments isolated. They might have different kernels for different tasks/workflows.

---

<div class="post-metadata">

**Author:** ![orome](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/orome/32/4914_2.png) [@orome](https://discourse.jupyter.org/u/orome)\
**Post date:** [August 18, 2021, 5:29pm UTC](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293/3 "2021-08-18T17:29:19Z")

</div>

There’s a [bit of a glitch though](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293) (not in the tech, but in the simple “kernel-venv” user model) here though with regard to extensions (now) automatically enabled by installing a package into an associated venv: those extensions are not available the kernels, but only to Jupyter Lab instances launched from the activated env.

---

<div class="post-metadata">

**Author:** ![radugrosu](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/radugrosu/32/7201_2.png) [@radugrosu](https://discourse.jupyter.org/u/radugrosu)\
**Post date:** [May 20, 2022, 11:08am UTC](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293/4 "2022-05-20T11:08:50Z")

</div>

Hi @Zsailer. Is there a possibility to create the static kernel _without_ installing ipykernel in that particular environment? My problem is that I don’t want anything installed in that env that will not run in prod, which includes ipykernel and its dependencies. It feels like it should be possible (and I tried simply creating the spec manually, but jupyter refuses to load it, probably because the connection file is wrong - and I don’t know how to solve that). Any hints would be greatly appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![Neela\_Das](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/neela_das/32/16098_2.png) [@Neela\_Das](https://discourse.jupyter.org/u/Neela_Das)\
**Post date:** [November 27, 2024, 3:11am UTC](https://discourse.jupyter.org/t/what-is-the-relationship-between-kernels-and-virtual-environments/10293/5 "2024-11-27T03:11:39Z")

</div>

“Jupyter(Lab/Server/Notebook) “dynamically” creates a kernel(spec) for your current python environment—that kernel type only exists in that virtual environment.” Is the name of this kernel “Python3 IPyKernel” ???
