Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why autocompletion options in Spyder 3.1 are not fully working in the Editor?

Running on Mac Sierra, the autocompletion in Spyder (from Anaconda distribution), seems quite erratic. When used from the Ipython console, works as expected. However, when used from the editor (which is my main way of writing), is erratic. The autocompletion works (i.e. when pressing TAB a little box appears showing options) for some modules, such as pandas or matplotlib. So writing 'pd.' and hitting TAB, gets the box with options as expected. However, this does not happen with many other objects: for example, after defining a dataframe named 'df', typing 'df.' TAB shows nothing. In the Ipython console, 'df.' TAB would show the available procedures for that dataframe, such as groupby, and also its columns, etc..

So the question is threefold. First, is there any particular configuration that should be enabled to get this to work? I don't think so, given some time spent googling, but just wanna make sure. Second, could someone state what is the official word on what works and what doesn't in terms of autocompletion (e.g. what particular modules do work from the editor, and which ones doesn't?). Finally, what are the technical aspects of the differences between the editor and the Ipython console in the performance of the autocompletion with Spyder? I read something about Jedi vs. PsychoPy modules, so got curious (however, please keep in mind that although I have scientific experience, I am relatively new to computation, so please keep it reasonably simple for an educated but not expert person).

UPDATE: As a side question, it would be great to know why is the autocompletion better in Rodeo (another IDE). It is more new, has way fewer overall options than Spyder, but the autocompletion works perfectly in the editor.

like image 927
Alejandro Avatar asked Mar 14 '17 03:03

Alejandro


People also ask

How do I enable suggestions on Spyder?

To do so, open Spyder's preferences, click on "Completion and linting" on the left hand side, and under the "Completion" tab, make the following changes: Ensure that "Enable code completion" is checked. Ensure that "Show completions on the fly" is checked. Ensure that "Enable code snippets" is checked.

Is Spyder a good code editor?

Spyder also supports debugging, has a variable explorer, and a history of previous commands. You'll find a lot of these features in other Python IDEs. Spyder lacks some more advanced features like version control, but its usability makes it a great choice for many projects.

How do I run code in editor on Spyder?

Once you have it open in your Editor, you can execute it by pressing the green run button. We can see the output in the Python Console [Show IPython console] as well as the path of the file that we are running and the working directory where this code was run.

How do I fix my Python Spyder?

Restart your machine, in case the problem lies with a lingering process or another such issue. From the Anaconda Prompt/Terminal/command line (on Windows/Mac/Linux), run the command spyder --reset , which will restore Spyder's config files to their defaults, which solves a huge variety of Spyder issues.


2 Answers

Autocomplete was not working for me at all. So, I tried Tools -> Reset Sypder to factory defaults and it worked.

like image 78
hemanto Avatar answered Oct 23 '22 11:10

hemanto


(Spyder developer here)

My answers:

is there any particular configuration that should be enabled to get this to work?

In Spyder 3.1 we added the numpydoc library to improve completions of some objects (like Matplotlib figures and NumPy arrays). If Dataframe completions are not working for you (they are for me), please open an issue in our issue tracker on Github to track and solve this problem.

could someone state what is the official word on what works and what doesn't in terms of autocompletion (e.g. what particular modules do work from the editor, and which ones doesn't?)

The most difficult part is getting completions of definitions when an object is generated by functions or methods developed in C/C++/Fortran and not in Python. I mean, things like

import numpy as np
a = np.array([])
a.<TAB>

As I said, this should be working now for arrays, figures and dataframes, but it doesn't work for all libraries (and most scientific Python libraries are created in C/C++/Fortran and wrapped in Python for speed).

The problem is that the completion libraries we use (Rope and Jedi) can't deal with this case very well because array (for example) can't be introspected in a static way (i.e. without running code involving it). So we have to resort to tricks like analyzing array's docstring to see its return type and introspect that instead.

what are the technical aspects of the differences between the editor and the Ipython console in the performance of the autocompletion with Spyder?

The most important difference is that in the IPython console you have to run your code before getting completions about it. For example, please run this in a fresh IPython console

In [1]: import pandas as pd
   ...: df = pd.Da<Tab>

and you will see that it won't return you any completions for Da (when it obviously should return Dataframe).

But, after evaluation, it is quite simple to get completions. You can simply run

dir(pd)

to get them (that's what IPython essentially does internally).

On the other hand, Spyder's Editor doesn't have a console to run code into, so it has to get completions by running static analysis tools in your code (like Jedi and Rope). As I said, they introspect your code without running it. While they work very well for pure Python code, they have the problems I described above for compiled libraries.

And trying to evaluate the code you have in the Editor to get completions is usually not a good idea because:

  1. It is not necessarily valid Python code all the time. For example, suppose you left an unclosed parenthesis somewhere, but you want to get completions at some other point. That should work without problems, right?

  2. It could involve a very costly computation (e.g. loading a huge CSV in a Dataframe), so evaluating it every time to get completions (and that's a must because your code is different every time you ask for completions) could consume all your RAM in a blink.

it would be great to know why is the autocompletion better in Rodeo (another IDE)

Last time I checked (a couple of years ago), Rodeo evaluated your code to get completions. However, we'll take a look at what they are doing now to see if we can improve our completion machinery.

like image 38
Carlos Cordoba Avatar answered Oct 23 '22 11:10

Carlos Cordoba