ModuleNotFoundError: No module named 'tensorflow.examples'
Asked Answered
H

12

29

When I import tensorflow

import tensorflow as tf

I don't get an error. However, I do get the error below. I'm using spyder if that helps.

As per other questions, I ensured up to date (v1.8) tensorflow using both conda and then pip installs. This didn't resolve the issue. Please assist.

import tensorflow.examples.tutorials.mnist.input_data as input_data

ModuleNotFoundError: No module named 'tensorflow.examples'
Halie answered 13/5, 2018 at 6:40 Comment(1)
go to the place where tensorflow has been installed and check whether example folder existsSwaim
N
3

Sometimes on downloading the TF, the example directory might not be available. You could rectify it by linking the 'example' directory from the GitHub repo into the tensorflow python wheel folder. That way you don't need to change the code.

If this doesn't work, try to replace import tensorflow.examples.tutorials.mnist.input_data as input_data as import input_data as mentioned in the link: TensorFlow MNIST example not running with fully_connected_feed.py

Hope this helps!!!

Normalcy answered 14/5, 2018 at 18:18 Comment(1)
Thanks. After deleting various installations and reinstalling tensorflow, dask and numpy eventually it corrected itself. I'm not sure what actually sorted it out.Halie
S
28

I think you should use like bellow on tensorflow 2

import tensorflow_datasets
mnist = tensorflow_datasets.load('mnist')
Stentor answered 28/11, 2019 at 20:13 Comment(2)
ugn! Now I've got ModuleNotFoundError: No module named 'tensorflow_datasets' lolMarche
@MonicaHeddneck - You then need to install the package with pip install tensorflow_datasets. DoneAubin
H
10

Use the following, it will download the data. It is from the tensorflow documentation

import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
Heterosexuality answered 1/12, 2019 at 10:38 Comment(1)
This is the only thing that worked for me.Marche
I
9

To load the mnist dataset in Tensorflow 2.0:

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

Here is the reference: TensorFlow 2 quickstart for beginners

Another method(also works for locally saved dataset):

DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'

path = tf.keras.utils.get_file('mnist.npz', DATA_URL)
with np.load(path) as data:
  train_examples = data['x_train']
  train_labels = data['y_train']
  test_examples = data['x_test']
  test_labels = data['y_test']

Here is the reference: Load NumPy data

Imperious answered 22/4, 2020 at 4:29 Comment(0)
A
6

Sometimes the TensorFlow examples are not pre-downloaded, so you might need to run the below command to install the examples from Github using the below code.

!pip install -q git+https://github.com/tensorflow/examples.git
Apologist answered 9/11, 2019 at 20:6 Comment(0)
N
3

Sometimes on downloading the TF, the example directory might not be available. You could rectify it by linking the 'example' directory from the GitHub repo into the tensorflow python wheel folder. That way you don't need to change the code.

If this doesn't work, try to replace import tensorflow.examples.tutorials.mnist.input_data as input_data as import input_data as mentioned in the link: TensorFlow MNIST example not running with fully_connected_feed.py

Hope this helps!!!

Normalcy answered 14/5, 2018 at 18:18 Comment(1)
Thanks. After deleting various installations and reinstalling tensorflow, dask and numpy eventually it corrected itself. I'm not sure what actually sorted it out.Halie
B
1

Different approach OS: Windows

copy from:

https://github.com/tensorflow/examples/tree/master/tensorflow_examples

to

[python folder]\Lib\site-packages\tensorflow_examples

use

import tensorflow_examples

example

from tensorflow_examples.models import pix2pix

but for datasets use:

pip install tensorflow_datasets
Bost answered 25/1, 2020 at 15:54 Comment(0)
H
1

I solved this issue by adding **tutorial** directory into tensorflow_core, usually this issue pops up when lacking of this file

  1. ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples check this directory to see if you have tutorials file. lack of tutorial file
  2. If you do not have, then go to https://github.com/tensorflow/tensorflow download the zip file, and extract all (or open it). download tutorial file
  3. find tutorials file from tensorflow-master\tensorflow\examples\, and copy it to ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples.
  4. Issue resolved. run
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
im = mnist.train.images[1]
im = im.reshape(-1, 28)
plt.imshow(im)

Haiku answered 5/2, 2020 at 1:41 Comment(0)
O
0

I solved this issue on Mac, simply copy the official examples to tensorflow_core/examples directory.

  1. pull the tensorflow code

    git clone https://github.com/tensorflow/tensorflow

  2. copy the examples to the system python3 directory

    cp -a tensorflow/examples/ /usr/local/lib/python3.7/site-packages/tensorflow_core/examples/

Outman answered 21/10, 2019 at 7:24 Comment(0)
T
0

You need to download the data sets to use it.

Command:

pip install tensorflow-datasets

Code part:

mnist_train = tfds.load(name="mnist", split="train")

You are done now. Happy coding! :)

Treasurer answered 3/1, 2020 at 6:33 Comment(0)
S
0

You just need to download the lost files and copy them to the tensorflow-core/ examples.

for me on windows 10 is : C:\Users\Amirreza\AppData\Local\Programs\Python\Python37\Lib\site-packages\tensorflow_core\examples

Schiedam answered 16/12, 2020 at 10:37 Comment(0)
U
0

This folder has been deleted at September/2020. See their repository.

I used the commands:

git clone https://github.com/tensorflow/tensorflow.git
tensorflow> git checkout c31acd156c
Update answered 24/10, 2021 at 14:54 Comment(0)
C
0

I solved this issue by installing Keras according to this answer from different question:

I didn't have Keras installed and had to add it manualy

ImportError: No module named 'keras'

Crier answered 6/6, 2022 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.