How to start an ipython shell(not notebook) within

2019-01-19 08:00发布

问题:

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 start jupyter notebook within the env, by creating a kernelspecs for the virtual env and then choosing the env kernel within the jupyter notebook.

here is the link : http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

But this only setup the jupyter notebook for the current environment. Is there a to do the same for ipython shell

回答1:

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.



回答2:

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