How to start an ipython shell(not notebook) within a conda or virtualenv

后端 未结 2 432
故里飘歌
故里飘歌 2020-12-06 15:16

Is it possible to start an ipython shell(in terminal) within a conda or virtualenv ? The ipython shell should belongs to the respective environment.

I know a way to

相关标签:
2条回答
  • 2020-12-06 16:03

    I know a way to start jupyter notebook within the env, by creating a kernelspecs for the virtual env and then choosing the env kernel within the jupyter notebook.

    You just need to do the same thing, but using console instead of notebook.

    For example:

    ipython console --kernel python2
    
    0 讨论(0)
  • 2020-12-06 16:11

    The answer given by Grisha Levit almost solved the problem. So, i am writing the complete details of the answer, how to setup a ipython console within a specific environment.

    1.) Activate the virtual env:

    source activate <environment-name>
    

    2.) From within the virtual env:

    jupyter kernelspec install-self --user
    

    3.) This will create a kernelspec for your virtual env and tell you where it is:

    • Installed kernelspec pythonX in home/username/.local/share/jupyter/kernels/pythonX

    Where pythonX is the version of the python in the virtualenv.

    4.) Copy the new kernelspec somewhere useful. Choose a kernel_name for your new kernel that is not python2 or python3 or one you've used before and then:

    mkdir -p ~/.ipython/kernels
    mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>
    

    5.) If you want to change the name of the kernel that IPython shows you, you need to edit ~/.ipython/kernels//kernel.json and change the JSON key called display_name to be a name that you like.

    6.) Running jupter/ipython console within the virtualenv.

    jupyter console --kernel <kernel-name>
    

    7.) This will start the jupyter console/shell for the current virtualenv and you can also see kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

    0 讨论(0)
提交回复
热议问题