IntelliSense在VSCode交互式Python代码文件中未列出成员。

huangapple go评论70阅读模式
英文:

IntelliSense not listing members with VSCode Interactive Python code files

问题

In VSCode中,我在Python交互模式下使用。我在一个窗格中打开了一个Python脚本,并在另一个窗格中执行代码。在交互式窗格中,智能感知正常工作 - 当我在对象后键入“.”时,它会列出所有对象的成员。但是当我在脚本窗格中尝试相同的操作时,它不会列出对象的成员。

示例如下。

在交互式窗格中,智能感知提供了对象的所有成员的完整列表:
交互式窗格自动完成

但是当我在Python脚本中尝试相同的操作时,运行单元格后不会列出对象的成员:
脚本窗格自动完成

我尝试了以下方法来解决此问题:

  • 更新到最新的VSCode,并卸载/重新安装Microsoft的Python扩展(没有安装其他扩展)
  • 在VSCode设置中,确保语言服务器正在使用Pylance
  • 我正在运行在一个conda虚拟环境中,并且只有一个终端在运行

我的解决方法是,当我想要检查一个变量时,我切换到交互式窗格以使用其自动完成功能,然后切换回脚本窗格继续编码。理想情况下,我应该能够在脚本窗格中看到完整的自动完成。

如果有办法解决这个问题,请提供建议。

编辑: 下面是最小示例。智能感知问题似乎特定于下面创建的Tensorflow/Keras对象history,因此我正在导入Tensorflow以重现问题:

# %% 在Python交互窗格中执行此代码单元格
import tensorflow as tf
model = tf.keras.Sequential([tf.keras.Input(1), tf.keras.layers.Dense(1)])
model.compile(loss='mse', optimizer='sgd')
history = model.fit([0, 1], [0, 1])

运行此代码后,当我键入history.history加上触发字符.)时,智能感知应该弹出一个小框,列出history对象的所有成员(例如history.epochhistory.params)。

在交互式窗格中,智能感知正常工作:从交互式窗格调用的智能感知

但是在脚本窗格中触发后,智能感知会显示“没有建议”:运行单元格后,在脚本窗格中触发后,智能感知不会列出对象的成员

英文:

In VSCode I use Python Interactive mode. I have a Python script open on one pane, and I execute the code in an interactive terminal open in another pane. IntelliSense works properly in the interactive pane - when I type "." after an object it lists all the object's members. But when I try the same thing in the script pane, it doesn't list the object's members.

Example below.

In the interactive pane, IntelliSense provides a full lists of the object's members:
Interactive pane autocomplete

But when I try the same thing in the Python script, it fails to lists the object's members after the cell has been run:
Script pane autocomplete

I have tried the following things to fix this:

  • Updated to the latest VScode, and uninstalled/reinstalled Microsoft's Python extension (no other extensions installed)
  • In VScode settings, made sure the language server was using Pylance
  • I am running inside a conda virtual environment, and I only have that one terminal running

My work-around is that, when I want to inspect a variable, I switch to the interactive pane in order to use its autocomplete, and then I switch back to the script pane to continue coding. Ideally, I should be able to see the full autocomplete in the script pane.

Please advise if there is a way to remedy this.

Edit: Minimal example below. IntelliSense problem seems specific to the Tensorflow/Keras object called history created below, hence I am importing Tensorflow to reproduce the problem:

#%% Execute this code cell in the Python interactive pane
import tensorflow as tf
model = tf.keras.Sequential([tf.keras.Input(1), tf.keras.layers.Dense(1)])
model.compile(loss='mse', optimizer='sgd')
history = model.fit([0, 1], [0, 1])

After running this, when I type history. (history plus trigger character .), IntelliSense should pop-up a small box listing all the members of the history object (e.g. history.epoch, history.params).

IntelliSense works in the Interactive pane: IntelliSense invoked from Interactive pane

But IntelliSense fails with "No suggestions." in the script pane: After running cell, IntelliSense doesn't lists object's members when triggered in script pane

答案1

得分: 0

The intellisense provided by Python and Pylance extensions is static type checking.

Obviously, the intellisense you need must be obtained after running the model, which is not achievable for intellisense.

The solution you have already mentioned is to use an interactive window to run code cells to obtain intellisense. Another method is similar to this one, you can use .ipynb file which is similar to interactive window. You can get the prompt after running the code cell.

IntelliSense在VSCode交互式Python代码文件中未列出成员。

英文:

The intellisense provided by Python and Pylance extensions is static type checking.

Obviously, the intellisense you need must be obtained after running the model, which is not achievable for intellisense.

The solution you have already mentioned is to use an interactive window to run code cells to obtain intellisense. Another method is similar to this one, you can use .ipynb file which is similar to interactive window. You can get the prompt after running the code cell.

IntelliSense在VSCode交互式Python代码文件中未列出成员。

huangapple
  • 本文由 发表于 2023年5月14日 18:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定