Keras: AttributeError: module 'tensorflow' has no attribute 'VERSION' on macOS Catalina

Created on 1 Nov 2019  路  8Comments  路  Source: rstudio/keras

Hi @dfalbel,

I'm having some troubles installing Keras for RStudio in macOS 10.15.1 (Catalina).

I've followed the install tutorial and when I try to call any Keras method, I get an error.

> version
               _                           
platform       x86_64-apple-darwin18.6.0   
arch           x86_64                      
os             darwin18.6.0                
system         x86_64, darwin18.6.0        
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes          

> devtools::install_github("rstudio/keras")
Skipping install of 'keras' from a github remote, the SHA1 (5300cb6d) has not changed since last install.

> library(keras)

> install_keras()
Using virtual environment '~/.virtualenvs/r-reticulate' ...
Collecting tensorflow==2.0.0
.
.
.
Successfully installed Pillow-6.2.1 absl-py-0.8.1 astor-0.8.0 cachetools-3.1.1 certifi-2019.9.11 chardet-3.0.4 gast-0.2.2 google-auth-1.6.3 google-auth-oauthlib-0.4.1 google-pasta-0.1.7 grpcio-1.24.3 h5py-2.10.0 idna-2.8 keras-2.3.1 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.17.3 oauthlib-3.1.0 opt-einsum-3.1.0 protobuf-3.10.0 pyasn1-0.4.7 pyasn1-modules-0.2.7 pyyaml-5.1.2 requests-2.22.0 requests-oauthlib-1.2.0 rsa-4.0 scipy-1.3.1 setuptools-41.6.0 six-1.12.0 tensorboard-2.0.1 tensorflow-2.0.0 tensorflow-estimator-2.0.1 tensorflow-hub-0.7.0 termcolor-1.1.0 urllib3-1.25.6 werkzeug-0.16.0 wheel-0.33.6 wrapt-1.11.2

Installation complete.

> reticulate::py_config()
python:         /Users/rim/.virtualenvs/r-reticulate/bin/python
libpython:      /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin/libpython3.7.dylib
pythonhome:     /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7:/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7
version:        3.7.3 (default, Oct 11 2019, 19:39:43)  [Clang 11.0.0 (clang-1100.0.33.12)]
numpy:          /Users/rim/.virtualenvs/r-reticulate/lib/python3.7/site-packages/numpy
numpy_version:  1.17.3

python versions found: 
 /Users/rim/.virtualenvs/r-reticulate/bin/python
 /usr/bin/python
 /usr/bin/python3
 /usr/local/bin/python3

> is_keras_available()
[1] FALSE

> mnist <- dataset_mnist()
Error in py_get_attr_impl(x, name, silent) : 
聽聽AttributeError: module 'tensorflow' has no attribute 'VERSION'

I've also tried installing it using anaconda but the result was the same ...

Please, let me know If I missed anything from the install tutorial or If this is actually a bug.

Thank you,

Rim.

installation

Most helpful comment

@sbegueria @marcdotson

I deleted the existing virtualenv with rm ~/.virtualenvs/r-reticulate (but reticulate::virtualenv_remove() might be a more portable way to do it, though I haven't tried.)

Assuming you have python 3 installed, you can create the virtualenv by python3 -m venv ~/.virtualenvs/r-reticulate, then activate it via source ~/.virtualenvs/r-reticulate/bin/activate. Now, pip install numpy, restart your R session, and see if reticulate will find it.

All 8 comments

@RimAboulaasri we have observed some issues when TensorFlow is installed along with Python 3.7.
Can you try using python 3.6? Probably the easiest way to do this is to download and install Anaconda 3 and call install_keras(method="conda"). install_keras with conda will use Python 3.6.8 instead of 3.7.3.

I think this is related directly to the TensorFlow python - this is the log I get when trying to import Tensorflow directly from within python 3.7 in a virtualenv.

dfalbel$ /Users/dfalbel/.virtualenvs/r-reticulate/bin/python
Python 3.7.3 (default, Sep 18 2019, 14:29:06) 
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/tensorflow/__init__.py", line 98, in <module>
    from tensorflow_core import *
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/tensorflow_core/__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 959, in _find_and_load_unlocked
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/tensorflow/__init__.py", line 50, in __getattr__
    module = self._load()
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/tensorflow/__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/tensorflow_core/python/__init__.py", line 52, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/tensorflow_core/core/framework/graph_pb2.py", line 7, in <module>
    from google.protobuf import descriptor as _descriptor
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/google/protobuf/__init__.py", line 37, in <module>
    __import__('pkg_resources').declare_namespace(__name__)
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/__init__.py", line 84, in <module>
    __import__('pkg_resources.extern.packaging.requirements')
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/_vendor/packaging/requirements.py", line 9, in <module>
    from pkg_resources.extern.pyparsing import stringStart, stringEnd, originalTextFor, ParseException
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/extern/__init__.py", line 43, in load_module
    __import__(extant)
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/_vendor/pyparsing.py", line 4756, in <module>
    _escapedPunc = Word( _bslash, r"\[]-*.$+^?()~ ", exact=2 ).setParseAction(lambda s,l,t:t[0][1])
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/_vendor/pyparsing.py", line 1284, in setParseAction
    self.parseAction = list(map(_trim_arity, list(fns)))
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/_vendor/pyparsing.py", line 1066, in _trim_arity
    this_line = extract_stack(limit=2)[-1]
  File "/Users/dfalbel/.virtualenvs/r-reticulate/lib/python3.7/site-packages/pkg_resources/_vendor/pyparsing.py", line 1050, in extract_stack
    frame_summary = traceback.extract_stack(limit=-offset+limit-1)[offset]
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/traceback.py", line 211, in extract_stack
    stack = StackSummary.extract(walk_stack(f), limit=limit)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/traceback.py", line 363, in extract
    f.line
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/traceback.py", line 285, in line
    self._line = linecache.getline(self.filename, self.lineno).strip()
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/linecache.py", line 16, in getline
    lines = getlines(filename, module_globals)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/linecache.py", line 48, in getlines
    for mod in sys.modules.values():
RuntimeError: dictionary changed size during iteration

FWIW this may be the related issue in the TF repo: https://github.com/tensorflow/tensorflow/issues/33183

Also hit this. Fix is to upgrade to Python 3.7.5.

There may be more straightforward ways, but what worked for me was wiping the existing r-reticulate virtualenv, creating a new one with python3 -m venv ~/.virtualenvs/r-reticulate, activating it and installing numpy so reticulate will look for it.

Thanks, @kevinykuo! Could you please elaborate your answer? It seems the only solution so far, but I'm not able to reproduce it...

I'm with @sbegueria: An elaboration of how to upgrade to Python 3.7.5 from @kevinykuo or someone else would be greatly appreciated. I haven't found a straightforward way to do this working from a simple Keras install.

@sbegueria @marcdotson

I deleted the existing virtualenv with rm ~/.virtualenvs/r-reticulate (but reticulate::virtualenv_remove() might be a more portable way to do it, though I haven't tried.)

Assuming you have python 3 installed, you can create the virtualenv by python3 -m venv ~/.virtualenvs/r-reticulate, then activate it via source ~/.virtualenvs/r-reticulate/bin/activate. Now, pip install numpy, restart your R session, and see if reticulate will find it.

That's just what I needed. Thank you, @kevinykuo!

I'm completely lost and can't seem to get any of these solutions, or the ones in the referenced TF thread to work ...? How can I edit that python linecache file in the Xcode app folder? sudo open TextEdit linecache.py doesn't allow me to then edit it. ..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gsimchoni picture gsimchoni  路  3Comments

masterlord99 picture masterlord99  路  6Comments

faltinl picture faltinl  路  4Comments

gundalav picture gundalav  路  3Comments

saanasum picture saanasum  路  4Comments