21

I'm running into problems using tensorflow 2 in VS Code. The code executes without a problem, the errors are just related to pylint in VS Code. For example this import from tensorflow.keras.layers import Dense gives a warning "Unable to import 'tensorflow.keras.layers'pylint(import-error)". Importing tensorflow and using tf.keras.layers.Dense does not produce an error. I'm just using a global python environment (3.7.2) on Windows 10, tensorflow is installed via Pip.

Sebastian E
  • 467
  • 1
  • 3
  • 15
  • Your pylint installed in same environment as you in 3.7.2? – Poojan Oct 02 '19 at 13:03
  • I thing so. It is located in C:\Program Files\Python37\Scripts and I pointed VS Code to that path. – Sebastian E Oct 02 '19 at 13:08
  • It's also possible that Pylint simply doesn't understand tensorflow 2 and its package layout (Pylint has known issues handling some other projects that contain extension modules like PyQt5). – Brett Cannon Oct 02 '19 at 21:06
  • Seems to be that way. Telling pylint to ignore tensorflow.keras removes the error messages, which is not a real solution but good enough for now. – Sebastian E Oct 02 '19 at 22:44
  • How do I configure pylint on vscode to ignore this tensorflow error? – Alberto A Mar 27 '20 at 11:59
  • Does this answer your question? [Import "tensorflow.keras" could not be resolved after upgrading to TensorFlow 2.8.0](https://stackoverflow.com/questions/71000250/import-tensorflow-keras-could-not-be-resolved-after-upgrading-to-tensorflow-2) – Mehraban Sep 21 '22 at 07:16

6 Answers6

33

The imports that were causing the issue for me:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense

The way I resolved it:

from tensorflow import keras
from keras.models import Model
from keras.layers import Dense
Atif Hassan
  • 932
  • 6
  • 12
3

I solved it by pressing ctrl+shift+P in Visual Studio Code, searching Python: Select Interpreter, and choosing the main environment.

More detailed information can be found here.

Burhan Bilen
  • 83
  • 2
  • 7
  • This actually works even if you were using a python virtual environment, turns out the interpreter was using the global environment, hence the warning. – Akshaya Natarajan Feb 24 '22 at 11:20
3

Instead of importing from tensorflow.keras import everything from "keras" solely. From one of the tensorflow versions until now there is a common error. For instance: Instead of

From tensorflow.keras import layers, models

Write this:

From keras import layers,models
MJELVEH
  • 83
  • 5
2

My way to work with that: The problem is related to the custom import system in tf2 (see this issue). A work around for this is possible (Windows, Linux) which basically tricks VS Code to directly import tensorflow_core and don't use the custom lazy loader. If you just want to remove the red lines (as this is only an editor-problem), use

"python.linting.pylintArgs": 
    ["--ignored-modules=tensorflow.keras"]
Sebastian E
  • 467
  • 1
  • 3
  • 15
1

I too faced the same issue. I solved it by installing keras as a new package and then I changed all packages name removing the prefix tensorflow.. So in your case after installing keras you should replace tensorflow.keras.layers with keras.layers

RejeeshChandran
  • 4,168
  • 3
  • 21
  • 32
-5

Its just better to use pycharm instead of vscode. This issue does not exist in pycharm. However, if you are insistent on using vscode, then the import statements have to be changed as follows.

from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense