Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import tensorflow.keras in VS Code

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.

like image 793
Sebastian E Avatar asked Oct 02 '19 13:10

Sebastian E


People also ask

How do I fix the import error in VSCode?

To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.

Why can't I use keras with TensorFlow?

It seems that just tensorflow's keras module can't be imported properly, i.e. vscode's automatic completion function didn't work but other modules works fine. Sorry, something went wrong. If the problem is related to the autocomplete functions of your IDEs, then maybe this isn't a tensorflow issue.

Is there a TensorFlow module for Visual Studio Code?

I have read ImportError: No module named tensorflow for Visual Studio Code but it is used for working with GPU but I only want to work with CPU. How can I fix it? Thanks in advance. Show activity on this post. You should create an environment for your python project. After that, you should install tensorflow with pip.

What version of python do I need for TensorFlow?

TensorFlow Requirements TensorFlow and Keras require Python 3.6+ (Python 3.8 requires TensorFlow 2.2+), and the latest version of pip. You can determine the version of Python installed on your computer by running the following command:

Is it possible to import Keras in an interpreter?

Importing keras in an interpreter works but all linters and autocomplete providers cannot resolve it. Unless it's something shared by all of them I would guess it's TF


1 Answers

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

like image 108
Andrzej Ziemecki Avatar answered Oct 16 '22 12:10

Andrzej Ziemecki