jen007
jen007

Reputation: 1589

ImportError: cannot import name 'MNIST'

I've able to pip install all other packages such as bumpy, sklearn etc. but mnist package throws me error as the following. I've been trying to use sudo pip install but it also says that

applesys$ pip install mnist
Requirement already satisfied: mnist in            /Users/applesys/anaconda3/lib/python3.5/site-packages
Requirement already satisfied: numpy in /Users/applesys/anaconda3/lib/python3.5/site-packages (from mnist)
applesys$ sudo pip install mnist
Password:
The directory '/Users/applesys/Library/Caches/pip/http' or its parent             directory is not owned by the current user and the cache has been disabled.     Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/applesys/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: mnist in    /Users/applesys/anaconda3/lib/python3.5/site-packages
Requirement already satisfied: numpy in /Users/applesys/anaconda3/lib/python3.5/site-packages (from mnist)

import error

Upvotes: 10

Views: 32900

Answers (7)

JSymons7
JSymons7

Reputation: 71

As noted by @dinosaur, python-mnist and mnist are two different packages. For python package, python-mnist, the only module is loader.

If you change the import to:

from mnist.loader import MNIST

It should work.

Upvotes: 5

Aayush Singla
Aayush Singla

Reputation: 1365

I am just a beginner to both python and Machine learning.For me it turned out that the python script I was using was also named 'mnist.py' which was causing the error(that's what i think).I am not sure about this but this might help you.

Upvotes: 1

dinosaur
dinosaur

Reputation: 3278

Note that python-mnist and mnist are two different packages, and they both have a module called mnist. The package you want is python-mnist. So do this:

pip install python-mnist

It might be necessary to uninstall the mnist package with:

pip uninstall mnist

Then your import statement should work.

Upvotes: 30

Yuval Harpaz
Yuval Harpaz

Reputation: 1426

The you are using the wrong mnist code. you must be following lasagne example; they have a file called mnist.py with load_data function. I suggest you go to their example file, copy the code and save as lasagne_mnist.py, so when you import from mnist it will be clear from which mnist you are importing. Remember to have the directory in python path and init.py file in place to recognize it. then you can:

from lasagne_mnist import load_dataset
X_train, y_train, X_valid, y_valid, X_test, y_test = load_dataset()

where it should download the data

Upvotes: 1

Markacho
Markacho

Reputation: 312

I had the same problem. While inspecting the "mnist" folder that holds the "mnist" module, I could not find "MNIST" in the main module

    __init__.py

However, the MNIST class was found in the "loader.py" file, at the same location. I copied the "loader.py" file to the same folder where my Python script resides (where I'm trying to use this mnist module). Now, instead of using:

    from mnist import MNIST

I used:

    from loader import MNIST

Now everything works fine for me.

Upvotes: 1

AshiX
AshiX

Reputation: 11

You are trying to call a function that is not existing for example

Import time Time.sleep(9)

Here it sleeps for 9 but if you try to call the module from the module

From time import time It will look in time and find a function called time for example

Time.time

In this example the time function is existing so it will work but instead of time.time it will be used as time

Before: current_time = time.time After: current_time = time (Would recommend importing it as a diffrent name example: from time import time as currentime)

So if you are trying to call a function in the module please re-look at the name If you are trying to call the module just do

Import mnist

Or if there is a function inside the module: from mnist import mnist no caps

Upvotes: 1

mprat
mprat

Reputation: 2471

The proper way to import the mnist package is to just do:

import mnist

Should work fine - pip installed your packages properly.

Upvotes: 0

Related Questions