已安装Keras,但无法导入,因为它表示找不到Keras模块

2024-05-29 06:01:03 发布

您现在位置:Python中文网/ 问答频道 /正文

!pip install Keras
import Keras
print(Keras.__version__)
import tensorflow as tf
print(tf.__version__)

在使用上面的代码之后,我遇到了这个错误,因为我试图使用nlp和深度学习解决分类问题,这就是为什么我尝试安装tensorflow和keras。但它不断地给我带来错误

 Requirement already satisfied: Keras in c:\users\hind\anaconda3\envs\toxic\lib\site-packages (2.4.3)
Requirement already satisfied: pyyaml in c:\users\hind\anaconda3\envs\toxic\lib\site-packages (from Keras) (5.4.1)
Requirement already satisfied: numpy>=1.9.1 in c:\users\hind\anaconda3\envs\toxic\lib\site-packages (from Keras) (1.19.5)
Requirement already satisfied: h5py in c:\users\hind\anaconda3\envs\toxic\lib\site-packages (from Keras) (3.1.0)
Requirement already satisfied: scipy>=0.14 in c:\users\hind\anaconda3\envs\toxic\lib\site-packages (from Keras) (1.6.2)
Requirement already satisfied: cached-property in c:\users\hind\anaconda3\envs\toxic\lib\site-packages (from h5py->Keras) (1.5.2)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-6-0a6f7852735b> in <module>
      1 get_ipython().system('pip install Keras')
----> 2 import Keras
      3 print(Keras.__version__)
      4 import tensorflow as tf
      5 print(tf.__version__)

ModuleNotFoundError: No module named 'Keras'

Tags: infromimportlibpackagessiterequirementusers
2条回答

试试这个: !pip安装keras或pip3安装keras

import keras
print(keras.__version__)

import tensorflow as tf
print(tf.__version__)

代码段中的包名称不正确

对于此问题,以下是最常见的错误及其解决方案:

  • python2和python3都安装在您的计算机上,python2使用pip,python3使用pip3,但您安装的软件包版本错误。尝试使用!pip3 install keras
  • 确保在安装任何软件包后重新启动了内核
  • Python区分大小写。确保你把所有东西都放在正确的箱子里。它是{}和{},而不是Keras或Tensoflow
  • 如果您同时拥有tensorflow和keras,根据版本的不同,可能会出现一些冲突,并且在执行import keras时会出现错误。改用from tensorflow import keras

相关问题 更多 >

    热门问题