如何在安装了 numpy >= 2.0.0 的 Python 中获取 pandas 版本

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

通常在 Python 中获取 pandas 版本如下:

import pandas
print(pandas.__version__)

但是,如果安装了 numpy 2.0.0 或更高版本,并且 pandas 版本为 < 2.2.2 it typically crashes as follows on the import statement:

enter image description here

是否有一种聪明的方法来检查 pandas 版本(在 Python 中),以警告用户这种不兼容性,而不是用户不友好的回溯转储?

python pandas numpy
1个回答
0
投票

您可以使用 try except 块来捕获错误并返回。示例代码如下:

try:
    import pandas as pd
except ValueError as e:
    print(f"Import error: {e}")
© www.soinside.com 2019 - 2024. All rights reserved.