简体   繁体   中英

Can tensorflow 2.x be run on the latest Python?

In a class I am taking on Machine Learning, we are instructed in detail how to create models using TensorFlow. Normally this is run on a virtual environment remotely, but for my own edification I figured I should be able to run TensorFlow on my own machine. I am currently running Python 3.8.6, and our instructions are keyed specifically for TensorFlow 2.1.0 or later.

First, the most direct means of installing TensorFlow is given on their homepage and numerous other sites. When I attempt to install TensorFlow this way (regardless what version I specify) I come up with an error like this:

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

It seems that all suggestions online seem to indicate that tensorflow doesn't work with Python 3.7 or later, and thus I should downgrade or uninstall Python to get it to work. Aside from the fact I don't really know how to downgrade python, I am not going to change my Python installation just to run one package. The only solution I was able to find that was able to work was given in this answer here .

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

However, once I do that I find that running Tensorflow does not actually work:

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

This is because async is a reserved word. So I hunt down the file in question, edit the actual Tensorflow package source code, and try again:

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.

This is where things get hilarious. All the suggestions I can find anywhere on the Internet says the only way to resolve this error is to re-install Tensorflow (as if I was able to install it in the first place). And yes, the URL given in the tracelog leads to a 404 error page .

Seems a little disillusioning that, after being instructed how to use Tensorflow for Machine Learning, it's not actually possible for me to run it? Is there an actual workaround, or is this class giving me misleading or outdated information?

Thanks, Nathan

EDIT: as stated already, it doesn't matter what version of Tensorflow I use. Specifying 2.2.0 or later gives the same result:

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

This would also be true of simply giving tensorflow without any version number

Seems to me like you are trying to install tensorflow 2.0.0 on python 3.8.6. Atleast that is how i understand it from your line:

pip install --upgrade tensorflow==2.0.0

From what i can read on tensorflows own documentation python 3.8 supports Tensorflow 2.2 or later. I would assume that you can fix this with command:

pip install --upgrade tensorflow

Which should just install the latest tensorflow release for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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