Python 无法导入 Tensorflow“没有名为 copyreg 的模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33656551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:43:19  来源:igfitidea点击:

Unable to import Tensorflow "No module named copyreg"

pythontensorflow

提问by Minh Mai

El Capitan OS here. I've been trying to find a workaround with import Tensorflow into my ipython notebook, but so far no luck.

El Capitan 操作系统在这里。我一直试图找到一种将 Tensorflow 导入到我的 ipython 笔记本中的解决方法,但到目前为止还没有运气。

Like many people in the forums, I've also had issues with install tensorflow because of the six package. I was able to install after some fidgeting with brew

像论坛中的许多人一样,由于六个包,我也遇到了安装 tensorflow 的问题。在对 brew 进行一些坐立不安之后,我能够安装

brew link gdbm
brew install python
rew linkapps python
sudo pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

I got a message that tensorflow was installed correctly. Even when I did sudo pip install tensorflowI get the message:

我收到一条消息,表明 tensorflow 已正确安装。即使sudo pip install tensorflow我收到了消息:

Requirement already satisfied (use --upgrade to upgrade): tensorflow in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in /Library/Python/2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.9.2 in /usr/local/lib/python2.7/site-packages (from tensorflow)

However, when I'm on my ipython notebook and I did an import tensorflowI get the message: ImportError: No module named tensorflow

但是,当我在 ipython 笔记本上执行操作时,import tensorflow我收到消息:ImportError: No module named tensorflow

I've dug further and found this error on the import as well:

我进一步挖掘并在导入中发现了这个错误:

In [1]: import tensorflow
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-a649b509054f> in <module>()
----> 1 import tensorflow

/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py in <module>()
      2 # module.
      3 # pylint: disable=wildcard-import
----> 4 from tensorflow.python import *

/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py in <module>()
     11 
     12 import tensorflow.python.platform
---> 13 from tensorflow.core.framework.graph_pb2 import *
     14 from tensorflow.core.framework.summary_pb2 import *
     15 from tensorflow.core.framework.config_pb2 import *

/usr/local/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py in <module>()
      6 from google.protobuf import descriptor as _descriptor
      7 from google.protobuf import message as _message
----> 8 from google.protobuf import reflection as _reflection
      9 from google.protobuf import symbol_database as _symbol_database
     10 from google.protobuf import descriptor_pb2

/usr/local/lib/python2.7/site-packages/google/protobuf/reflection.py in <module>()
     56   from google.protobuf.pyext import cpp_message as message_impl
     57 else:
---> 58   from google.protobuf.internal import python_message as message_impl
     59 
     60 # The type of all Message classes.

/usr/local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py in <module>()
     57 
     58 import six
---> 59 import six.moves.copyreg as copyreg
     60 
     61 # We use "as" to avoid name collisions with variables.

ImportError: No module named copyreg

采纳答案by Hamed MP

As Jonah commented, it's solved by this:

正如约拿所评论的那样,它已通过以下方式解决:

On MacOSX

在 MacOSX 上

If you encounter:

如果您遇到:

import six.moves.copyreg as copyreg
ImportError: No module named copyreg

Solution: TensorFlow depends on protobuf which requires six-1.10.0. Apple's default python environment has six-1.4.1 and may be difficult to upgrade. So we recommend either installing a separate copy of python via homebrew:

解决方案:TensorFlow 依赖于需要 6-1.10.0 的 protobuf。苹果默认的python环境有6-1.4.1,可能很难升级。因此,我们建议通过自制软件安装单独的 python 副本:

brew install python

But I highly recommend using virtualenv for this purpose.

但我强烈建议为此目的使用 virtualenv。

# On Mac:
$ sudo easy_install pip  # If pip is not already installed
$ sudo pip install --upgrade virtualenv

Next, set up a new virtualenv environment. To set it up in the directory ~/tensorflow, run:

接下来,设置一个新的 virtualenv 环境。要在目录中设置它~/tensorflow,请运行:

$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow

Then activate the virtualenv:

然后激活virtualenv:

$ source bin/activate  # If using bash
$ source bin/activate.csh  # If using csh
(tensorflow)$  # Your prompt should change

Inside the virtualenv, install TensorFlow:

在 virtualenv 中,安装 TensorFlow:

(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

You can then run your TensorFlow program like:

然后你可以像下面这样运行你的 TensorFlow 程序:

(tensorflow)$ python tensorflow/models/image/mnist/convolutional.py

# When you are done using TensorFlow:
(tensorflow)$ deactivate  # Deactivate the virtualenv

$  # Your prompt should change back

回答by Gideon

Copyreg can be found in the six module in Python2.

Copyreg 可以在 Python2 的六个模块中找到。

Short answer, try upgrading/installing six:

简短回答,尝试升级/安装六个:

pip install -U six

回答by Russell

Maybe try reinstalling six. It looks like it is in a different location from your other libraries. /Library/Python/...vs /usr/local/lib/...This probably means that six was install before homebrew python.

也许尝试重新安装六个。它看起来与您的其他库位于不同的位置。/Library/Python/...vs/usr/local/lib/...这可能意味着在自制 python 之前安装了六个。

回答by eggplantelf

Another simple way I tried to fix this is to edit the source code and python_message.pyand change the line import six.moves.copyreg as copyreginto from six.moves import copyreg.

我尝试解决此问题的另一种简单方法是编辑源代码并将python_message.py该行更改import six.moves.copyreg as copyregfrom six.moves import copyreg.

Of course you might need to change the permission of this read-only file in order to modify it.

当然,您可能需要更改此只读文件的权限才能对其进行修改。