ModuleNotFoundError: No module named 'tensorflow.python.training'
Asked Answered
S

3

9

When I am trying to run this

import keras

I am getting following error:

Traceback (most recent call last):

  File "<ipython-input-1-c74e2bd4ca71>", line 1, in <module>
    import keras

  File "/Users/rezwan/anaconda/lib/python3.6/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils

  File "/Users/rezwan/anaconda/lib/python3.6/site-packages/keras/utils/__init__.py", line 6, in <module>
    from . import conv_utils

  File "/Users/rezwan/anaconda/lib/python3.6/site-packages/keras/utils/conv_utils.py", line 3, in <module>
    from .. import backend as K

  File "/Users/rezwan/anaconda/lib/python3.6/site-packages/keras/backend/__init__.py", line 83, in <module>
    from .tensorflow_backend import *

  File "/Users/rezwan/anaconda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2, in <module>
    from tensorflow.python.training import moving_averages

ModuleNotFoundError: No module named 'tensorflow.python.training'

Already I have installed Theano, Tensorflow and Keras through the following commands:

Theano:

Rezwans-iMac:~ rezwan$ pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

Tensorflow:

Rezwans-iMac:~ rezwan$ conda create -n tensorflow python=3.6

Rezwans-iMac:~ rezwan$ source activate tensorflow

(tensorflow) Rezwans-iMac:~ rezwan$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0rc2-py3-none-any.whl

(tensorflow) Rezwans-iMac:~ rezwan$ pip3 install --upgrade $TF_BINARY_URL

Keras:

Rezwans-iMac:~ rezwan$ pip install --upgrade keras

Above commands work properly.

But I am getting above error. How can I solve this error?

Swor answered 27/9, 2017 at 7:10 Comment(5)
Are you using jupyter? Jupyter with conda has a very dirty bug. If you had jupyter installed before installing conda, it will keep pointing to your original python installed and will not point to conda's environments. (Thus will not find updates or libraries that exist only in conda). I haven't found a solution for that besides never installing jupyter without conda.Woolfolk
It seems that tensorflow supports well python 3.5. Not sure about python 3.6.Woolfolk
I am using spyder. And my OS is mac@DanielMöllerSwor
Are installing keras for python2 and tensorflow for python3? conda create -n tensorflow python=3.6; pip install --upgrade keras; you should install with pip3Headset
@Headset keras is in /Users/rezwan/anaconda/lib/python3.6/site-packages/keras, there is no issue with python2 visible hereScandium
H
2

To make sure all libraries are installed, you can use a text file with something like this:

jupyter==1.0.0
Keras==2.1.3
numpy==1.15.1
pandas==0.22.0
scikit-learn==0.19.1
scipy==1.0.0
tensorboard==1.10.0
tensorflow==1.10.0

Then use pip to install:

pip install -r requirements.txt

or

pip3 install -r requirements.txt

All libraries will be installed for use in the same python version, in case you have multiple installations.

Headset answered 19/9, 2018 at 15:18 Comment(0)
S
1

Your tensorflow installation commands:

Rezwans-iMac:~ rezwan$ conda create -n tensorflow python=3.6
Rezwans-iMac:~ rezwan$ source activate tensorflow
(tensorflow) Rezwans-iMac:~ rezwan$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0rc2-py3-none-any.whl
(tensorflow) Rezwans-iMac:~ rezwan$ pip3 install --upgrade $TF_BINARY_URL

created and activated a virtual environment called tensorflow. Notice the (tensorflow) before Rezwans-iMac, indicating that you are inside the newly created virtual environment. Since tensorflow is only installed in this virtual environment, you need to switch into it with

source activate tensorflow

everytime you want to use tensorflow. You will also need to install other packages that you want to use inside this environment.

Also configure your IDE to use the interpreter in .../anaconda/env/tensorflow/bin/python

Scandium answered 19/9, 2018 at 15:25 Comment(0)
H
1

Upgrading tensorflow to "1.15.2" Solved the issue for me. Seems like tensorflow doesn't have backword compatibility with

Happ answered 8/5, 2020 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.