简体   繁体   English

无法在 VS 代码中导入 tensorflow.keras

[英]Can't import tensorflow.keras in VS Code

I'm running into problems using tensorflow 2 in VS Code.我在 VS Code 中使用 tensorflow 2 时遇到问题。 The code executes without a problem, the errors are just related to pylint in VS Code.代码执行没有问题,错误仅与 VS Code 中的 pylint 有关。 For example this import from tensorflow.keras.layers import Dense gives a warning "Unable to import 'tensorflow.keras.layers'pylint(import-error)".例如from tensorflow.keras.layers import Dense会发出警告“无法导入 'tensorflow.keras.layers'pylint(import-error)” Importing tensorflow and using tf.keras.layers.Dense does not produce an error.导入 tensorflow 并使用tf.keras.layers.Dense不会产生错误。 I'm just using a global python environment (3.7.2) on Windows 10, tensorflow is installed via Pip.我只是在 Windows 10 上使用全局 python 环境(3.7.2),tensorflow 是通过 Z608C2BFD1742AEBA743 安装的

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.我通过安装keras作为新的 package 解决了这个问题,然后我更改了所有包名称,删除了前缀tensorflow. . . So in your case after installing keras you should replace tensorflow.keras.layers with keras.layers因此,在您安装keras之后,您应该将tensorflow.keras.layers替换为keras.layers

I solved it by pressing ctrl+shift+P in Visual Studio Code, searching Python: Select Interpreter , and choosing the main environment.我通过在 Visual Studio Code 中按ctrl+shift+P来解决它,搜索Python: Select Interpreter ,然后选择主环境。

More detailed information can be found here .更详细的信息可以在这里找到。

My way to work with that: The problem is related to the custom import system in tf2 (see this issue ).我的处理方式:问题与 tf2 中的自定义导入系统有关(请参阅此问题)。 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.可以解决此问题( WindowsLinux ),这基本上欺骗了 VS Code 直接导入 tensorflow_core 并且不使用自定义惰性加载器。 If you just want to remove the red lines (as this is only an editor-problem), use如果您只想删除红线(因为这只是一个编辑器问题),请使用

"python.linting.pylintArgs": 
    ["--ignored-modules=tensorflow.keras"]

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

I had the same issue.我遇到过同样的问题。 I solved it by installing Keras and directly importing from keras instead from tensorflow.我通过安装 Keras 并直接从 keras 导入而不是从 tensorflow 解决了这个问题。

pip install Keras pip 安装 Keras

from keras.layers import Dense

Its just better to use pycharm instead of vscode.最好使用 pycharm 而不是 vscode。 This issue does not exist in pycharm. pycharm 中不存在此问题。 However, if you are insistent on using vscode, then the import statements have to be changed as follows.但是,如果您坚持使用 vscode,则必须将 import 语句更改如下。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM