-4

this may be an easy one so sorry for the post but I can't seem to get random.choices() to import

my code:

import random

scans = 100
foobar = []

for i in range(scans):
    string = 'https://'
    string.join(random.choices(string.ascii_lowercase + string.digits, k = url_len)) 
    string.join(random.choice(domains_list))
    foobar.append(string)
    del string

I am getting:

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

as an output in the mac-os terminal.

I have tried:

  • restarting editor

  • using from random import choices instead(alongside) of import random

again sorry if this seems like a spam post but I have been googling for a while and have not found a single answer.

Thanks to all! :D

  • 3
    Please show your *whole* code including the `import` statement. This is not a reproducible example. Also please show what output you are getting, and what output you want to get. – alani Oct 01 '20 at 20:06
  • 4
    It would also be useful for you to paste the error you get into your question - including the whole error trace. – Mark Ransom Oct 01 '20 at 20:08
  • Hey @MarkRansom! Just did an update on it. Added the error message and more of the code. – Daniel Dorman Oct 01 '20 at 20:15
  • 2
    `random.choices` was added in Python 3.6. Do you run an older version of Python? An other cause for the error might be that you called your own script `random.py`. BTW, you really don't need `del string` (`string` is a bad name for a variable anyway). – Matthias Oct 01 '20 at 20:17
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. – Prune Oct 01 '20 at 20:17
  • 2
    You shouldn't name a variable the same as a module. That will lead to confusion and/or bugs. – Mark Ransom Oct 01 '20 at 20:18
  • `string = 'foobar'` causes the error `AttributeError: 'str' object has no attribute 'ascii_uppercase'`. As for the error `AttributeError: 'module' object has no attribute 'choices'`, that may be because you've got a file named `random.py` somewhere. – Random Davis Oct 01 '20 at 20:39
  • Hey everyone. Sorry for all of the confusion. I am extremely new to the stack overflow platform. I have updated the code to be within MRE specifications. Thanks! – Daniel Dorman Oct 01 '20 at 20:55
  • @SyntaxTeen we need to know your Python version to be able to track down your issue, please read all the comments – Random Davis Oct 02 '20 at 15:23

1 Answers1

3

If you do your import like this, importing the function from the module:

from random import choices

Then your code needs to just refer to that function, since you haven't imported the module itself, so Python won't be aware of the module's name in your code:

choices(string.ascii_uppercase + string.digits, k = 10)

But if you instead import the whole module:

import random

Then your code needs to refer to both the module and the function, because the only way Python knows about the function is via the imported module:

random.choices(string.ascii_uppercase + string.digits, k = 10)

If you get this error:

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

Then that could mean you might also be using an old Python version which simply doesn't have choices, as it was added in Python 3.6.

If that's not the case, then you possibly have a file named random.py that's accidentally getting imported instead. See this question for more details if that's the case.

But without knowing more details about your python version and local files, it's impossible to say exactly why you in particular are getting that particular error.

Another issue in your above code is this line - it's overwriting your import string statement, and setting string to an actual string instead (your string was 'foobar' but any string would've caused the issue):

string = 'foobar'

When you refer to the string module right after, since you've overwritten it with the string 'foobar', trying to use the module at all (like string.ascii_uppercase for example) will result in this kind of error:

AttributeError: 'str' object has no attribute 'ascii_uppercase'
Random Davis
  • 6,662
  • 4
  • 14
  • 24