首页 > 解决方案 > tensorflow 2.x 可以在最新的 Python 上运行吗?

问题描述

在我正在学习的机器学习课程中,我们详细了解了如何使用 TensorFlow 创建模型。通常这是在远程虚拟环境中运行的,但为了我自己的启发,我认为我应该能够在自己的机器上运行 TensorFlow。我目前正在运行 Python 3.8.6,我们的指令是专门为 TensorFlow 2.1.0 或更高版本键入的。

首先,安装 TensorFlow 最直接的方法是在他们的主页和许多其他网站上。当我尝试以这种方式安装 TensorFlow 时(无论我指定什么版本),我会遇到如下错误:

D:\Documents>pip install --upgrade tensorflow==2.0.0
WARNING: Ignoring invalid distribution -ip (d:\program files (x86)\python38-32\lib\site-packages)
WARNING: Ignoring invalid distribution - (d:\program files (x86)\python38-32\lib\site-packages)
ERROR: Could not find a version that satisfies the requirement tensorflow==2.0.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==2.0.0

似乎所有在线建议似乎都表明 tensorflow 不适用于 Python 3.7 或更高版本,因此我应该降级或卸载 Python 以使其正常工作。除了我真的不知道如何降级 python 之外,我不会为了运行一个包而更改我的 Python 安装。我能找到的唯一能够工作的解决方案在这个答案中给出。

pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.8.0-py3-none-any.whl

但是,一旦我这样做了,我发现运行 Tensorflow 实际上并不起作用:

D:\Documents>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py", line 22, in <module>
from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 114
def TFE_ContextOptionsSetAsync(arg1, async):
                                     ^
SyntaxError: invalid syntax

这是因为async是保留字。所以我找到了有问题的文件,编辑了实际的 Tensorflow 包源代码,然后再试一次:

D:\Documents>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
  File "D:\Program Files (x86)\Python38-32\lib\imp.py", line 296, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    import _pywrap_tensorflow_internal
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\__init__.py", line 22, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
  File "D:\Program Files (x86)\Python38-32\lib\imp.py", line 296, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\nsg3\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    import _pywrap_tensorflow_internal
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

这就是事情变得有趣的地方。我在 Internet 上任何地方都能找到的所有建议都表明,解决此错误的唯一方法是重新安装 Tensorflow(就好像我一开始就能够安装它一样)。是的,跟踪日志中给出的 URL会导致 404 错误页面

似乎有点失望,在被指示如何使用 Tensorflow 进行机器学习之后,我实际上不可能运行它?是否有实际的解决方法,或者这门课是否给了我误导或过时的信息?

谢谢, 内森

编辑:如前所述,我使用什么版本的 Tensorflow 并不重要。指定 2.2.0 或更高版本会产生相同的结果:

D:\Documents>pip install --upgrade tensorflow==2.2.0
WARNING: Ignoring invalid distribution -ip (d:\program files (x86)\python38-32\lib\site-packages)
WARNING: Ignoring invalid distribution - (d:\program files (x86)\python38-32\lib\site-packages)
ERROR: Could not find a version that satisfies the requirement tensorflow==2.2.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==2.2.0

这也适用于简单地给出tensorflow而没有任何版本号

标签: pythonpython-3.xtensorflowinstallationpip

解决方案


在我看来,您正在尝试在 python 3.8.6 上安装 tensorflow 2.0.0。至少这就是我从您的台词中理解的方式:

pip install --upgrade tensorflow==2.0.0

从我在 tensorflows 自己的文档中可以读到的内容, python 3.8 支持 Tensorflow 2.2 或更高版本。我假设您可以使用以下命令解决此问题:

pip install --upgrade tensorflow

应该只为您安装最新的 tensorflow 版本。


推荐阅读