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

六月ゝ 毕业季﹏ 提交于 2019-11-28 11:46:26

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!