Search code examples
pythonvisual-studio-codeautocomplete

Vscode keras intellisense(suggestion) not working properly


Intellisense works fine on importing phrase

image1

But when it comes with chaining method, it shows different suggestions

image2

Python & Pylance extensions are installed.


Solution

  • From this issue on github try adding this to the bottom of your tensorflow/__init__.py (in .venv/Lib/site-packages/tensorflow for me)

    # Explicitly import lazy-loaded modules to support autocompletion.
    # pylint: disable=g-import-not-at-top
    if _typing.TYPE_CHECKING:
      from tensorflow_estimator.python.estimator.api._v2 import estimator as estimator
      from keras.api._v2 import keras
      from keras.api._v2.keras import losses
      from keras.api._v2.keras import metrics
      from keras.api._v2.keras import optimizers
      from keras.api._v2.keras import initializers
    # pylint: enable=g-import-not-at-top
    

    The problem is because keras is a special class that enables lazy loading and not a normal module.

    Edit: With updates to tf, vscode, or something else I'm not having this issue and don't need to use the above fix anymore. I just have to use keras = tf.keras instead of from tensorflow import keras and I have Intellisense working now.