Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In virtual environment wrong version of python is used #7583

Open
eudaiman opened this issue Feb 6, 2025 · 2 comments
Open

In virtual environment wrong version of python is used #7583

eudaiman opened this issue Feb 6, 2025 · 2 comments
Labels
bug status:Needs Info status:Needs Triage Applied to issues that need triage

Comments

@eudaiman
Copy link

eudaiman commented Feb 6, 2025

Description

In my virtual environment I installed jupyter notebook. But when I tried running it, I discovered that it's using the wrong python.

Reproduce

Create venv. Then install venv jupyter:
python -m pip install jupyter
python -m ipykernel install --name=my-kernel-name --prefix=$VIRTUAL_ENV
Then in jupyter notebook import sys and print sys.version. I warrant you it will not be the one in venv.

Expected behavior

Should be the same version as python -V in terminal.

Context

Using Ubuntu 20.04, everything else is latest version (python, jupyter etc.)

@eudaiman eudaiman added bug status:Needs Triage Applied to issues that need triage labels Feb 6, 2025
@minrk
Copy link
Member

minrk commented Feb 6, 2025

This suggests to me that perhaps there may be a different kernelspec installed somewhere unexpected. can you share jupyter kernelspec list? And cat $VIRTUAL_ENV/share/jupyter/kernels/my-kernel-name/kernel.json?

Here's what I get following your steps:

> python3 -m venv /tmp/my-env
minrk@heavy-3 [11:18:49] ~
> source /tmp/my-env/bin/activate
(my-env) minrk@heavy-3 [11:18:56] ~
> which python3
/tmp/my-env/bin/python3
(my-env) minrk@heavy-3 [11:19:05] ~
> python3 -m pip install ipykernel notebook
...
Successfully installed MarkupSafe-3.0.2 anyio-4.8.0 appnope-0.1.4 argon2-cffi-23.1.0 argon2-cffi-bindings-21.2.0 arrow-1.3.0 asttokens-3.0.0 async-lru-2.0.4 attrs-25.1.0 babel-2.17.0 beautifulsoup4-4.13.3 bleach-6.2.0 certifi-2025.1.31 cffi-1.17.1 charset-normalizer-3.4.1 comm-0.2.2 debugpy-1.8.12 decorator-5.1.1 defusedxml-0.7.1 executing-2.2.0 fastjsonschema-2.21.1 fqdn-1.5.1 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1 idna-3.10 ipykernel-6.29.5 ipython-8.32.0 isoduration-20.11.0 jedi-0.19.2 jinja2-3.1.5 json5-0.10.0 jsonpointer-3.0.0 jsonschema-4.23.0 jsonschema-specifications-2024.10.1 jupyter-client-8.6.3 jupyter-core-5.7.2 jupyter-events-0.12.0 jupyter-lsp-2.2.5 jupyter-server-2.15.0 jupyter-server-terminals-0.5.3 jupyterlab-4.3.5 jupyterlab-pygments-0.3.0 jupyterlab-server-2.27.3 matplotlib-inline-0.1.7 mistune-3.1.1 nbclient-0.10.2 nbconvert-7.16.6 nbformat-5.10.4 nest-asyncio-1.6.0 notebook-7.3.2 notebook-shim-0.2.4 overrides-7.7.0 packaging-24.2 pandocfilters-1.5.1 parso-0.8.4 pexpect-4.9.0 platformdirs-4.3.6 prometheus-client-0.21.1 prompt_toolkit-3.0.50 psutil-6.1.1 ptyprocess-0.7.0 pure-eval-0.2.3 pycparser-2.22 pygments-2.19.1 python-dateutil-2.9.0.post0 python-json-logger-3.2.1 pyyaml-6.0.2 pyzmq-26.2.1 referencing-0.36.2 requests-2.32.3 rfc3339-validator-0.1.4 rfc3986-validator-0.1.1 rpds-py-0.22.3 send2trash-1.8.3 six-1.17.0 sniffio-1.3.1 soupsieve-2.6 stack_data-0.6.3 terminado-0.18.1 tinycss2-1.4.0 tornado-6.4.2 traitlets-5.14.3 types-python-dateutil-2.9.0.20241206 typing_extensions-4.12.2 uri-template-1.3.0 urllib3-2.3.0 wcwidth-0.2.13 webcolors-24.11.1 webencodings-0.5.1 websocket-client-1.8.0
(my-env) minrk@heavy-3 [11:19:34] ~
> python3 -m ipykernel install --name=my-kernel-name --prefix=$VIRTUAL_ENV
Installed kernelspec my-kernel-name in /tmp/my-env/share/jupyter/kernels/my-kernel-name
(my-env) minrk@heavy-3 [11:20:28] ~
> jupyter kernelspec list
Available kernels:
  my-kernel-name    /tmp/my-env/share/jupyter/kernels/my-kernel-name
  python3           /tmp/my-env/share/jupyter/kernels/python3
(my-env) minrk@heavy-3 [11:21:00] ~
> cat /tmp/my-env/share/jupyter/kernels/my-kernel-name/kernel.json
{
 "argv": [
  "/tmp/my-env/bin/python3",
  "-Xfrozen_modules=off",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "my-kernel-name",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}%

and after launching jupyter-notebook, create a notebook with my-kernel-name:

In [1]: sys.executable
Out[1]: '/tmp/my-env/bin/python3'

so everything appears to be going as expected.

@prajwal-38
Copy link

The issue arises because the --prefix=$VIRTUAL_ENV flag for ipykernel install is incorrect. It installs the kernel in the wrong location, not properly linking it to the virtual environment's Python.

create your venv and then activate it, and while in venv you need to install jupyter and ipykernel using pip

pip install jupyter ipykernel

and then install kernel properly

python -m ipykernel install --user --name=my-kernel-name --display-name "Python(venv)"

Hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug status:Needs Info status:Needs Triage Applied to issues that need triage
Projects
None yet
Development

No branches or pull requests

4 participants