# Jupyter Kernel not running same Python version as the Virtual Environment

**URL:** <https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521>\
**Category:** Notebook\
**Created:** [May 23, 2020, 2:15pm UTC](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521 "2020-05-23T14:15:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkMIR4GE](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/darkmir4ge/32/2122_2.png) [@DarkMIR4GE](https://discourse.jupyter.org/u/DarkMIR4GE)\
**Post date:** [May 23, 2020, 2:15pm UTC](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521/1 "2020-05-23T14:15:10Z")

</div>

I want to be able to run both **Python 3.8** (currrent version) and **Python 3.7** in my Jupyter Notebook. I understand creating different IPython kernels from virtual environments is the way.  
So I downloaded Python 3.7 and locally installed it in my home directory. Used this python binary file to create a virtual environment by

```
> virtualenv -p ~/Python3.7/bin/python3 py37
> source py37/bin/activate

```

This works perfectly and gives ‘Python 3.7’ correctly on checking with `python --version` and `sys.version`.  
Then for creating IPython kernel,

```
(py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
(py37) > jupyter notebook

```

This also runs without error and the kernel can be confirmed to be added in the Notebook. However it does not run Python 3.7 like the virtual environment, but Python 3.8 like the default kernel. (confirmed with `sys.version`)

I checked `~/.local/share/jupyter/kernels/py37/kernel.json` and saw its contents as

```
{
 "argv": [
  "/usr/bin/python3",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3.7",
 "language": "python"

```

So naturally I tried editing the `/usr/bin/python3` to point to my Python 3.7 binary file path that is `~/Python3.7/bin/python3`, but then even the kernel doesn’t work properly in the notebook.

What can I possibly do?

NB: I use **Arch Linux** , so I installed **jupyter** , **virtualenv** , … through **pacman** not **pip** as its recommended in Arch.

---

<div class="post-metadata">

**Author:** ![DarkMIR4GE](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/darkmir4ge/32/2122_2.png) [@DarkMIR4GE](https://discourse.jupyter.org/u/DarkMIR4GE)\
**Post date:** [May 24, 2020, 4:43am UTC](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521/2 "2020-05-24T04:43:07Z")

</div>

Found it myself, the hard way. Let me share anyway, in case this helps anyone.

I guess, the problem was that, jupyter notebook installed through **pacman** searches for python binary files in the PATH variable and not in the path specified by the virtual environment. Since I installed Python 3.7 locally in my home directory, Jupyter can’t find it and it might have defaulted to the default python version.

So the possible solutions are:

1. Install Jupyter Notebook through **pip** (instead of **pacman** ) within the virtual environment set on Python 3.7 _(This is not at all recommended for **Arch Linux** users, as installing packages through pip can probably cause issues in future)_

> ```
> > wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz  
> > tar -xvf Python-3.7.4.tgz    
> > cd Python-3.5.1/     
> > ./configure --prefix=$HOME/Python37     
> > make     
> > make install   
> 
> > virtualenv -p ~/Python3.7/bin/python3 py37   
> > source py37/bin/activate 
>   
> (py37) > pip install notebook   
> (py37) > python -m notebook
> 
> ```

1. Install Python 3.7 within default directory (instead of specifying somewhere else). Create a new IPython kernel using the suitable virtual environment and use **jupyter-notebook** installed through **pacman**. _(Recommended for **Arch Linux** users)_  
_Note 1: `> python` points to the updated global Python 3.8 version and `> python3` or `> python3.7` points to newly installed Python 3.7_  
_Note 2: Once the required kernel is created, you might even be able to use that python version outside the virtual environment._

> ```
> > wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz  
> > tar -xvf Python-3.7.4.tgz    
> > cd Python-3.5.1/     
> > ./configure   
> > make     
> > sudo make install   
> 
> > virtualenv -p $(which python3.7) py37   
> > source py37/bin/activate 
>   
> (py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
> (py37) > jupyter notebook
> 
> ```

1. Add the path of the directory where you have locally installed the new Python version to the **$PATH** variable, create an IPython kernel and run Jupyter Notebook within suitable virtual environment. _(Haven’t yet tried this one personally. Just felt that this should work. So no guarantee. Also I don’t think this is a good solution)_

> ```
> > wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz  
> > tar -xvf Python-3.7.4.tgz    
> > cd Python-3.5.1/     
> > ./configure --prefix=$HOME/Python37 
> > make     
> > make install   
> > export PATH="$HOME/Python37/bin:$PATH"          
> 
> > virtualenv -p py37   
> > source py37/bin/activate 
>   
> (py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
> (py37) > jupyter notebook
> 
> ```

---

<div class="post-metadata">

**Author:** ![joeflack4](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/joeflack4/32/6110_2.png) [@joeflack4](https://discourse.jupyter.org/u/joeflack4)\
**Post date:** [January 3, 2022, 1:52am UTC](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521/3 "2022-01-03T01:52:45Z")

</div>

This is probably a rare edge case, and I know it doesn’t apply to the original post here, but there aren’t many threads coming up on the subject.

For me, it wasn’t working with my `virtualenvwrapper` environment. When I switched to a normal `virtualenv`, it worked.

Maybe this will help some people. I know it won’t help the OP, since he’s already using `virtualenv`.

---

<div class="post-metadata">

**Author:** ![sndselecta](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/sndselecta/32/2755_2.png) [@sndselecta](https://discourse.jupyter.org/u/sndselecta)\
**Post date:** [February 21, 2024, 7:49pm UTC](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521/5 "2024-02-21T19:49:47Z")

</div>

I’ve experienced the same problem for a while now. My system is a Mac OSX 13.6.3, but this has happened through many os versions, same machine intel core i5, zsh in term2, zsh version 5.9 (x86\_64-apple-darwin22.0). I created a venv using venv, activated it, pip installed jp nb (obv within the env), when I launched jp-nb, the kernel points to the global python on my system and not the venv version, therefore no libs from the virtualenv site packages which were pip installed accessable. Only way to get it to work is to append to sys.path the venv site packages manually in jp-nb. Forcing the kernel via ipython kernel install was one of the options that worked, but I felt it was a work around. There is a way modify the kernel spec of jp-nb, but I didnt have time to try it. Check this article good article. [Installing Python Packages from a Jupyter Notebook | Pythonic Perambulations](https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/)

So I had thought the python -m pip install module in an activate virtualenv was working but in fact it isn’t. I do not think this post should be closed.

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [February 22, 2024, 1:23am UTC](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521/6 "2024-02-22T01:23:30Z")

</div>

> [@sndselecta](#):
>
> Check this article good article. [Installing Python Packages from a Jupyter Notebook | Pythonic Perambulations](https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/)

That resource is woefully outdated @sndselecta .  
The magic install commands were added in 2019 to make it much easier to install packages from within the running notebook.  
See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4) for more about the modern magic install commands.  
You’ll see one of those listed resources, [this one in particular](https://twitter.com/jakevdp/status/1139213090745020416), is by the author of Pythonic Permabulations suggesting the way added in 2019 is the improvement since that 2017 post you referenced.
