Pylint: `Unable to import submodule` warning

Created on 16 Apr 2019  路  9Comments  路  Source: PyCQA/pylint

I'm experiencing the issue described in the posts below. Pylint complaining that it's unable to import submodules.

import tensorflow as tf works fine
from tensorflow.keras import layers throws error:

Unable to import 'tf.keras'pylint(import-error)

I'm operating in VSCode using a virtual environment and python3. Code runs fine at runtime, so it appears to be pylint having trouble with submodule imports of the form from xxx.yyy import zzz

Would appreciate guidance whether this is a bug or other issue. After extensive research I'm still unable to resolve. Thank you.

Issue is also described here:
https://medium.com/@haominnn/how-to-deal-with-python-not-finding-tensor-flow-module-under-anaconda-3aefa8ffae11
With related discussion here:
https://stackoverflow.com/questions/49601292/python-not-finding-tensorflow-module-under-anaconda

bug topic-import-system

All 9 comments

Same problem in a virtualenv environment. Definitely not a VSCode bug, since it is not used in this test.

Getting versions:

(venv) [atila.alr@atila dir]$ python3 -m pylint --version
__main__.py 2.3.1
astroid 2.2.5
Python 3.6.6 (default, Jul 19 2018, 16:29:00) 
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]



md5-02647d2812d2837c2189ed2b2bb7ba0b



(venv) [atila.alr@atila dir]$ pylint --version



md5-02647d2812d2837c2189ed2b2bb7ba0b



pylint 2.3.1
astroid 2.2.5
Python 3.6.6 (default, Jul 19 2018, 16:29:00) 
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]



md5-6a4a2be96a21b9b0c546f25d16cc4849



from tensorflow.keras import models

print(dir(models))



md5-60cf26b658f3f92d15b6e581c1a2dcaf



(venv) [atila.alr@atila dir]$ python3 test.py 



md5-02647d2812d2837c2189ed2b2bb7ba0b



['Model', 'Sequential', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'clone_model', 'load_model', 'model_from_config', 'model_from_json', 'model_from_yaml', 'save_model']



md5-0730927e9ac8a52a9b3a99cbd6f77c99



(venv) [atila.alr@atila dir]$ python3 -m pylint test.py 



md5-02647d2812d2837c2189ed2b2bb7ba0b



************* Module test
test.py:1:0: C0111: Missing module docstring (missing-docstring)
test.py:1:0: E0401: Unable to import 'tensorflow.keras' (import-error)

----------------------------------------------------------------------
Your code has been rated at -20.00/10 (previous run: -25.00/10, +5.00)

Imports without submodules, as import tensorflow or from tensorflow import keras gives no error.
It seems that the errors shows up only when submodules are involved.

Thanks for the report, folks, I can reproduce this as well.

Same problem for me with the following version of pylint when importing submodules
from tensorflow.keras import layers
from tensorflow.keras import models
(tensorflow2.0) [bilal@HP ~]$ python3 -m pylint --version
pylint 2.4.4
astroid 2.3.3
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0]

I solved that by importing the full name of tensorflow module with following commands;
from tensorflow_core.python.keras.api._v2.keras import layers
from tensorflow_core.python.keras.api._v2.keras import models

from tensorflow_core.python.keras.api._v2.keras import layers

You must not import from tensorflow_core. This will break your code at some point if it didn't already because it will import some tensorflow stuff multiple times

BTW: Can anyone close this in favor of #2603? @PCManticore

Thanks for the ping @Flamefire Let's close this one then.

some problem.

import tensorflow as tf # works fine
from tensorflow import estimator, keras # cannot go to the definition of keras, but ok for estimator
from tensorflow.keras.models import Sequential, load_model, Model # unable to import

pylint 2.4.4
astroid 2.3.3
Python 3.6.2
tensorflow 1.15.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamtheturtle picture adamtheturtle  路  3Comments

sambarluc picture sambarluc  路  3Comments

DGalt picture DGalt  路  3Comments

ethanchewy picture ethanchewy  路  3Comments

TBoshoven picture TBoshoven  路  3Comments