【问题标题】:Can tensorflow 2.x be run on the latest Python?tensorflow 2.x 可以在最新的 Python 上运行吗?
【发布时间】:2021-08-19 07:16:52
【问题描述】:

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

首先,安装 TensorFlow 的最直接方法是在他们的 homepage 和许多其他网站上提供。当我尝试以这种方式安装 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 安装。 this answer here 中给出了我能找到的唯一可行的解​​决方案。

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.

这就是事情变得热闹的地方。我在互联网上任何地方都能找到的所有建议都说解决这个错误的唯一方法是重新安装 Tensorflow(就好像我一开始就可以安装它一样)。是的,跟踪日志中给出的 URL leads to a 404 error page。

在被告知如何使用 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 而没有任何版本号

【问题讨论】:

  • 你为什么使用 32 位 python? TensorFlow 仅适用于 64 位平台。

标签: python python-3.x tensorflow installation pip


【解决方案1】:

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

pip install --upgrade tensorflow==2.0.0

根据我在 tensorflows 上看到的内容,documentationpython 3.8 支持 Tensorflow 2.2 或更高版本。我假设您可以使用以下命令解决此问题:

pip install --upgrade tensorflow

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

【讨论】:

  • 那行不通。给出任何版本号,无论是 2.2 还是其他任何版本,都会给出相同的结果。单独给出没有版本号的 tensorflow 给出相同的结果
  • 我明白了。无论如何,我认为应该避免安装 2.0.0,因为您使用的是 python 3.8。正如史努比博士在 cmets 中提到的那样,您似乎使用的是 32 位而不是 64 位的 python。您应该尝试安装 64 位版本的 python,看看是否有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多