-2

I can do

import random
a = [1, 2, 3]
random.shuffle(a)

or

from random import shuffle
a = [1, 2, 3]
shuffle(a)

But I can't

import random.shuffle
a = [1, 2, 3]
random.shuffle(a)

which will make the interpreter complain

'random' is not a package.

Why is this?

Shen Zhuoran
  • 385
  • 3
  • 13

3 Answers3

2

You can't import random.shuffle, because it's function, so error saying it's not a package is correct.

If you want to import shuffle only, you can do this:

from random import shuffle

a = [1, 2, 3]
shuffle(a)
Justinas Marozas
  • 2,482
  • 1
  • 17
  • 37
1

Just putting the comments into an answer. import a.b imports the b module or package from a. Since shuffle isn’t a package, it can’t import it. Read more about packages here.

rajatppn
  • 440
  • 2
  • 7
-1

Import shuffle like this

from sklearn.utils import shuffle
Shuchita Bora
  • 257
  • 2
  • 5