Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import "tensorflow.keras" could not be resolved #3753

Closed
otavio-silva opened this issue Dec 12, 2022 · 6 comments
Closed

Import "tensorflow.keras" could not be resolved #3753

otavio-silva opened this issue Dec 12, 2022 · 6 comments

Comments

@otavio-silva
Copy link

Environment data

  • Language Server version: 2022.12.20
  • OS and version: win32 x64
  • Python version (and distribution if applicable, e.g. Anaconda):
  • python.analysis.indexing: true
  • python.analysis.typeCheckingMode: basic

Code Snippet

import tensorflow as tf
from tensorflow.keras import activations, initializers, layers, losses, metrics, models, optimizers, regularizers

Repro Steps

  1. Open VSCode
  2. Create somename.py
  3. Type code snippet

Expected behavior

Imports should be resolved

Actual behavior

Import "tensorflow.keras" could not be resolvedPylance[reportMissingImports](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportMissingImports)

Logs

pylance.log

@rchiodo
Copy link
Contributor

rchiodo commented Dec 12, 2022

I think tensorflow.keras is a special case. Its location is determined dynamically as far as I can tell, so pylance cannot figure out where it is.

The tensorflow.keras module is found here for me:

.venv/lib/python3.10/site-packages/keras/

whereas pylance would look for it here:

.venv/lib/python3.10/site-packages/tensorflow/keras

@bschnurr has advice on how to get pylance to see where it is:

#3387 (comment)

@rchiodo rchiodo closed this as completed Dec 12, 2022
@otavio-silva
Copy link
Author

@rchiodo the comment you linked does nothing, it says that anything name under the tf.keras is of type Any and does not gives suggestions, see:
image

@bschnurr
Copy link
Member

whats your version of tensorflow? try the latest

@otavio-silva
Copy link
Author

I installed version 2.10 using mambaforge on WSL2 Ubuntu, it works now.
And it seems that TensorFlow is going for Linux-only, this is the way then.

@AnuraagaNath
Copy link

Try keras only without using tensorflow before it

@mnothman
Copy link

mnothman commented Jan 3, 2025

With keras3 there is a new way to import, the documentation has examples for layers, models, optimizers, applications
https://keras.io/api/

Just do below with no other imports:

import tensorflow as tf
from tensorflow import keras

For Dense you would do

keras.layers.Dense

Example:

x = keras.layers.Dense(128, activation='relu')(x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants