tensorflow 2.x可以在最新的Python上运行吗?

2024-09-30 16:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

在我学习机器学习的一节课上,我们被详细指导如何使用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不适用于Python3.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(就好像我一开始就可以安装它一样)。是的,tracelogleads to a 404 error page中给出的URL

似乎有点幻灭,在被指导如何使用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而不提供任何版本号,也是如此


Tags: inpyimportpackagestensorflowlinesiteusers
1条回答
网友
1楼 · 发布于 2024-09-30 16:31:52

在我看来,您正在尝试在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版本

相关问题 更多 >