tensorflow: IDE cannot resolve module tf.keras

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No.
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 18.10
  • TensorFlow installed from (source or binary): binary (pip)
  • TensorFlow version (use command below): 2.0_alpha
  • Python version: 3.6

Describe the current behavior import tensorflow as tf then pyCharm cannot resolve module tf.keras and report an error: Cannot find reference 'keras' in '__init__.py' But when running program, everything works well.

Describe the expected behavior tf.keras imported successfully with autocomplete of pyCharm.

Code to reproduce the issue import tensorflow as tf

Other info / logs It seems that there is no import command for keras module in __init_.py of tensorflow package. When I added from tensorflow.python import keras to __init__.pymanually, everything work well. Maybe there are some problem for package importing after keras was moved from _api to python.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 47
  • Comments: 71 (17 by maintainers)

Most upvoted comments

Someone should reopen this since neither tensorflow-gpu-2.0.0rc0 nor tf-nightly-gpu-2.0-preview enables IDE to resolve tf.keras. I don’t really understand why tf2 still uses such a complex importing method. tf1 has reinvented logging and argparse, now tf2 is still reinventing import.

Yes, this is not a bug from pyCharm but tensorflow itself. The bug is caused by missing tensorflow.python import keras in __init__.py of tensorflow package.

🤕 I just upgraded to 2.0rc … and my code is full of yellows Earlier for 2.0b I used to use the hack for keras and other stuff

import tensorflow.python as tfpy
tfpy.keras

Now that doesn’t work … but nonetheless code runs …

Same issue with the new tensorflow==2.0.0-rc0 release. If in beta release I could just use the tensorflow.python.keras workaround, now keras.layers module doesn’t see the layers classes (Dense, Embedding, etc) but packages in which every layer is grouped. If I import them separately from each module the call of a layer over an input doesn’t seem to work. This behaviour might be only a problem in PyCharm (in cmd works just fine) but as the majority of developers are using PyCharm as IDE it would be nice to not struggle to use tensorflow library. In addition the tensorflow namespace doesn’t contain anything at import, all the packages are grouped in tensorflow_core. I understand that the backend grouping of modules must be done that way, the problem is that the importing step doesn’t match the guides and tutorials on the official site and makes everything hard to use.

If it is any workaround on this issue until a fix is released (from tensorflow team or PyCharm) I would be grateful to use it.

For folks hitting this issue, the fix has merged. Please try the latest 2.0 nightly and let us know if it is not yet resolved.

why is this issue closed while it’s still not fixed?

With PyCharm 2019.3 (Early Access Preview or later) the issue disappears.

I am also still having this issue running PyCharm 2019.2 with Python 3.7 and tf-2.0.0-beta1. If I run from tensorflow import keras in a Python interpreter it works fine, but tf seems to have done something that renders PyCharm unable to find many of its submodules, which makes it difficult to work with due to the lack of autocomplete and false import warnings.

I would say the original issue is resolved since it talks about import tensorflow as tf specifically. However, from tensorflow.keras import losses and similar are not resolved yet. May be it is better to open a new issue?

I had the same problem and it bugged me for a good couple of hours 😦 . I used from tensorflow.python.keras import *** for importing the libraries and it worked fine for me.

Thanks for the temporary solution! It had been working for a while; but now with 2.0.0-dev20190805, even this solution does not work: no way to make PyCharm(2019.2) find the correct reference inside TensorFlow.

Interesting , @annarev when using import tensorflow as tf then tf.keras.* auto completes But from tensorflow.keras import layers will still not “work”

seems this issue hasn’t been resolved yet, any idea to fix it temporarily? thx!

At least for code completion to find it, a quick and dirty fix is to throw from tensorflow.python import keras at the top of <python_site_packages_dir>/tensorflow/__init__.py In pycharm you can get there quickly by just going to the declaration of tensorflow (ctrl+b default)

keras is imported dynamically in the __init__.py file. I am not familiar with the way pyCharm looks for autocomplete symbols, but I will see if there is any workaround (may be adding ‘keras’ to __all__).

As we discussed here, It’s a serious problem which has been created by tensorflow implementation. Could you please kindly consider it to resolve as soon as possible for next versions?

tensorflow-version:‘2.0.0’ CUDA:10.0 Cudnn:7.6.4 (I have tried update tensorflow to 2.2.0rc0 but I get import error:Failed to load the native TensorFlow runtime )

pycharm version: 2019.2.4 -> 2019.3.4

Problem solved! thanks @annarev and @fzyzcjy

At least with PyCharm IDE (EAP release), it will work. I have tried it and it works. Check here.

Avoid importing tensorflow_core if you want to keep code compatible.

Does not work with the stable version of tensorflow.keras(stable release 1st October 2019)!!!

I added an update on this issue on the freshly opened #31973 : #31973 (comment)

TL;DR: we are working on this but the fix is quite complex

Thanks for your great comment. We seriously need this feature; otherwise, pycharm will be completely useless!!!

I added an update on this issue on the freshly opened #31973 : https://github.com/tensorflow/tensorflow/issues/31973#issuecomment-524928419

TL;DR: we are working on this but the fix is quite complex

This occurs also in 1.14, and renders PyCharm completely useless with TensorFlow. It used to be OK, what happened?

I had the same problem and it bugged me for a good couple of hours 😦 . I used from tensorflow.python.keras import *** for importing the libraries and it worked fine for me.

There is a difference between the two: tf.keras.* uses the attribute keras of module imported as tf but import tensorflow.keras looks for module keras in package tensorflow.

still not solved for TF 2.1 (Pylance 2020.9.6)

I believe this is unresolved as the current working suggestion is a temporary workaround

On Pycharm, instead of from tensorflow import keras, try to use from tensorflow.python import keras will help autocomplete. I tested with Tensorlow 2.0 alpha and it is working.

Problem still existing in the lastest tensorflow-gpu-2.0.0a0, Please fix this.

Insight normal in the terminal, but not in pycharm

I will reopen the bug. I suspect getting import tensorflow.keras would be much harder to fix. Especially, if it depends on having keras under tensorflow directory (which is not possible since these are installed as separate pip packages and live in different directories).

@knaydenov none of your solutions is pythonic. It should work like @tensorflow import keras in its examples.