10000 tensorflow.keras IDE auto-completion fails · Issue #56231 · tensorflow/tensorflow · GitHub
[go: up one dir, main page]

Skip to content

tensorflow.keras IDE auto-completion fails #56231

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
Yuri-Su opened this issue May 24, 2022 · 75 comments
Closed

tensorflow.keras IDE auto-completion fails #56231

Yuri-Su opened this issue May 24, 2022 · 75 comments
Assignees
Labels
comp:keras Keras related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.8 type:bug Bug

Comments

@Yuri-Su
Copy link
Yuri-Su commented May 24, 2022
Click to expand!

Issue Type

Bug

Source

binary

Tensorflow Version

tf 2.8 2.9

Custom Code

Yes

OS Platform and Distribution

win mac

Mobile device

No response

Python version

3.10

Bazel version

No response

GCC/Compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current Behaviour?

A bug happened!
tensorflow.keras IDE auto-completion fails.
Using "from tensorflow." does not pop up keras, pycharm will report red but it works.
Also, tf 2.5 is normal.
I see there are other people who have had this problem

Standalone code to reproduce the issue

from tensorflow.keras import layers

Relevant log output

No response

@google-ml-butler google-ml-butler bot added the type:bug Bug label May 24, 2022
@mohantym mohantym assigned tilakrayal and unassigned mohantym May 24, 2022
@tilakrayal tilakrayal added the TF 2.9 Issues found in the TF 2.9 release (or RCs) label May 24, 2022
@tilakrayal
Copy link
Contributor

@Yuri-Su ,
Could you provide the error log which you are facing issue. It helps to debug the issue. 8000 Thank you!

@tilakrayal tilakrayal added the stat:awaiting response Status - Awaiting response from author label May 24, 2022
@Dadealos
Copy link

So... there's no "error log" for code completion failures. As Yuri-Su mentioned, everyone is having the same issue.

Steps to reproduce:

  1. from tensorflow.keras import Model

That's literally it. If you want to rule out other IDEs, configuration issues, etc etc, just go to https://www.tensorflow.org/tutorials/quickstart/advanced

Run the first cell:
image

@Dadealos
Copy link

A side question - does everyone just use Notepad++? This has been a persistent issue with VSCode and Pycharm since at least 2017. I'm baffled that it's not fixed, but I don't understand it well enough to fix it myself.

I would happily just buy a different IDE if there's one that supports code completion and doesn't break on every new TF release.

@Yuri-Su
Copy link
Author
Yuri-Su commented May 25, 2022

A side question - does everyone just use Notepad++? This has been a persistent issue with VSCode and Pycharm since at least 2017. I'm baffled that it's not fixed, but I don't understand it well enough to fix it myself.

I would happily just buy a different IDE if there's one that supports code completion and doesn't break on every new TF release.

image
As far as I know, this problem appeared from 2.6 and lasted until 2.9, if it is not solved, I am willing to use pytorch in the future.

@tilakrayal
Copy link
Contributor

@Yuri-Su ,
I was able to import and execute the code in tensorflow version 2.8. Could you find the gist of it here. Thanks you!

@tilakrayal tilakrayal added TF 2.8 type:support Support issues comp:keras Keras related issues stat:awaiting response Status - Awaiting response from author and removed stat:awaiting response Status - Awaiting response from author type:bug Bug TF 2.9 Issues found in the TF 2.9 release (or RCs) labels May 26, 2022
@Yuri-Su
Copy link
Author
Yuri-Su commented May 26, 2022

@Yuri-Su , I was able to import and execute the code in tensorflow version 2.8. Could you find the gist of it here. Thanks you!

Maybe you misunderstood what I meant. The code will run, but there will be red lines and no code completion

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label May 29, 2022
@cpuimage
Copy link

@Yuri-Su try this:
#53144 (comment)

@Yuri-Su
Copy link
Author
Yuri-Su commented May 31, 2022

@Yuri-Su try this: #53144 (comment)

image
I have made the changes as suggested and cleared the cache, but it still doesn't work.

@cpuimage
Copy link
cpuimage commented May 31, 2022

@Yuri-Su try this:

before:

# 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
# pylint: enable=g-import-not-at-top

after:

# 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
  from keras.api._v2 import keras
# pylint: enable=g-import-not-at-top

@Yuri-Su
Copy link
Author
Yuri-Su commented May 31, 2022

@Yuri-Su try this:

before:

# 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
# pylint: enable=g-import-not-at-top

after:

# 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
  from keras.api._v2 impo
8000
rt keras
# pylint: enable=g-import-not-at-top

image
image
Thank you very much for your help, but still can't solve the problem.
Bye tensorflow, hello pytorch!!!

@cpuimage
Copy link

@Yuri-Su
site-packages\tensorflow\keras is removed in tf 2.8 and tf 2.9

so you can't import it like that.

The temporary solution can only be:

before:
from tensorflow.keras.callbacks import EarlyStopping
after:
from tensorflow.python.keras.callbacks import EarlyStopping

or:
import tensorflow as tf
then
tf.keras.~

@alkatar21
Copy link

This has been a problem for a while now, see #53144. It would be really nice if this would be fixed. It can't be that everyone who uses an IDE and code completion, which should be normal by now, has to hack around in his installation.

@qlzh727
Copy link
Member
qlzh727 commented May 31, 2022

Sorry for the very long wait.

This issue should be addressed by #54104, which is landed on tf-nightly (but didn't included in tf 2.9). I was able to verify this locally with the latest tf-nightly pip package.

Could any of you test with latest tf-nightly on the IDE and see if it fix the issue? Thanks.

@haifeng-jin
Copy link
Contributor

I am closing this issue.
Please reopen if TF 2.13 release still have this problem.

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@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

@brorro
Copy link
brorro commented Jun 28, 2023

It still shows tensorflow.keras could not be resolved in VScode.
Tested in:
TensorFlow version: 2.13.0-rc2
Pylance: v2023.6.31
Python: 3.11

@Hacker1337
Copy link

before:
from tensorflow.keras.callbacks import EarlyStopping
after:
from tensorflow.python.keras.callbacks import EarlyStopping

Be careful with that. It fixed the issue for me. But then I found out that it harmed performance really strong and increased model learning time 12 times....

@bermeitinger-b
Copy link

Use a recent version of tensorflow.

@Darkdragon84
Copy link

@haifeng-jin a change in keras will not solve anything. The problem is in tensorflow and the problem is still there in the latest pypi release 2.13.0 as nothing changed in the lazy import in tensorflow.__init__.py

_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 most IDEs thus can't find tensorflow.keras in during code inspection. @bermeitinger-b which version are you referring to that would change this?

The problem will persist until either

  • tensorflow does a normal from keras.api._v2 import keras (can anyone elaborate why this isn't done?)
    or
  • All IDEs adapt to understand that particular tensorflow specific import syntax.

I don't find it realistic to require all dev tools and IDEs to adapt to that design choice. Imho it would be very beneficial for tensorflow in the future if the tensorflow dev team heeded the requests of hitherto loyal tensorflow users and responded to this somewhat annoying issue.

@kruthi-sb
Copy link

Man, I can't believe this is still an issue 😒

Symlink works fine on Windows, at least as of TF 2.9. Windows users - just open an elevated cmd prompt, go to your site-packages\tensorflow directory and type:

mklink /D keras ..\keras\api_v2\keras

If for whatever reason you can't make a symlink, a junction works just as well:

mklink /J keras C:\fullpath\site-packages\keras\api_v2\keras

This worked for me! thanks

@weidler
Copy link
weidler commented Nov 29, 2023

This issue has resurfaced in PyCharm with TF/Keras 2.15.

@michelkok
Copy link

Not a real solution at all, but I just want to mention that Keras V3 came out yesterday. If you can already use it, then you have no problem with this anymore.

@OlegNovosad
Copy link
OlegNovosad commented Dec 6, 2023

This is so crazy.

PyCharm 2023.2.5 and VSCode 1.84.2 (pylance v2023.11.10) still issue with tensorflow 2.15.0.
I'm starting learning tensorflow, keras and all of that stuff and not being able to see autocompletion and being forced to memorising optimisers, layers, metrics, params etc. is frustrating 😢

@haifeng-jin
Copy link
Contributor

@weidler @OlegNovosad Are you using import keras or from tensorflow import keras?
import keras should always work for autocompletion.

@OlegNovosad
Copy link

@haifeng-jin I'm using import tensorflow as tf and then tf.keras.<whatever>

@weidler
Copy link
weidler commented Dec 11, 2023

@haifeng-jin Also using import tensorflow as tf. Will switch to import keras though, since with Keras 3.0 that seems to be the recommended approach anyways.

@haifeng-jin
Copy link
Contributor

@OlegNovosad @weidler , Thanks for the reply! Unfortunately, I don't think we can do much about the tf.keras import path.

On the Keras side, we have implemented a new mechanism for API exporting, which works for both Keras 2 (since TF 2.13) & 3 for code completion. It creates real directories and files under the import path instead of just adding attributes. So it should work with all code completion tools in IDEs.

A note for new users:
Please import keras directly in order to use the code completion feature.

@lllllllllaa
Copy link
lllllllllaa commented Dec 30, 2023

i made it autocompleting by changing import keras section line 397 in init.py tf 2.15 to:

# Lazy-load Keras v2/3.
import typing as _typing
if _typing.TYPE_CHECKING:
  from keras.api._v2 import keras
else:
  _tf_uses_legacy_keras = (
          _os.environ.get("TF_USE_LEGACY_KERAS", None) in ("true", "True", "1"))
  setattr(_current_module, "keras", _KerasLazyLoader(globals()))
  _module_dir = _module_util.get_parent_dir_for_name("keras._tf_keras.keras")
  _current_module.__path__ = [_module_dir] + _current_module.__path__
  if _tf_uses_legacy_keras:
    _module_dir = _module_util.get_parent_dir_for_name("tf_keras.api._v2.keras")
  else:
    _module_dir = _module_util.get_parent_dir_for_name("keras.api._v2.keras")
  _current_module.__path__ = [_module_dir] + _current_module.__path__

can someone please tell me if it's going to crash

@steveepreston
Copy link
steveepreston commented Aug 28, 2024

This issue still exist in PyCharm 2024.2 with TensorFlow 2.16.1

@olurocks
Copy link

Issue still persists on vscode .... 7 years down the line and no fix

interesting

@steveepreston
Copy link
steveepreston commented Sep 19, 2024

This issue still exist in PyCharm 2024.2.1 with TensorFlow 2.17

I'm wondering why the issue is closed! please re-open it

@yuanxiqd
Copy link
yuanxiqd commented Nov 8, 2024

This issue still exist in VScode 1.95.2 with Tensorflow 2.18.

@steveepreston
Copy link
steveepreston commented Nov 8, 2024

@haifeng-jin @Yuri-Su Please re-open the issue. problem still exists in TensorFlow 2.17 and 2.18

@ThunderCgo
Copy link

TensorFlow's team really really fail me for many times. Changing installation methods multiple times, annoying warnings after running code, and compatibility issues between "import" and IDE

@steveepreston
Copy link
steveepreston commented Dec 7, 2024

@Letronxis Sadly worst thing is that long time issues like this are marked as closed and no one trying to open and follow them

@Roobahist
Copy link

Still unresolved. VSCode 1.99.2 Tensorflow 2.18.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.8 type:bug Bug
Projects
None yet
Development

No branches or pull requests

0