optree 实际上已经安装了,但是当运行“from keras.models import Sequential”时,它仍然抛出 ImportError 要求我安装 optree,为什么?

问题描述 投票:0回答:1

我正在尝试使用 VS Code 启动一个小型数据科学项目,目前我在通过运行以下行导入

keras
时遇到问题:

LSTM 和 GRU 项目

依赖关系

import pandas as pd
import matplotlib as mpl
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Dense

回溯如下:

ImportError                               Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_21244\449270897.py in <cell line: 6>()
      4 import matplotlib as mpl
      5 from sklearn.preprocessing import MinMaxScaler
----> 6 from keras.models import Sequential
      7 from keras.layers import LSTM, Dropout, Dense

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py in <module>
      8 
      9 # Import everything from /api/ into keras.
---> 10 from keras.api import *  # noqa: F403
     11 from keras.api import __version__  # Import * ignores names start with "_".
     12 

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\api\__init__.py in <module>
      6 
      7 
----> 8 from keras.api import activations
      9 from keras.api import applications
     10 from keras.api import backend

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\api\activations\__init__.py in <module>
      5 """
      6  ...
---> 10     raise ImportError(
     11         "To use Keras, you need to have `optree` installed. "
     12         "Install it via `pip install optree`"

ImportError: To use Keras, you need to have `optree` installed. Install it via `pip install optree` Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

我决定在

pip install keras --upgrade --user
之类的终端命令中运行来检查上面的任何依赖项是否确实未安装,输出非常令人失望,因为它表明该项目中使用的每个依赖项都已满足其要求,请参阅:

C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install numpy --upgrade --user
Requirement already satisfied: numpy in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (1.26.4)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install optree --upgrade --user
Requirement already satisfied: optree in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (0.11.0)
Requirement already satisfied: typing-extensions>=4.0.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from optree) (4.2.0)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install keras --upgrade --user 
Requirement already satisfied: keras in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (3.3.3)
Requirement already satisfied: numpy in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (1.26.4)
Requirement already satisfied: optree in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (0.11.0)
Requirement already satisfied: h5py in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (3.11.0)
Requirement already satisfied: ml-dtypes in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (0.4.0)
Requirement already satisfied: absl-py in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (2.1.0)
Requirement already satisfied: rich in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (13.3.1)
Requirement already satisfied: typing-extensions>=4.0.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from optree->keras) (4.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.14.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from rich->keras) (2.14.0)
Requirement already satisfied: mdurl~=0.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from markdown-it-py<3.0.0,>=2.1.0->rich->keras) (0.1.2)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install pandas --upgrade --user 
Requirement already satisfied: pandas in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (2.2.2)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from pandas) (2.8.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from pandas) (2024.1)
Requirement already satisfied: pytz>=2020.1 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from pandas) (2021.3)
Requirement already satisfied: numpy>=1.22.4 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from pandas) (1.26.4)
Requirement already satisfied: six>=1.5 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install matplotlib --upgrade --user 
Requirement already satisfied: matplotlib in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (3.8.4)
Requirement already satisfied: packaging>=20.0 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from matplotlib) (24.0)
Requirement already satisfied: importlib-resources>=3.2.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (6.4.0)
Requirement already satisfied: numpy>=1.21 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (1.26.4)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (4.33.3)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (1.4.2)
Requirement already satisfied: pillow>=8 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (8.4.0)
Requirement already satisfied: cycler>=0.10 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (1.2.1)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (3.0.9)
Requirement already satisfied: zipp>=3.1.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from importlib-resources>=3.2.0->matplotlib) (3.6.0)
Requirement already satisfied: six>=1.5 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
WARNING: There was an error checking the latest version of pip.

我的Python解释器是

Python 3.9.6 64-bit
,我确信上面的所有依赖项都安装在其相应的路径中,我尝试卸载并重新安装
keras
optree
,包括运行
pip cache purge
以及重新启动内核,但它仍然抛出同样的错误。

我还注意到

keras.models
keras.layers
带下划线,当我“将鼠标悬停在它们上”时,它会显示:

Import "keras.models" could not be resolved Pylance(reportMissingImports)

所以,我被困住了,我想知道该怎么处理这个问题?

python-3.x visual-studio-code keras deep-learning importerror
1个回答
0
投票

¡解决了!

它与

typing_extensions
依赖关系有关,哪个版本太旧了,我是这样意识到的,我在 Jupyter Notebook 单元中运行了以下脚本:

import optree
import keras
print(optree.__version__)
print(keras.__version__)

哪个扔了:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_27656\3452976656.py in <cell line: 1>()
----> 1 import optree
      2 import keras
      3 print(optree.__version__)
      4 print(keras.__version__)

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\optree\__init__.py in <module>
     15 """OpTree: Optimized PyTree Utilities."""
     16 
---> 17 from optree import integration, typing
     18 from optree.ops import (
     19     MAX_RECURSION_DEPTH,

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\optree\typing.py in <module>
     36     Union,
     37 )
---> 38 from typing_extensions import NamedTuple  # Generic NamedTuple: Python 3.11+
     39 from typing_extensions import OrderedDict  # Generic OrderedDict: Python 3.7.2+
     40 from typing_extensions import Self  # Python 3.11+

ImportError: cannot import name 'NamedTuple' from 'typing_extensions' (c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\typing_extensions.py)

在 stackoverflow 上搜索,我发现了类似问题的 this 答案,所以我按照建议在 VS Code 终端上运行了这样的命令 (

pip install typing_extensions==4.7.1 --upgrade
),然后运行
pip install tensorflow
,然后运行
pip install numpy==1.23.5
,到最后重新启动 Python 内核,以便能够导入此类依赖项,并且出现 0 个问题和警告 👍.

© www.soinside.com 2019 - 2024. All rights reserved.