Search code examples
pythonpython-3.xwindowstensorflowanaconda

How do I import TensorFlow in Python?


I am trying to import the TensorFlow library in Python (Anaconda Spyder) on Windows:

import tf.contrib.keras.preprocessing

It's giving me:

No module found

tensorflow.contrib.keras.preprocessing and from tf.contrib.keras.preprocessing.text import Tokenizer also don't work. I verified it is present (link to the library). pip and conda install also throw an error. From Anaconda prompt I typed:

activate tensorflow
pip install tf.contrib.keras.preprocessing
conda install tf.contrib.keras.preprocessing

Solution

  • You are doing it wrong as tf is not the name of the tensorflow module but an alias in the tutorials.

    import tensorflow as tf
    

    Thus try this:

    from tensorflow.contrib.keras.preprocessing.text import Tokenizer
    

    From your comments it seems that the module might not be installed so you can check in the list of installed packages:

    conda list
    

    If not present, install it with pip. Follow the doc.