31

Could someone please tell me what I may be doing wrong. I keep getting this message when I run my python code:

import random

foo = ['a', 'b', 'c', 'd', 'e']

random_item = random.choice(foo)

print random_item

Error

AttributeError: 'module' object has no attribute 'choice'

7
  • What exactly do u want to do please?
    – mlwn
    Sep 5, 2014 at 23:49
  • 3
    @mlwn They want to choose a random item from their list foo, is that not obvious? Sep 5, 2014 at 23:50
  • The code works perfectly for me... Which version of python you are using? And which os?
    – mlwn
    Sep 5, 2014 at 23:52
  • 2
    It's obvious that it's working for you, but the possible problem is explained twice in the answers ;)
    – TidB
    Sep 5, 2014 at 23:53
  • 1
    @AMHD: That doesn't explain either of the two questions that mlwn asked, since PyCharm works with a wide range of Python versions, on at least 3 different OSs.
    – abarnert
    Sep 6, 2014 at 0:13

6 Answers 6

88

Shot in the dark: You probably named your script random.py. Do not name your script the same name as the module.

I say this because the random module indeed has a choice method, so the import is probably grabbing the wrong (read: undesired) module.

3
  • 2
    If this doesn't fix it, check for a random.pyc file as well.
    – zimbu668
    May 12, 2016 at 19:49
  • 3
    It may be a shot in the dark, but it is an impressive shot in the dark nonetheless. +1 (Also, for the record, having a name of math.py causes the same problem)
    – riwalk
    Sep 21, 2018 at 18:53
  • this totally saved me
    – tingyiy
    Oct 25, 2022 at 0:33
18

The problem in my case was I used random.choices() with python 3.5. However, it is available from python 3.6 onwards.

Instead, use random.sample() without the weights or cum_weights attributes you would specify in random.choices() if you are using python 3.5 and 2.7. Documentation is here.

2
  • 4
    use "random.sample"
    – davesave
    May 20, 2019 at 8:10
  • 2
    random.sample worked for me when my production server was using python3.5. No code changes needed. Just add it. Of course please test your code before doing this. Sep 12, 2019 at 20:34
7

Sounds like an import issue. Is there another module in the same directory named random? If so (and if you're on python2, which is obvious from print random_item) then it's importing that instead. Try not to shadow built-in names.

You can test this with the following code:

import random

print random.__file__

The actual random.py module from stdlib lives in path/to/python/lib/random.py. If yours is somewhere else, this will tell you where it is.

6
  • Isn't it normally path/to/python/lib/pythonX.Y/random.py (unless he's using a really old Python, I think before 2.3 or so)? Also, I don't think 2.7 gives you the .py file even if there's a .pyc the way 3.3+ does, so probably random.pyc, but I could be misremembering that. But other than that little nitpicky bit, great answer.
    – abarnert
    Sep 5, 2014 at 23:55
  • I saved my file is on my desktop.
    – AMHD
    Sep 6, 2014 at 0:10
  • 1
    @AdamSmith: Oh yeah, on Windows it leaves off the pythonX.Y directory because the top-level directory is already PythonXY.
    – abarnert
    Sep 6, 2014 at 3:45
  • 1
    It gives me SyntaxError: Missing parentheses in call to 'print' Nov 17, 2017 at 8:45
  • 1
    @Marine1 The OP on this question was running Python2, in which python is a statement not a function and requires no parentheses.
    – Adam Smith
    Nov 17, 2017 at 16:25
4

I also got this error by naming a method random like this:

import random

def random():

  foo = ['a', 'b', 'c', 'd', 'e']

  random_item = random.choice(foo)

  print random_item

random()

It's not your case (naming a file random.py) but for others that search about this error and may make this mistake.

3

In short, Python's looking in the first file it finds named "random", and isn't finding the choice attribute.

99.99% of the time, that means you've got a file in the path/directory that's already named "random". If that's true, rename it and try again. It should work.

1

If you are using Python older than 3.6 version, than you have to use NumPy library to achieve weighted random numbers. With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array.


from numpy.random import choice
  
sampleList = [100, 200, 300, 400, 500]
randomNumberList = choice(
  sampleList, 5, p=[0.05, 0.1, 0.15, 0.20, 0.5])
  
print(randomNumberList)

source : geeksforgeek

Not the answer you're looking for? Browse other questions tagged or ask your own question.