# Jupyter jumps out of virtual env to reference different python?

**URL:** <https://discourse.jupyter.org/t/jupyter-jumps-out-of-virtual-env-to-reference-different-python/9455>\
**Category:** Kernels\
**Created:** [June 3, 2021, 10:03pm UTC](https://discourse.jupyter.org/t/jupyter-jumps-out-of-virtual-env-to-reference-different-python/9455 "2021-06-03T22:03:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkbelew](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rkbelew](https://discourse.jupyter.org/u/rkbelew)\
**Post date:** [June 3, 2021, 10:03pm UTC](https://discourse.jupyter.org/t/jupyter-jumps-out-of-virtual-env-to-reference-different-python/9455/1 "2021-06-03T22:03:25Z")

</div>

i’m having trouble getting a kernel started under a virtualenv.  
i’ve run across this post from May’20  
[Jupyter Kernel not running same Python version as the Virtual Environment](https://discourse.jupyter.org/t/jupyter-kernel-not-running-same-python-version-as-the-virtual-environment/4521)  
but still haven’t solved it. (THis is on MacOSX v10.15.) i’ll attach  
the full trace below, but the issue seems to be that in the  
virtualenv within which i fire up `jupyter notebook`,  
`jupyter_client/launcher.launch_kernel()` calls a different python?!

the issue must be something in the environment? looking at the  
dictionary of values that is the value of `cmd` (line#132) everything  
looks fine, including that `PATH` has the virtualenv `bin` directory  
first.

looking thru jupyter options, the only ones that point to a particular kernel  
to start have `python3` as their default. but `which python3` also  
points to the correct python within the virtualenv.

Anyone have a clue what’s going on, or how to fix it?

Full trace below:

```auto

Traceback (most recent call last):
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/web.py", line 1704, in _execute
result = await result
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/notebook/services/sessions/handlers.py", line 69, in post
model = yield maybe_future(
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/gen.py", line 762, in run
value = future.result()
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/notebook/services/sessions/sessionmanager.py", line 98, in create_session
kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/gen.py", line 762, in run
value = future.result()
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/notebook/services/sessions/sessionmanager.py", line 110, in start_kernel_for_session
kernel_id = yield maybe_future(
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/tornado/gen.py", line 762, in run
value = future.result()
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/notebook/services/kernels/kernelmanager.py", line 176, in start_kernel
kernel_id = await maybe_future(self.pinned_superclass.start_kernel(self, **kwargs))
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/jupyter_client/multikernelmanager.py", line 186, in start_kernel
km.start_kernel(**kwargs)
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/jupyter_client/manager.py", line 341, in start_kernel
self.kernel = self._launch_kernel(kernel_cmd, **kw)
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/jupyter_client/manager.py", line 249, in _launch_kernel
return launch_kernel(kernel_cmd, **kw)
File "/System/Volumes/Data/rikData/virtualenv/covasim/lib/python3.8/site-packages/jupyter_client/launcher.py", line 132, in launch_kernel
proc = Popen(cmd, **kwargs)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/opt/python/bin/python3.7'

```

---

<div class="post-metadata">

**Author:** ![rawlins](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/rawlins/32/5385_2.png) [@rawlins](https://discourse.jupyter.org/u/rawlins)\
**Post date:** [October 4, 2021, 3:27pm UTC](https://discourse.jupyter.org/t/jupyter-jumps-out-of-virtual-env-to-reference-different-python/9455/2 "2021-10-04T15:27:31Z")

</div>

Just to say that I’ve been having this happen sporadically when switching between kernels (while testing a kernel), somehow occasionally even the default ipykernel has somehow managed to escape the conda env and call the wrong python binary (in my testing I can see that this is happening just by looking at `sys.path`). I unfortunately cannot figure out how to reproduce this reliably (or at all right now) but you aren’t insane.
