Search code examples
pythonvisual-studio-codeintellisenseinteractivepylance

IntelliSense not listing members with VSCode Interactive Python code files


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


Solution

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

    enter image description here