首页 > 解决方案 > 导入模块后tensorflow导入报错的解决方法

问题描述

pip install tensorflow
#== import it 
import tensorflow as tf

错误:

Traceback (most recent call last):
  File "C:\Users\C1Manager\PycharmProjects\pythonProject\dani\comp-vision\ld dataset\try.py", line 1, in <module>
    import tensorflow
  File "C:\Users\C1Manager\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\__init__.py", line 49, in <module>
    from ._api.v2 import __internal__
  File "C:\Users\C1Manager\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\_api\v2\__internal__\__init__.py", line 13, in <module>
    from . import distribute
  File "C:\Users\C1Manager\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\_api\v2\__internal__\distribute\__init__.py", line 10, in <module>
    from . import combinations
  File "C:\Users\C1Manager\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\_api\v2\__internal__\distribute\combinations\__init__.py", line 27, in <module>
    from tensorflow.python.distribute.strategy_combinations import parameter_server_strategy_1worker_2ps_1gpu
ImportError: cannot import name 'parameter_server_strategy_1worker_2ps_1gpu' from 'tensorflow.python.distribute.strategy_combinations' (C:\Users\C1Manager\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\strategy_combinations.py)

标签: pythontensorflow

解决方案


看起来 Tensorflow 安装不正确,请卸载现有的 Tensorflow 并按照说明安装新的,

使用画中画

#Install tensorflow using pip virtual env 
$pip install virtualenv
$virtualenv tf   # tf is virtual env name
$source tf/bin/activate
tf $ pip install tensorflow
tf $ python
>>import tensorflow as tf
>>tf.__version__
2.5.0

使用蟒蛇

1. Set Up Anaconda Environments
conda create --name tf_env python=3

2. Activate the new Environment
source activate tf_env

3. Install Tensorflow
pip install tensorflow

推荐阅读