简体   繁体   English

使用ipython在pycharm中获得REAL代码完成

[英]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. 许多python IDE都提供代码完成(代码洞察),PyCharm是其中一个IDE。 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.' 在'm'之后击中CTRL空间。 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. 不会给我任何代码完成, - 无论我多么难以点击它;)..我想这是因为IDE必须进行类型推断才能知道变量'm'的类型,而且这不是'在动态编程语言领域中是微不足道的。

Now, PyCharm comes with a setting called "Collect run-time types information for code insight", which indeed sounds promising. 现在,PyCharm带有一个名为“收集代码洞察的运行时类型信息”的设置,这听起来确实很有希望。 However, it doesn't seem to fix the problem mentioned above.. I am still not able to get code-completion on the variable 'm'. 但是,它似乎没有解决上面提到的问题..我仍然无法在变量'm'上获得代码完成。

Thus far, I have only found one way to get code-completion on variables in PyCharm: 到目前为止,我只找到了一种在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. 在这个例子中,我可以在'm。'之后按CTRL空格时获得代码完成,这是因为我通过使用docstring指定变量的类型来帮助IDE。 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?) IPython救援..(也许?)

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'. 现在,如果我们在linux-terminal中启动IPython并输入第一段代码,我们将能够一直获得代码完成,即使是变量'm'。 (where the code-completion in IPython is achieved by pressing TAB instead of CTRL-space).. (通过按TAB而不是CTRL空格来实现IPython中的代码完成)

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... 我对IPython没有多少经验,但我相信我听说过IPython不断在循环中执行代码或类似的东西......

I am thinking that it should be possible to use IPython to achieve REAL code-completion on all variables in the editor of PyCharm.... 我认为应该可以使用IPython来实现PyCharm编辑器中所有变量的REAL代码完成....

Is there a way to setup PyCharm to use IPython for code-completion? 有没有办法设置PyCharm使用IPython进行代码完成?

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... 请注意,我不满意将代码发送到终端窗口/控制台,或类似的东西,我想在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... 在Pycharm Ubuntu中看过像这样的问题添加ipython作为解释器 ,但它似乎是在控制台中使用IPython,而不是在编辑器中......在IDE中也有很多关于代码完成的问题,但他们似乎都有与PyCharm相同的令人不满意的代码完成程度......

My setup 我的设置

  • OS: Debian testing 操作系统:Debian测试
  • python: Python3 and IPython3 python:Python3和IPython3
  • IDE: Pycharm 3.0.2 professional edition IDE:Pycharm 3.0.2专业版

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) 它无法分辨出函数的返回值(实际运行脚本没有,这就是为什么ipython知道它是什么(它实际上运行了代码并收回了它可以反省的对象)

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)

To achieve iPython like functionality in pycharm, you can do it two ways: 要在pycharm中实现类似iPython的功能,您可以通过两种方式实现:

  1. setup a breakpoint, and debug your code. 设置断点,并调试代码。 when it reaches the breakpoint, go to the console tab right below the editor, and launch a command line by clicking the button that says show command line 当它到达断点时,转到编辑器正下方的控制台选项卡,然后单击显示命令行的按钮启动命令行

  2. Launch a python command line from your console (without running debugger), by clicking on Tools, Run Python console 通过单击Tools,Run Python console,从控制台启动python命令行(不运行调试器)

I had the same question, I found the answer here: https://www.jetbrains.com/pycharm/help/using-ipython-notebook-with-pycharm.html 我有同样的问题,我在这里找到答案: 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. 您需要做的是在项目工具窗口中创建一个ipython笔记本。 This is a file with the extension '.ipynb'. 这是一个扩展名为“.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. 右键单击要放置文件的目录,选择“新建 - >文件”,在对话框中输入文件名,并提供文件扩展名.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. 打开笔记本文件,当您开始在窗口中键入内容时,会出现一个下拉窗口,其中包含命名空间中的对象以及以已键入的字母开头的所有命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM