Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open an IPython console connected to an exiting running kernel in PyCharm

Is there a way to open an IPython interactive console in pycharm that is connected to an existing running kernel (similar to "python --existing")?

btw: in case it's relevant, in my case, the running kernel is of a Jupiter notebook...

EDIT: To clarify, my question is NOT about how to open an interactive console in PyCharm. It is about how to connect that interactive console to a an existing running (Jupiter notebook) Kernel.

like image 766
amit Avatar asked Feb 23 '17 18:02

amit


People also ask

How do I run IPython in PyCharm?

IPython is available on your computer with Anaconda. The corresponding Conda environment is set as the default interpreter for the current project. Once you set a Conda environment as a Python interpreter, PyCharm automatically recognizes IPython, and changes the prompt of the Python console. From the main menu, select Tools | Python Console.

How do I open the console in PyCharm?

You can assign a shortcut to open Python console: press Ctrl+Alt+S, navigate to Keymap, specify a shortcut for Main menu | Tools | Python or Debug Console. The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.

How do I use Conda with PyCharm?

Once you set a Conda environment as a Python interpreter, PyCharm automatically recognizes IPython, and changes the prompt of the Python console. Use the magic commands of IPython From the main menu, select Tools | Python Console. If IPython has been properly installed, PyCharm will report about the version used.

How to run Qt console in a process in IPython?

Start a special IPython kernel, the IPython.kernel.inprocess.ipkernel.InProcessKernel, that allows a QtConsole in the same process. See examples/inprocess/embedded_qtconsole.py for an example.


1 Answers

(tl;dr: Use jupyter console --existing in the PyCharm "Terminal" tool window (not the "Python Console" tool window) to connect to an existing iPython kernel running in a local Jupyter Notebook server.)

I can confirm that the comment by @john-moutafis suggesting ipython console --existing is the right idea. The command gives "WARNING | You likely want to use jupyter console in the future" so I tried that.

  • I have a project using a conda environment as its interpreter. Jupyter Notebook is installed in the conda environment.
  • I open the Terminal tool window. It automatically activates the conda environment.
  • I type jupyter notebook. The notebook server starts and a browser window opens.
  • I create a notebook in the browser, and execute a cell containing foo = "bar".
  • In PyCharm, I open another Terminal tool window by clicking the plus sign to the left of the terminal pane.
  • In the new terminal I type jupyter console --existing, and it starts an ipython console session.
  • At the prompt I type dir(), and foo is among the results, confirming that I'm attached to the same kernel as the notebook.

I don't know how it picks which kernel to connect to when there are multiple kernels running in the notebook server.

Don't type exit in the iPython session if you plan to continue using the notebook, it shuts down the kernel.

Unfortunately, tools like Debug and "Execute Line/Selection in Console", which are available for the "Python Console" tool window, are not available for the "Terminal" tool window. In fact, because the Terminal tool window is a simple tool, and that's where I've run my commands, this solution isn't very integrated with PyCharm. The terminal opens in the project directory and activates the conda environment, and it's conveniently adjacent to the editors and tools of the IDE, but otherwise there's no connection to PyCharm's tools.

If anyone can successfully attach PyCharm's integrated PyDev debugger to a running kernel, please chime in.

I'm using PyCharm 2016.3 on macOS 10.12.3.

like image 98
jjpr Avatar answered Nov 12 '22 13:11

jjpr