notebook icon indicating copy to clipboard operation
notebook copied to clipboard

How to properly use --sys-prefix

Open thatlittleboy opened this issue 6 years ago • 4 comments
trafficstars

According to the help documentation of ipykernel install, there is a --sys-prefix flag that I can pass to install the kernel in my activated virtual environment instead of the default %appdata%/jupyter/kernels location.

However, if I do that (install the kernel into my venv/share/jupyter folder), the Jupyter notebook does not find the ipykernel when I launch the notebook.

I did:

$ mkdir testing
$ cd testing
$ pipenv install ipykernel
$ pipenv shell
$ which python jupyter ipython
/c/Users/user/.virtualenvs/testing-DhFhkxYw/Scripts/python
/c/Users/user/.virtualenvs/testing-DhFhkxYw/Scripts/jupyter
/c/Users/user/.virtualenvs/testing-DhFhkxYw/Scripts/ipython

$ jupyter kernelspec list
Available kernels:
  python3    c:\users\user\.virtualenvs\testing-dhfhkxyw\share\jupyter\kernels\python3

$ python -m ipykernel install --sys-prefix --name=testing
Installed kernelspec testing in C:\Users\user\.virtualenvs\testing-DhFhkxYw\share\jupyter\kernels\testing

$ jupyter kernelspec list
Available kernels:
  python3    c:\users\user\.virtualenvs\testing-dhfhkxyw\share\jupyter\kernels\python3
  testing    c:\users\user\.virtualenvs\testing-dhfhkxyw\share\jupyter\kernels\testing

$ jupyter --paths
config:
    C:\Users\user\.jupyter
    c:\users\user\.virtualenvs\testing-dhfhkxyw\etc\jupyter
    C:\ProgramData\jupyter
data:
    C:\Users\user\AppData\Roaming\jupyter
    c:\users\user\.virtualenvs\testing-dhfhkxyw\share\jupyter
    C:\ProgramData\jupyter
runtime:
    C:\Users\user\AppData\Roaming\jupyter\runtime

Note that at this time, the share folder of my virtual environment (testing) is in the data search paths. And that the default location C:\Users\user\AppData\Roaming\jupyter is empty. C:\Users\user\.virtualenvs\testing-DhFhkxYw\share\jupyter\kernels\testing\kernel.json reads:

{
 "argv": [
  "C:\\Users\\user\\.virtualenvs\\testing-DhFhkxYw\\Scripts\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "testing",
 "language": "python"
}

But even then, running jupyter notebook, I don't see the testing kernel in the available dropdown menu (top right hand corner). What gives?


Note that if I instead did:

$ python -m ipykernel install --user --name=testing

as suggested in the documentation, then, testing would appear in the kernel drop down list inside jupyter notebook.

So how can I get --sys-prefix to work properly here?

TL;DR I want the ipykernel to be installed in my virtualenv share folder instead of the local user folder. Using --sys-prefix does that, but it does not appear in Jupyter notebook.

Info:

I have Anaconda installed --- This is the one the Jupyter defaults to (kernel=Python3) when I try to create a notebook; Windows 64 bit.

thatlittleboy avatar Apr 15 '19 05:04 thatlittleboy

https://stackoverflow.com/questions/58068818/how-to-use-jupyter-notebooks-in-a-conda-environment/58068850 This answer may be helpful

jusdfo avatar Dec 24 '22 12:12 jusdfo

if you create jupyter kernel in specific envs, the jupyter kernel only shows when you in this env by using "jupyter kernelspec list". After using "conda deactivate", this jupyter kernel won't show. It may relate to the environment variables, I think, because the created kernel is in the "/share/jupyter/kernel/" folder which located in the root folder of specific "python.exe". When you use different envs, this "share" folder is different. The kernel will find kernels in "share" folder as well as user configuration folder.

jusdfo avatar Dec 24 '22 13:12 jusdfo

I have the same issue with Polyglot Notebook: https://github.com/dotnet/interactive/issues/3478

suugbut avatar Mar 02 '24 21:03 suugbut

I am having the same issue. I want to install and use a kernel --sys-prefix so I don't clash with globally installed kernels. The system I am on has a globally installed python3 kernel which gets activated instead of the one in the venv.

mforbes avatar May 31 '24 08:05 mforbes