Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use ipython to get REAL code-completion in pycharm

Many python IDE's boasts of providing code-completion (code insight), PyCharm is one of those IDE's. However, it seems to me that the provided code-completion is extremely limited. Let me give you an example to make it clear:

import numpy as np
m = np.random.random((3,5))
m.

Hitting CTRL-space after 'm.' will not give me any code-completion, -no matter how hard I hit it ;).. I guess this is because the IDE would have to do type inference to know the type of the variable 'm', and that this isn't trivial in the domain of dynamic programming languages.

Now, PyCharm comes with a setting called "Collect run-time types information for code insight", which indeed sounds promising. However, it doesn't seem to fix the problem mentioned above.. I am still not able to get code-completion on the variable 'm'.

Thus far, I have only found one way to get code-completion on variables in PyCharm:

import numpy as np
m = np.random.random((3,5))
''':type : np.matrix'''
m.

In this example I am able to get code-completion when pressing CTRL-space after 'm.', and this is because I am helping the IDE by specifying the type of the variable with a docstring. I am, however, not satisfied with this way of getting code-completion, because it adds unnecessary verbosity to the code with all these docstrings (not to mention all the extra keyboard-typing)...

IPython to the rescue.. (maybe?)

Now, if we start IPython in a linux-terminal, and enter the first piece of code, we will be able to get code-completion all the way, -even for the variable 'm'. (where the code-completion in IPython is achieved by pressing TAB instead of CTRL-space)..

I don't have much experience with IPython, but I believe that I've heard something about IPython constantly executing the code in a loop or something like that...

I am thinking that it should be possible to use IPython to achieve REAL code-completion on all variables in the editor of PyCharm....

Is there a way to setup PyCharm to use IPython for code-completion?

Note that I am not satisfied with sending the code to a terminal window/console, or something like that, I want code-completion inside the editor of PyCharm...

I have looked at questions like this Adding ipython as an interpreter in Pycharm Ubuntu, but it seems to be about using IPython in the console, -not in the editor... There are also plenty of questions talking about code-completion in IDE's, but they all seem to have the same unsatisfying level of code-completion as PyCharm...

My setup

  • OS: Debian testing
  • python: Python3 and IPython3
  • IDE: Pycharm 3.0.2 professional edition
like image 641
Michelrandahl Avatar asked Jan 31 '14 17:01

Michelrandahl


2 Answers

It cannot tell what returns are from functions(with out actually running the script, thats why ipython knows what it is (it has actually run the code and recieved back an object it can introspect)

if you want code completion without having to actually execute your script up to where you are entering text you have to do an extra step

import numpy as np
m = np.random.random((3,5))
assert isinstance(m,np.ndarray)
m. #now you get code completion (since the IDE now knows the class of m, without having to execute your script)
like image 140
Joran Beasley Avatar answered Sep 17 '22 12:09

Joran Beasley


I had the same question, I found the answer here: https://www.jetbrains.com/pycharm/help/using-ipython-notebook-with-pycharm.html

What you need to do is to create a ipython notebook in the project tool window. This is a file with the extension '.ipynb'. Right click on the directory where you want to put the file, select 'New-->File', enter a file name in the dialog box and give the file extension .ipynb. Open the notebook file and when you start to type something in the window, a drop down window appears with objects in the namespace plus any commands that start with the letters already typed.

like image 29
flynneria Avatar answered Sep 17 '22 12:09

flynneria