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

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

问题描述

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

所以问题是三重的.首先,是否应该启用任何特定配置才能使其正常工作?我不这么认为,因为花了一些时间在谷歌上搜索,但只是想确定一下.其次,有人可以说明在自动完成方面什么有效,什么无效的官方说法是什么(例如,哪些特定模块在编辑器中起作用,哪些不起作用?).最后,编辑器和Ipython控制台在用Spyder自动补全的性能上有哪些技术方面的差异?我读了一些关于绝地 vs. PsychoPy 模块的内容,所以很好奇(但是,请记住,虽然我有科学经验,但我对计算相对较新,所以对于受过教育但不是专家的人来说,请保持它相当简单).

更新:作为一个附带问题,很高兴知道为什么 Rodeo(另一个 IDE)中的自动完成功能更好.与 Spyder 相比,它更新,整体选项更少,但自动完成功能在编辑器中完美运行.

解决方案

(Spyder 开发人员在此)

我的回答:

<块引用>

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

在 Spyder 3.1 中,我们添加了 numpydoc 库以改进某些对象(如 Matplotlib 图形和 NumPy 数组)的完成.如果 Dataframe 完成对您不起作用(它们对我有用),请在 Github 上的问题跟踪器中打开一个问题以跟踪和解决此问题.

<块引用>

有人能说出关于自动完成方面哪些有效哪些无效的官方说法(例如,哪些特定模块在编辑器中有效,哪些无效?)

当对象是由在 C/C++/Fortran 而非 Python 中开发的函数或方法生成时,最困难的部分是完成定义.我的意思是,诸如

将 numpy 导入为 npa = np.array([])a.<TAB>

正如我所说,这现在应该适用于数组、图形和数据框,但它不适用于所有库(大多数科学 Python 库是用 C/C++/Fortran 创建并用 Python 包装以提高速度).

问题是我们使用的补全库(Rope 和 Jedi)不能很好地处理这种情况,因为 array(例如)不能以静态方式内省(即无需运行涉及它的代码).所以我们不得不求助于诸如分析 array 的文档字符串之类的技巧来查看它的返回类型并反省它.

<块引用>

编辑器和 Ipython 控制台在使用 Spyder 自动完成的性能方面的差异有哪些技术方面?

最重要的区别在于,在 IPython 控制台中,您必须运行您的代码,然后才能完成它.例如,请在新的 IPython 控制台中运行它

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

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

但是,在评估之后,获得完成非常简单.你可以简单地运行

dir(pd)

获取它们(这就是 IPython 本质上在内部所做的).

另一方面,Spyder 的编辑器没有用于运行代码的控制台,因此它必须通过在代码中运行静态分析工具(如 Jedi 和 Rope)来完成.正如我所说,他们会在不运行代码的情况下内省您的代码.虽然它们对纯 Python 代码工作得很好,但它们存在我在上面描述的编译库的问题.

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

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

  2. 它可能涉及非常昂贵的计算(例如在 Dataframe 中加载一个巨大的 CSV),因此每次都对其进行评估以获得完成(这是必须的,因为您每次要求完成时您的代码都不同)可能一眨眼就消耗你所有的内存.

<块引用>

很高兴知道为什么 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天全站免登陆