关键词

导入tensorflow时报错:cannot import name ‘abs’的解决

当在导入Tensorflow时遇到“cannotimportname'abs'”错误时,通常是因为在导入Tensorflow时,发现了不兼容版本的Numpy库,导致错误发生。要解决这个问题,我们可以采取以下步骤:

  1. 确认Numpy库的版本是否兼容。Tensorflow文档中指定了与其结合使用的Numpy版本,我们可以访问Tensorflow官网,查找文档并确认Numpy版本是否正确。

  2. 若Numpy版本正确,但问题依然存在,则通过命令升级Numpy库的版本。我们可以使用以下命令升级Numpy。

pip install --upgrade numpy

例如,当前需要升级本地的Numpy库版本:

$ pip install --upgrade numpy
Collecting numpy
Downloading numpy-1.21.2-cp39-cp39-macosx_10_9_x86_64.whl (16.0 MB)
|████████████████████████████████| 16.0 MB 2.8 MB/s
Installing collected packages: numpy
Successfully installed numpy-1.21.2

可以看到,执行此命令后成功安装了新版本的Numpy库。

  1. 如果升级Numpy后问题仍然存在,则可以尝试重新安装Tensorflow。如果您使用的是conda环境,则建议先尝试卸载旧版本的Tensorflow库,然后重新安装最新版本。可以使用以下命令卸载Tensorflow:

conda remove tensorflow

然后运行以下命令重新安装Tensorflow:

conda install tensorflow

如果使用pip安装Tensorflow,可以使用以下命令重新安装:

pip uninstall tensorflow
pip install tensorflow

例如,以下是通过第二种方法解决问题的一个示例:

$ pip install --upgrade numpy
Collecting numpy
  Using cached numpy-1.21.2-cp39-cp39-macosx_10_9_x86_64.whl (16.0 MB)
Installing collected packages: numpy
Successfully installed numpy-1.21.2

$ python
Python 3.9.5 (default, May  3 2021, 08:20:33)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/anaconda3/envs/tf2/lib/python3.9/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/Users/user/anaconda3/envs/tf2/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 47, in <module>
    import numpy as np
  File "/Users/user/anaconda3/envs/tf2/lib/python3.9/site-packages/numpy/__init__.py", line 292, in <module>
    raise ImportError(msg)
ImportError: Error importing NumPy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
>>>

此示例演示了使用命令行升级Numpy库,并尝试在Python中导入Tensorflow时仍然遇到错误的情况。

下面是通过重新安装Tensorflow解决问题的一个示例:

$ conda remove tensorflow
Collecting package metadata (current_repodata.json): ...done
Solving environment: ...done

## Package Plan ##

  environment location: /Users/user/anaconda3/envs/tf2

  removed specs:
    - tensorflow


Removing intermediate dependencies...
collecting... done

$ conda install tensorflow
Collecting package metadata (current_repodata.json): ...done
Solving environment: ...done

## Package Plan ##

  environment location: /Users/user/anaconda3/envs/tf2

  added / updated specs:
    - tensorflow


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    keras-applications-1.0.8   |             py_1          23 KB
    keras-preprocessing-1.1.2  |             py_0          35 KB
    tensorboard-1.15.0         |   py37he3c9ec4_0         3.4 MB
    tensorflow-1.15.0          |gpu_py37h9419444_0           4 KB
    tensorflow-base-1.15.0     |gpu_py37h6c56545_0       146.5 MB
    tensorflow-estimator-1.15.1|             py_0         462 KB
    ------------------------------------------------------------
                                           Total:       150.5 MB

The following NEW packages will be INSTALLED:

  absl-py            pkgs/main/osx-64::absl-py-0.12.0-py37hf7b0b55_0
  astor              pkgs/main/osx-64::astor-0.8.1-py37_0
  gast               pkgs/main/osx-64::gast-0.2.2-py37_0
  google-pasta       pkgs/main/osx-64::google-pasta-0.1.7-py37_0
  grpcio             pkgs/main/osx-64::grpcio-1.38.0-py37hf8bcb03_0
  h5py               pkgs/main/osx-64::h5py-2.10.0-py37h3134771_0
  keras-applications pkgs/main/noarch::keras-applications-1.0.8-py_1
  keras-preprocessi~ pkgs/main/noarch::keras-preprocessing-1.1.2-py_0
  markdown           pkgs/main/noarch::markdown-3.3.4-pyhd8ed1ab_0
  protobuf           pkgs/main/osx-64::protobuf-3.17.2-py37h6c56545_0
  tensorboard        pkgs/main/osx-64::tensorboard-1.15.0-py37he3c9ec4_0
  tensorflow         pkgs/main/osx-64::tensorflow-1.15.0-gpu_py37h9419444_0
  tensorflow-base    pkgs/main/osx-64::tensorflow-base-1.15.0-gpu_py37h6c56545_0
  tensorflow-estima~ pkgs/main/noarch::tensorflow-estimator-1.15.1-py_0
  termcolor          pkgs/main/osx-64::termcolor-1.1.0-py37_1

Proceed ([y]/n)? y

Preparing transaction: ...done
Verifying transaction: ...done
Executing transaction: ...done

$ python
Python 3.7.10 (default, Feb 26 2021, 13:26:09)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-07-29 23:02:54.782812: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.dylib
2021-07-29 23:02:54.798168: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 3.95GiB deviceMemoryBandwidth: 104.43GiB/s
2021-07-29 23:02:54.798281: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.dylib
2021-07-29 23:02:54.947119: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.dylib
2021-07-29 23:02:54.996606: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.dylib
2021-07-29 23:02:55.023455: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.dylib
2021-07-29 23:02:55.162150: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.dylib
2021-07-29 23:02:55.188064: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.dylib
2021-07-29 23:02:55.485324: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.dylib
2021-07-29 23:02:55.485506: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2021-07-29 23:02:55.485542: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.dylib
>>> 

此示例演示了通过移除并重新安装Tensorflow解决问题的情况。感谢您的提问,希望这些信息对您有所帮助。

本文链接:http://task.lmcjl.com/news/13671.html

展开阅读全文