PyCharm cannot import tensorflow.keras

Answered

I am working with the tensorflow-implementation from Keras and I can use it without issues, however, my IDE thinks that the keras submodule in tf does not exist. I am using anaconda where I install tensorflow and all my other libraries. I make sure that I select the right interpreter in PyCharm (all my other obscure libraries are imported without issue) and the base module from tf is imported without any problem (I get autocomplete etc.) but when I import "tensorflow.keras" the IDE complains that it cannot find the reference 'keras'. As said above, executing the files works without problem. Is there a way to tell the PyCharm that there is actually a keras submodule?

PyCharm 2018.3.3 (Community Edition)
Build #PC-183.5153.39, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-43-generic

 

14
30 comments

Hi,

As a first troubleshooting step, can you try to find where the keras module is physically located in your packages directories, and check if this directory is present in the sys.path of your interpreter?

Also, please try to open python console and do the import from there.

0
Avatar
Permanently deleted user

I have the same problem. I use virtualenv and it is in "env" directory. From PyCharm's Python Console it works, though the completion is not as expected.

The code works with the following import:

from tensorflow.keras import backend as K
from tensorflow.keras.layers import Lambda, Input, Flatten
from tensorflow.keras.models import Model

But PyCharm shows "Unresolved reference" error.

My workaround is this:

import tensorflow as tf
keras = tf.keras
K = keras.backend
KL = keras.layers
Lambda, Input, Flatten = KL.Lambda, KL.Input, KL.Flatten
Model = keras.Model

 

1

Hey Adam,

Yes, I can reproduce the issue, but please note that it's expected in some limited cases. The tooltip for that inspection says:

"This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items."

So another workaround would be to disable that inspection.

0
Avatar
Permanently deleted user

Hi Andrey,


That import is working from python console in PyCharm. 

I think yesterday (but sadly not today) my PyCharm was able to resolve this reference.

0
Avatar
Permanently deleted user

I've found workaround. 

Instead of

from tensorflow.keras import layers

do import it as:

from tensorflow._api.v1.keras import layers
0
Avatar
Permanently deleted user

Same error here, cannot autocomplete in code but work fine in python console, the work arounds posted here didn't help. I'm using tensorflow 2.0 btw. 

0
Avatar
Permanently deleted user

In tensorflow 2.0 you can import keras using

`from tensorflow.python import keras`

and it will autocomplete

9
Avatar
Permanently deleted user

I got the same problem using tensorflow 1.13 and the latest PyCharm Pro. Will this be fixed?

1

Dear all,

I'm sincerely sorry for the inconvenience. The issue is known https://youtrack.jetbrains.com/issue/PY-34174, please feel free to vote and leave comments.

1
Avatar
Permanently deleted user

Rendered PyCharm almost obsolete for me unless I can find a workaround. I've ditched the virtual env, installed all the dependencies via pip and am running the model in the terminal. None of the workarounds posted here resolved the problem for me.

1

Pawel Panasewicz provided a good workaround above.

0

Just have started to use PyCharm and gonna stop using it same day... Will this bug be fixed?

 

0

The issue with Tensorflow (https://youtrack.jetbrains.com/issue/PY-34174) should be fixed in the next release of PyCharm (in EAP next week as the developer told me)

0

Unfortunately, the bug has not been fixed.

Using tensorflow-gpu 2.0.0 and the IDE can not resolve tensorflow.python, tensorflow.keras.

 

0

@Niels Schneider

Make sure you are using the latest PyCharm version. The issue should be fixed in 2019.2.1 https://youtrack.jetbrains.com/issue/PY-33034

Note, there is another known and not yet addressed issue with tensorflow 2.0.0rc2 https://youtrack.jetbrains.com/issue/PY-38220

0

@Sergey Karpov

it was work in tf-2.0.0rc2

but... still not work in 2019.2.3 & tensorflow 2.0.0(stable) 

import tensorflow_core.python import keras # also not work

 

 

 

0

Have the same Problem as @YSLEE.

Using 2019.2.3 & import tensorflow_core.python import keras # also not work

0

Please report it to https://youtrack.jetbrains.com/issues/PY attaching a screenshot showing the issue and some code for reproducing.

0

This works for me using Pycharm 2018.1.3 and tensorflow-gpu 2.0.0 from conda. Of course, I set the interpreter to the python binary file of my conda environment: '/home/<username>/anaconda3/envs/<envname>/bin/python'.

from tensorflow_core.python import keras

Tensorflow_core can be found in:  'Project structure' -> 'External Libraries' -> '<Python 3.7.5 ... >' -> 'Remote Libraries' -> 'path to environment's site-packages folder' -> 'tensorflow_core'

 

1

It's November 16 and this problem still exist. None of the suggestions here or online have worked for me. I am using 2019.2.4.

Just gave JetBrains $90 for the Professional Version to SSH into my remote linux system just to not have accurate code completion and debugging using the remote or local interpreter. Perfect!

1
Avatar
Permanently deleted user

I second the comment by Zacchaeus1991. Same situation. Please fix this!

0

Here is a workaround I have been using for PyCharm 2019.2 on Linux.
1. Create a stubs directory under the root of your python environment (you can actually put anywhere you like, I just prefer to keep it together with the environment).
a. In my case this involved the following commands in the terminal (I'm using conda, but this should work for other virtual envs as well (with appropriately modified commands)):
> conda activate MyEnv
> which python
/home/luser/anaconda3/envs/MyEnv/bin/python
> cd /home/luser/anaconda3/envs/MyEnv
> mkdir stubs

2. Find the tensorflow_core directory.
> find `pwd` -type d -name 'tensorflow_core' -print
/home/luser/anaconda3/envs/MyEnv/lib/python3.7/site-packages/tensorflow_core

3. Create a symbolic link called tensorflow, in the stubs directory, linked to the tensorflow_core directory in your environment's site-packages directory.
> ln -s /home/luser/anaconda3/envs/MyEnv/lib/python3.7/site-packages/tensorflow_core /home/luser/anaconda3/envs/MyEnv/stubs/tensorflow

4. In your PyCharm projects, add the stubs directory to your content root.
a. Open "File" | "Settings..."
b. Navigate to "Project: ..." | "Project Structure"
c. Select "+ Add Content Root"
d. Choose the stubs directory you created in the previous steps.
e. Click OK
f. Select "File" | "Invalidate Caches/Restart..." to rebuild the indices.
g. Click "Invalidate and Restart" to rebuild the indices.

1

Here is a Windows 10 version of the workaround for I have been using for PyCharm on Linux. I thought it might help someone who is still learning the ropes.
1. Create a stubs directory under the root of your python environment (you can actually put anywhere you like, I just prefer to keep it together with the environment).
a. In my case this involved the following commands in a Command Prompt (cmd.exe) (I'm using conda, but this should work for other virtual envs as well (with appropriately modified commands)):
> conda activate MyEnv
> where python
C:\Users\luser\AppData\Local\Continuum\anaconda3\envs\MyEnv\python.exe
# NOTE: If more then one path to python.exe is returned, use the topmost one (i.e. the one from your virtual environment)
> cd C:\Users\luser\AppData\Local\Continuum\anaconda3\envs\MyEnv
> mkdir stubs

2. Find the tensorflow_core directory. It will be in the virtual environment (C:\Users\luser\AppData\Local\Continuum\anaconda3\envs\MyEnv) under .\Lib\site-packages\
for example: C:\Users\luser\AppData\Local\Continuum\anaconda3\envs\MyEnv\Lib\site-packages\tensorflow_core

3. Create a symbolic link called tensorflow, in the stubs directory, linked to the tensorflow_core directory in your environment's site-packages directory.
a. NOTE: In Windows 10, one has to be in an administrative command prompt to create a symbolic link.
> mklink /D "C:\Users\luser\AppData\Local\Continuum\anaconda3\envs\MyEnv\stubs\tensorflow" "C:\Users\luser\AppData\Local\Continuum\anaconda3\envs\MyEnv\Lib\site-packages\tensorflow_core"

4. In your PyCharm projects, add the stubs directory to your content root.
a. Open "File" | "Settings..."
b. Navigate to "Project: ..." | "Project Structure"
c. Select "+ Add Content Root"
d. Choose the stubs directory you created in the previous steps.
e. Click OK
f. Select "File" | "Invalidate Caches/Restart..." to rebuild the indices.
g. Click "Invalidate and Restart" to rebuild the indices.

0

This issue is fixed in Pycharm 2019.3.2.

-1
Avatar
Permanently deleted user

 

same problem in PyCharm 2019.3.4...

1
from tensorflow.python.keras import layers
-2

This is still happening.

Any version from 2021 to 2022, Ubuntu or arch, any interpreter 3.7, 3.8, 3.9, 3.10 (Python or Conda) doesn't fix this.

(The example with the backend is just an example, it can't import anything from the keras submodule, or use it with `tensorflow.keras` when importing "only" `tensorflow`.)

 

The import with `.python.` doesn't help (no wrigly lines but no Autocomplete and runtime breaks)

import tensorflow.python.keras as keras
l = keras.layers.LayerNormalization()
>>> AttributeError: module 'tensorflow.python.keras.layers' has no attribute 'LayerNormalization'

 

0

The bug that keeps coming back...

I still use PyCharm for a lot of development, but I've switched to VSCode for tensorflow projects because Jetbrains can't seem to get this right.

0

Hello, 

Please vote for the relevant bug report https://youtrack.jetbrains.com/issue/PY-53599/tensorflow-keras-subpackages-are-unresolved to be updated on resolution. 

 

I apologize for the inconvenience .

0

importing as below resolved the error for me 

from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import image_utils
from keras.optimizers import RMSprop
 
0

Please sign in to leave a comment.