简体   繁体   English

TensorFlow 的 ImportError 和 AttributeError

[英]ImportError and AttributeError for TensorFlow

Background背景

I'm trying to work on a GAN neural network (I'm a beginner for both Python and Machine-Learning), and I need Tensorflow.我正在尝试研究 GAN 神经网络(我是 Python 和机器学习的初学者),我需要 Tensorflow。

Problem问题

I have tried to use TensorFlow but can't install.我尝试使用 TensorFlow 但无法安装。 I have read questions and answers on SO about various errors, and have tested out those solutions, but I believe this case is different.我已经阅读了关于各种错误的问题和答案,并测试了这些解决方案,但我相信这种情况是不同的。

What I have tried (in chronological order)我尝试过的(按时间顺序)

1. Plain Reboot 1. 普通重启

a) Close all tabs for Jupyter Notebook a) 关闭 Jupyter Notebook 的所有选项卡
b) Close Anaconda Navigator b) 关闭 Anaconda 导航器
c) Restart Jupyter Notebook c) 重启 Jupyter Notebook
d) Rerun code d) 重新运行代码

Result: ImportError: no module结果: ImportError: no module

2. Reinstall tf 2.重新安装tf

a) Repeat 1a and 1b a) 重复 1a 和 1b
b) Open Anaconda Prompt b) 打开 Anaconda 提示
c) pip install tensorflow c) pip install tensorflow

Result: module installed结果: module installed

3. Check out Navigator 3.查看导航器

Tensorflow installed in all environments I have. Tensorflow 安装在我拥有的所有环境中。

4. Reinstall tf (Take 2) 4. 重新安装 tf (Take 2)

a) Repeat 1a and 1b a) 重复 1a 和 1b
b) Open Anaconda Prompt b) 打开 Anaconda 提示
c) conda install -c conda-forge tensorflow c) conda install -c conda-forge tensorflow

Result: EnvironmentNotWritableError:The current user does not have write permissions to the target environment. environment location: C:\\ProgramData\\<my username>结果: EnvironmentNotWritableError:The current user does not have write permissions to the target environment. environment location: C:\\ProgramData\\<my username> EnvironmentNotWritableError:The current user does not have write permissions to the target environment. environment location: C:\\ProgramData\\<my username>

5. Run as admin (from this question) 5. 以管理员身份运行(来自这个问题)

a) Repeat 1a and 1b a) 重复 1a 和 1b
b) Open Anaconda Prompt b) 打开 Anaconda 提示
c) conda install -c conda-forge tensorflow c) conda install -c conda-forge tensorflow

Result:结果:

Preparing transaction: done
Verifying transaction: done
Executing transaction: <after some text> done

6. Run in Jupyter 6. 在 Jupyter 中运行

The code has nothing to do with TF at the moment, but still, it doesn't work.该代码目前与 TF 无关,但仍然不起作用。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import glob2

font_lib = glob2.glob('**/*.ttf', recursive=True)

count = 0
for f in font_lib:
    count = count + 1
    if count < 10:
        print (f)
    else:
        break
print ("done")

Result:结果:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-efbffb1990be> in <module>
----> 1 import tensorflow as tf
      2 import numpy as np
      3 import matplotlib.pyplot as plt
      4 import os
      5 import glob2

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     22 
     23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
     25 
     26 from tensorflow._api.v1 import app

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     80 from tensorflow.python import data
     81 from tensorflow.python import distribute
---> 82 from tensorflow.python import keras
     83 from tensorflow.python.feature_column import feature_column_lib as feature_column
     84 from tensorflow.python.layers import layers

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\__init__.py in <module>
     23 
     24 from tensorflow.python.keras import activations
---> 25 from tensorflow.python.keras import applications
     26 from tensorflow.python.keras import backend
     27 from tensorflow.python.keras import callbacks

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\applications\__init__.py in <module>
     24 from tensorflow.python.keras import backend
     25 from tensorflow.python.keras import engine
---> 26 from tensorflow.python.keras import layers
     27 from tensorflow.python.keras import models
     28 from tensorflow.python.keras import utils

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\__init__.py in <module>
     27 
     28 # Advanced activations.
---> 29 from tensorflow.python.keras.layers.advanced_activations import LeakyReLU
     30 from tensorflow.python.keras.layers.advanced_activations import PReLU
     31 from tensorflow.python.keras.layers.advanced_activations import ELU

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\advanced_activations.py in <module>
     25 from tensorflow.python.keras.engine.base_layer import Layer
     26 from tensorflow.python.keras.engine.input_spec import InputSpec
---> 27 from tensorflow.python.keras.utils import tf_utils
     28 from tensorflow.python.ops import math_ops
     29 from tensorflow.python.util.tf_export import tf_export

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\__init__.py in <module>
     36 from tensorflow.python.keras.utils.layer_utils import get_source_inputs
     37 from tensorflow.python.keras.utils.losses_utils import squeeze_or_expand_dimensions
---> 38 from tensorflow.python.keras.utils.multi_gpu_utils import multi_gpu_model
     39 from tensorflow.python.keras.utils.np_utils import normalize
     40 from tensorflow.python.keras.utils.np_utils import to_categorical

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\multi_gpu_utils.py in <module>
     20 from tensorflow.python.framework import ops
     21 from tensorflow.python.keras import backend as K
---> 22 from tensorflow.python.keras.engine.training import Model
     23 from tensorflow.python.ops import array_ops
     24 from tensorflow.python.util.tf_export import tf_export

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in <module>
     40 from tensorflow.python.keras.engine import training_generator
     41 from tensorflow.python.keras.engine import training_utils
---> 42 from tensorflow.python.keras.engine.network import Network
     43 from tensorflow.python.keras.optimizer_v2 import optimizer_v2
     44 from tensorflow.python.keras.utils import data_utils

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.py in <module>
     38 from tensorflow.python.keras.engine import base_layer
     39 from tensorflow.python.keras.engine import base_layer_utils
---> 40 from tensorflow.python.keras.engine import saving
     41 from tensorflow.python.keras.engine import training_utils
     42 from tensorflow.python.keras.utils import generic_utils

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\saving.py in <module>
     36 # pylint: disable=g-import-not-at-top
     37 try:
---> 38   import h5py
     39   HDF5_OBJECT_HEADER_LIMIT = 64512
     40 except ImportError:

C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py in <module>
     34 _errors.silence_errors()
     35 
---> 36 from ._conv import register_converters as _register_converters
     37 _register_converters()
     38 

h5py\h5r.pxd in init h5py._conv()

h5py\h5r.pyx in init h5py.h5r()

AttributeError: type object 'h5py.h5r.Reference' has no attribute '__reduce_cython__'

7. Update h5py 7.更新h5py

a) Followed @Alireza Tajadod's instructions and tried. a) 按照@Alireza Tajadod 的指示进行尝试。 b) Run code in Jupyter b) 在 Jupyter 中运行代码

Result: Same as 6)...结果:同 6)...


I have tried every method I could, and any help would be highly appreciated.我已经尝试了所有可能的方法,任何帮助将不胜感激。 Thank you in advance!先感谢您!

Edit:编辑:

Reminded by the answer by @GarytheIceBreaker: Sorry that I forgot to mention, but I have everything installed and set up in Windows. @GarytheIceBreaker 的回答提醒:抱歉,我忘了提及,但我已经在 Windows 中安装并设置了所有内容。 Although this might be frustrating to some, please suggest solutions that can be done within Windows OS premises.尽管这可能会让某些人感到沮丧,但请提出可以在 Windows 操作系统前提下完成的解决方案。 Thanks!谢谢!

I tried jumping through hoops on Windows, and did get Anaconda working, but not Tensorflow.我尝试在 Windows 上跳过箍,并且确实让 Anaconda 工作,但不是 Tensorflow。 I recommend running Ubuntu virtually, on WSL if you don't want to make any major changes to your machine.如果您不想对机器进行任何重大更改,我建议在 WSL 上虚拟运行 Ubuntu。 Ubuntu is pretty user friendly these days, even if you use it without any graphical shell enabled.如今,Ubuntu 对用户非常友好,即使您在没有启用任何图形 shell 的情况下使用它。
Enable WSL, install Ubuntu from the Microsoft store, and apt-get install tensorflow.启用 WSL,从 Microsoft 商店安装 Ubuntu,然后 apt-get install tensorflow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM