Skip to content

PyCharm doesn't resolve anything under tensorflow.keras #53144

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

Closed
markorakita opened this issue Nov 20, 2021 · 67 comments
Closed

PyCharm doesn't resolve anything under tensorflow.keras #53144

markorakita opened this issue Nov 20, 2021 · 67 comments
Assignees
Labels
comp:keras Keras related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.7 Issues related to TF 2.7.0 type:bug Bug

Comments

@markorakita
Copy link

System information

  • OS Platform and Distribution: Windows 10
  • TensorFlow installed from: binary
  • TensorFlow version: 2.7.0
  • Python version: 3.9
  • Installed using: Conda
  • CUDA/cuDNN version: 11.2/8.1

Describe the problem
I've upgraded tensorflow from 2.5 to 2.7 and now PyCharm doesn't resolve anything under tensorflow.keras.

keras-no-autocompletion

Other modules of tensorflow work, it's only keras that's problematic. I believe this has something to do with the change in TF 2.6 where keras has been split into a separate PIP package.

People have also been reporting this problem to JetBrains (PyCharm developers): https://youtrack.jetbrains.com/issue/PY-50318

Provide the exact sequence of commands / steps that you executed before running into the problem

  1. Create the conda environment:
    ~ conda create --name tensorflow27
  2. Activate it:
    ~ conda activate tensorflow27
  3. Install Python:
    ~ conda install python=3.9
  4. Install TensorFlow:
    ~ pip install tensorflow
  5. Create new project in PyCharm
  6. Write this code:
import tensorflow as tf
tf.keras.preprocessing.image_dataset_from_directory()
  1. When you move mouse to image_dataset_from_directory function there will be no autocomplete.
  2. Writing tf.keras. yields no modules/functions

Any other info / logs
/

@markorakita markorakita added the type:build/install Build and install issues label Nov 20, 2021
@mohantym mohantym added TF 2.7 Issues related to TF 2.7.0 comp:keras Keras related issues type:bug Bug subtype:windows Windows Build/Installation Issues and removed comp:keras Keras related issues type:bug Bug labels Nov 21, 2021
@mohantym
Copy link
Contributor

Hi @sanatmpa1! Could you please look at this issue?

@mohantym mohantym assigned sanatmpa1 and unassigned mohantym Nov 22, 2021
@sanatmpa1
Copy link

@markorakita,

Can you clarify if you mean that autocomplete is not working in Pycharm, or you're not even able to load the modules/functions in tf.keras?

@sanatmpa1 sanatmpa1 added the stat:awaiting response Status - Awaiting response from author label Nov 23, 2021
@google-ml-butler
Copy link

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

@google-ml-butler google-ml-butler bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Nov 30, 2021
@markorakita
Copy link
Author

Sorry I thought I replied to this.

You can write code and run it and it works, but you don't have autocomplete nor you can go to declaration of any keras function. It's like writing code in notepad :)

@google-ml-butler google-ml-butler bot removed stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author labels Nov 30, 2021
@cpuimage
Copy link

cpuimage commented Dec 3, 2021

hi, @markorakita
It's a naming conflict issue.
try modify the file site-packages/tensorflow/__init__.py near line 387.

before:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

after:

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", keras)

it work for me.

@sanatmpa1
Copy link

@cpuimage,

Thanks for sharing your workaround.

@markorakita,

Can you try as per the suggestion by @cpuimage and let us know if it helps in resolving the auto completion problem?

@sanatmpa1 sanatmpa1 added stat:awaiting response Status - Awaiting response from author comp:keras Keras related issues type:bug Bug and removed type:build/install Build and install issues subtype:windows Windows Build/Installation Issues labels Dec 3, 2021
@markorakita
Copy link
Author

@cpuimage Thank you for finding the cause of the problem in the source code!

@sanatmpa1 I've tried the workaround and it works, but I don't think it's the complete fix since some of the keras classes are still not visible. For example tensorflow.keras.layers.CenterCrop and tensorflow.keras.layers.Rescaling layers cannot be resolved, this gets highlighted in red with the message "Cannot find reference 'CenterCrop' in __init__.py":
from tensorflow.keras.layers import CenterCrop

I hope someone in your team could take a deeper look and apply the proper fix for the next tensorflow update?

@markorakita
Copy link
Author

@cpuimage Thanks, they can be found in that path but that is the old path, in TF 2.7 they were moved from experimental to tensorflow.keras.layers (see API docs) and those new paths should work in IDE. Maybe the same bug that is in site-packages/tensorflow/__init__.py is also in site-packages/tensorflow/keras/__init__.py

These are just the classes that I noticed at a first glance that cannot be resolved, but there might be more, somebody with domain knowledge should really look into this and devise full and proper fix.

@cpuimage
Copy link

cpuimage commented Dec 4, 2021

@markorakita Everything is stirred up, for the time being, only part of it can be solved first, and I hope that someone will solve the problem as soon as possible.

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Dec 6, 2021
@birjinia
Copy link

hi, @markorakita It's a naming conflict issue. try modify the file site-packages/tensorflow/__init__.py near line 387.

before:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

after:

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", keras)

it work for me.

This solution worked for me. Thanks for sharing it.

@ashwin8121
Copy link

Just copy the folder keras from site-packages/tensorflow/python to site-packages/tensorflow. And the auto completion will start working perfectly

@King-Of-Knights
Copy link

Just copy the folder keras from site-packages/tensorflow/python to site-packages/tensorflow. And the auto completion will start working perfectly

Thanks! It is a simple and prefect way!

@xiaolv7
Copy link

xiaolv7 commented Jun 13, 2023

Just copy the folder keras from site-packages/tensorflow/python to site-packages/tensorflow. And the auto completion will start working perfectly

Thank you for your idea! However, it only works for some. It looks like site-packages/tensorflow/python/keras is different from site-packages/keras.

After applying your method, I have the following results:
image
image

set_random_seed is not resolved when calling from the copied folder

@romerojhh
Copy link

romerojhh commented Jul 13, 2023

To everyone that's still couldn't make it work. Try this, I just tweaked the popular solution on this thread:

modify the file site-packages/tensorflow/__init__.py near line 387
From:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

To:

Note that I used _keras instead of keras

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  _keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", _keras)

And then make sure to copy folder
/site-packages/keras/ into /site-packages/tensorflow/

After all of that, invalidate and restart your IDE

This works on my setup using Conda

@dhruv1710
Copy link

It worked on my anaconda also! . Thanks for all your help code completion and that mini documentation is very important for productivity.

@ChienKangLu
Copy link

ChienKangLu commented Jul 28, 2023

To everyone that's still couldn't make it work. Try this, I just tweaked the popular solution on this thread:

modify the file site-packages/tensorflow/__init__.py near line 387 From:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

To:

Note that I used _keras instead of keras

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  _keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", _keras)

And then make sure to copy folder /site-packages/keras/ into /site-packages/tensorflow/

After all of that, invalidate and restart your IDE

This works on my setup using Conda

It worked perfectly on PyCharm IDE with TensorFlow 2.13.0! Finally from tensorflow.keras.layers import Dense, Flatten, Conv2D is successfully recognized by IDE.

With a small change, instead of copy folder /site-packages/keras/ into /site-packages/tensorflow/, I use symbolic link ln -s <full-path-to-site-package>/site-packages/keras <full-path-to-site-package>/tensorflow/keras

@chottuthejimmy
Copy link

To everyone that's still couldn't make it work. Try this, I just tweaked the popular solution on this thread:

modify the file site-packages/tensorflow/__init__.py near line 387 From:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

To:

Note that I used _keras instead of keras

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  _keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", _keras)

And then make sure to copy folder /site-packages/keras/ into /site-packages/tensorflow/

After all of that, invalidate and restart your IDE

This works on my setup using Conda

I am facing this super weird issue.

I am on version 2.16.1 First of my site-packages/tensorflow/__init__.py does not contain any such method.
image
And this is how the file looks like near line 387.

I still copied folder /site-packages/keras/ into /site-packages/tensorflow/ . But still no luck

Very interesting thing is that I can access the definition of __version__ in VS Code insiders but not on PyCharm.
image
image

At the same time I can go to Dense via from tensorflow.keras.layers import Dense on PyCharm, but not on VS Code insiders, also I am not able to access Input which also has the same parent package of Dense.

And ya, both of them are running on the same global interpreter 3.12.0

One thing I figured out was PyCharm was saving these packages at its own end.
image
Whereas, VS Code insiders was doing it from the /site-packages/tensorflow/keras. I was able to access tensorflow.keras after copying the folder, but not tensorflow.keras.layers.

@HeisenbergKnocks
Copy link

HeisenbergKnocks commented Jun 5, 2024

To everyone that's still couldn't make it work. Try this, I just tweaked the popular solution on this thread:
modify the file site-packages/tensorflow/__init__.py near line 387 From:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

To:
Note that I used _keras instead of keras

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  _keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", _keras)

And then make sure to copy folder /site-packages/keras/ into /site-packages/tensorflow/
After all of that, invalidate and restart your IDE
This works on my setup using Conda

I am facing this super weird issue.

I am on version 2.16.1 First of my site-packages/tensorflow/__init__.py does not contain any such method. image And this is how the file looks like near line 387.

I still copied folder /site-packages/keras/ into /site-packages/tensorflow/ . But still no luck

Very interesting thing is that I can access the definition of __version__ in VS Code insiders but not on PyCharm. image image

At the same time I can go to Dense via from tensorflow.keras.layers import Dense on PyCharm, but not on VS Code insiders, also I am not able to access Input which also has the same parent package of Dense.

And ya, both of them are running on the same global interpreter 3.12.0

One thing I figured out was PyCharm was saving these packages at its own end. image Whereas, VS Code insiders was doing it from the /site-packages/tensorflow/keras. I was able to access tensorflow.keras after copying the folder, but not tensorflow.keras.layers.

I have the same problem. It looks like they've changed the code so the old workarounds are no longer relevant. Has anybody found a fix for the newest versions of Tensorflow?

@aimfeld
Copy link

aimfeld commented Jun 7, 2024

I have the same problem. It looks like they've changed the code so the old workarounds are no longer relevant. Has anybody found a fix for the newest versions of Tensorflow?

Same issue here with tf 2.16.1

@jbed94
Copy link

jbed94 commented Jun 7, 2024

So far I haven't seen anyone propose this solution, so I'm passing it on. If PyCharm does not suggest the code to you, but the code itself executes (i.e. if you run the code, e.g. tf.keras.layers.LayerNormalization, which the IDE does not suggest, executes correctly), you can install the package:

pip install types-tensorflow

which contains "stubs" compatible with the corresponding versions of TensorFlow. If you use TensorFlow 2.16.x, you can install, for example, types-tensorflow==2.16.0.20240606 (see which version of TensorFlow these definitions are for).

pip install types-tensorflow==2.16.0.20240606

PyCharm, after refreshing the indexes, should correctly suggest classes contained in TensorFlow, unfortunately it will not suggest documentation, but it always makes writing code easier.

@aimfeld
Copy link

aimfeld commented Jun 7, 2024

So far I haven't seen anyone propose this solution, so I'm passing it on. If PyCharm does not suggest the code to you, but the code itself executes (i.e. if you run the code, e.g. tf.keras.layers.LayerNormalization, which the IDE does not suggest, executes correctly), you can install the package:

pip install types-tensorflow

which contains "stubs" compatible with the corresponding versions of TensorFlow. If you use TensorFlow 2.16.x, you can install, for example, types-tensorflow==2.16.0.20240606 (see which version of TensorFlow these definitions are for).

pip install types-tensorflow==2.16.0.20240606

PyCharm, after refreshing the indexes, should correctly suggest classes contained in TensorFlow, unfortunately it will not suggest documentation, but it always makes writing code easier.

The stubs are nice, but documentation is what I need most. I decided to downgrade to 2.12.1 to get documentation working again. Now code is linked correctly and documentation works, but GPU support is broken, because I use Cuda 12.3 which requires a newer tensorflow version.

@steveepreston
Copy link

steveepreston commented Aug 28, 2024

this issue still exist in PyCharm 2024.2 with TensorFlow 2.16.1

@sana33
Copy link

sana33 commented Sep 15, 2024

hi, @markorakita It's a naming conflict issue. try modify the file site-packages/tensorflow/__init__.py near line 387.

before:

_keras_module = "keras.api._v2.keras"
keras = _LazyLoader("keras", globals(), _keras_module)
_module_dir = _module_util.get_parent_dir_for_name(_keras_module)
if _module_dir:
  _current_module.__path__ = [_module_dir] + _current_module.__path__
setattr(_current_module, "keras", keras)

after:

import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _keras_module = "keras.api._v2.keras"
  keras = _LazyLoader("keras", globals(), _keras_module)
  _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
  if _module_dir:
    _current_module.__path__ = [_module_dir] + _current_module.__path__
  setattr(_current_module, "keras", keras)

it work for me.

I tried this wih Python 3.9 and tensorflow 2.11, though nothing changed!?

@jithinvijayan
Copy link

Just copy the folder keras from site-packages/tensorflow/python to site-packages/tensorflow. And the auto completion will start working perfectly

This worked for me as well..

@steveepreston
Copy link

This worked for me as well

Works but that is a dirty way, and I think it need to be repeated after each update
Should fixed on source side

@steveepreston
Copy link

steveepreston commented Oct 20, 2024

Since this issue is a duplicate as #56231, I am going to close this one, so that everyone can track on same issue.

@qlzh727 Both issues are closed and problem is not fixed yet
Please re-open this

@qlzh727
Copy link
Member

qlzh727 commented Oct 20, 2024

Sorry for the very late reply. Both me and haifeng has moved to some new project for sometime. Let me cc @mattdangerw and @hertschuh here.

@qlzh727 qlzh727 assigned hertschuh and mattdangerw and unassigned qlzh727 and haifeng-jin Oct 20, 2024
@steveepreston
Copy link

@qlzh727 Thank you!

@simond07
Copy link

Same problem in 2.18.0.

@hertschuh
Copy link
Member

Hi,

as of Tensorflow 2.16, the preferred way of using keras is to import keras and use it directly:

import keras

inputs = keras.Input(...)

tf.keras is only provided for backwards compatibility.

My experience is that code completion for keras works in PyCharm with import keras.

@negsi
Copy link

negsi commented Jan 25, 2025

My experience is that code completion for keras works in PyCharm with import keras.

Working like a charm.

Also I am using this:

from keras.api.datasets import mnist
from keras.api.models import Sequential
from keras.api.layers import Dense, Flatten
from keras.api.utils import to_categorical

Image

@gabemorris12
Copy link

Hi,

as of Tensorflow 2.16, the preferred way of using keras is to import keras and use it directly:

import keras

inputs = keras.Input(...)
tf.keras is only provided for backwards compatibility.

My experience is that code completion for keras works in PyCharm with import keras.

I don't like having to resolve to importing directly from keras. Doing from tensorflow.keras.models import Sequential is much cleaner as it enforces the use of the tensorflow backend. Is there still no fix for this in March of 2025?

@hertschuh
Copy link
Member

@gabemorris12

Doing from tensorflow.keras.models import Sequential is much cleaner as it enforces the use of the tensorflow backend.

The use of tensorflow.keras vs. import keras does not enforce the use of the TensorFlow backend.

import os
os.environ["KERAS_BACKEND"] = "jax"

import tensorflow as tf

type(tf.keras.ops.ones(()))

jaxlib.xla_extension.ArrayImpl

@kpilkk
Copy link

kpilkk commented Mar 29, 2025

Has any solution worked? I'm using tensorflow version 2.17.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.7 Issues related to TF 2.7.0 type:bug Bug
Projects
None yet
Development

No branches or pull requests