为什么Spyder 3.1中的自动完成选项在编辑器中不能完全起作用? [英] Why autocompletion options in Spyder 3.1 are not fully working in the Editor?

查看:105
本文介绍了为什么Spyder 3.1中的自动完成选项在编辑器中不能完全起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac Sierra上运行,Spyder的自动完成功能(来自Anaconda发行版)似乎很不稳定.从Ipython控制台使用时,可以正常工作.但是,在编辑器中使用(这是我的主要写作方式)时,情况很不稳定.自动补全功能适用于某些模块(例如pandas或matplotlib)(即,按TAB时会出现一个小框,显示选项).所以写"pd".并按一下TAB键,得到带有预期选项的框.但是,对于其他许多对象则不会发生这种情况:例如,在定义名为"df"的数据框后,键入"df". TAB什么也没显示.在Ipython控制台中,为"df". TAB将显示该数据框的可用过程,例如groupby及其列等.

所以问题有三重.首先,是否应该启用任何特定的配置才能使其正常工作?考虑到花了一些时间在谷歌搜索上,我不这么认为,但只想确定一下.其次,有人可以在自动补全方面说明什么有效的正式词是什么(例如,哪些特定模块在编辑器中起作用,哪些不起作用).最后,在使用Spyder自动完成时,编辑器和Ipython控制台之间的区别在技术方面有哪些?我读了一些有关Jedi与PsychoPy模块的内容,因此感到很好奇(但是,请记住,尽管我有科学经验,但我对计算还比较陌生,所以请对受过教育但不是专家的人保持简单)./p>

更新:作为附带的问题,很高兴知道为什么Rodeo(另一个IDE)中的自动完成功能更好.它比Spyder更加新,具有更少的总体选项,但是自动补全功能在编辑器中可以完美地工作.

解决方案

(此处是Spyder开发人员)

我的答案:

是否应该启用任何特定的配置才能使其正常工作?

在Spyder 3.1中,我们添加了numpydoc库以改善某些对象(例如Matplotlib图形和NumPy数组)的完成度.如果数据框补全对您不起作用(对我而言),请在Github上的问题跟踪器中打开一个问题,以跟踪并解决此问题.

有人可以在自动补全方面说明什么是有效的,什么是无效的(例如,哪些特定模块在编辑器中起作用,哪些不起作用?)

最困难的部分是当对象是由用C/C ++/Fortran开发的功能或方法而不是用Python开发的对象时,完成定义的.我是说

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

正如我所说,这现在应该适用于数组,图形和数据框,但不适用于所有库(并且大多数科学Python库都是在C/C ++/Fortran中创建的,并且为了提高速度而封装在Python中).

问题是我们使用的完成库(Rope和Jedi)不能很好地处理这种情况,因为array(例如)不能以静态方式进行内省(即,不运行涉及它的代码) ).因此,我们必须采取一些技巧,例如分析array的文档字符串以查看其返回类型并进行内部检查.

在使用Spyder自动完成时,编辑器和Ipython控制台之间的区别在技术方面有哪些?

最重要的区别是,在IPython控制台中,您必须必须运行代码,然后才能获得有关代码的完成信息.例如,请在新的IPython控制台中运行此程序

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

,您会看到它不会返回您Da的任何补全信息(显然它应该返回Dataframe).

但是,经过评估,获得完成非常简单.您可以简单地运行

dir(pd)

获得它们(这是IPython在内部所做的事情).

另一方面,Spyder的编辑器没有可在其中运行代码的控制台,因此它必须通过在代码中运行静态分析工具(例如Jedi和Rope)来完成操作.正如我所说,他们会在不运行代码的情况下自省代码.尽管它们对于纯Python代码非常有效,但它们具有我上面针对编译后的库所述的问题.

尝试评估您在编辑器中的代码以获取完成通常不是一个好主意,因为:

  1. 不一定总是有效的Python代码.例如,假设您在某个地方留下了一个未封闭的括号,但是您想在其他位置获得补全.应该没有问题,对吧?

  2. 这可能涉及到非常昂贵的计算(例如,在数据框中加载巨大的CSV),因此每次都需要对它进行评估以获得完成(这是必须的,因为每次您要求完成时您的代码都不同)瞬间消耗掉所有的RAM.

很高兴知道为什么Rodeo(另一个IDE)中的自动补全效果更好

上次我检查(几年前)时,Rodeo评估了您的代码以获取完成.但是,我们将看看他们现在在做什么,看看是否可以改善完工机制.

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.

解决方案

(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.

这篇关于为什么Spyder 3.1中的自动完成选项在编辑器中不能完全起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆