简体   繁体   中英

Why can't I import from a directory?

My directory structure looks like:

在此处输入图片说明

In my main.py , I have:

from lib.dataset.cifar import load_cifar_10

And my lib/data/cifar.py has:

from keras.utils import to_categorical
from keras.datasets import cifar10


def load_cifar_10():
    num_classes = 10

    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    # Convert class vectors to binary class matrices.
    y_train = to_categorical(y_train, num_classes)
    y_test = to_categorical(y_test, num_classes)

    ret_val = {
        "x_train": x_train,
        "x_test": x_test,
        "y_train": y_train,
        "y_test": y_test,
        "num_classes": num_classes
    }

    return ret_val

But the error I get is:

    dataset = cifar.load_cifar_10()
NameError: name 'cifar' is not defined

What am I doing incorrectly?

The import statement you used:

from lib.dataset.cifar import load_cifar_10

only imports the one function, under the name load_cifar_10 . The name of the whole module, cifar , is not imported.

But with this import, you can just access load_cifar_10 directly - so there is no need to use the "dot notation".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM