Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'string' has no attribute 'letters' #24

Open
beeekind opened this issue Sep 2, 2017 · 2 comments
Open

AttributeError: module 'string' has no attribute 'letters' #24

beeekind opened this issue Sep 2, 2017 · 2 comments

Comments

@beeekind
Copy link

beeekind commented Sep 2, 2017

This error is raised when the following code is called in a Django test case:

from django_faker import Faker

populator = Faker.getPopulator()
populator.addEntity(User, 2)
# Task contains a foreign key to a User object
populator.addEntity(Task, 5)
models = populator.execute()

Relevant stacktrace

Traceback (most recent call last):
  File "/Users/ben/PycharmProjects/mistreatment/tasks/tests.py", line 14, in setUp
    models = populator.execute()
  File "/usr/local/lib/python3.6/site-packages/django_faker/populator.py", line 163, in execute
    insertedEntities[klass].append( self.entities[klass].execute(using, insertedEntities) )
  File "/usr/local/lib/python3.6/site-packages/django_faker/populator.py", line 104, in execute
    value = format(insertedEntities) if hasattr(format,'__call__') else format
  File "/usr/local/lib/python3.6/site-packages/django_faker/guessers.py", line 23, in <lambda>
    if name in ('username','login','nickname'): return lambda x:generator.userName()
  File "/usr/local/lib/python3.6/site-packages/faker/providers/Internet.py", line 57, in userName
    return self.bothify( self.generator.parse(format) ).lower()
  File "/usr/local/lib/python3.6/site-packages/faker/providers/__init__.py", line 82, in bothify
    return BaseProvider.lexify( BaseProvider.numerify( text ) )
  File "/usr/local/lib/python3.6/site-packages/faker/providers/__init__.py", line 72, in lexify
    return re.sub( r'\?', lambda x: BaseProvider.randomLetter(), text )
  File "/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 191, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/local/lib/python3.6/site-packages/faker/providers/__init__.py", line 72, in <lambda>
    return re.sub( r'\?', lambda x: BaseProvider.randomLetter(), text )
  File "/usr/local/lib/python3.6/site-packages/faker/providers/__init__.py", line 35, in randomLetter
    return random.choice( string.letters )
AttributeError: module 'string' has no attribute 'letters'

If the test is run 5 times it will only error 3 or 4 times it is run - which makes it even more confusing. Any idea what is going on here? Thanks.

@Ahmed7fathi
Copy link

hey I'm not dealing with django yet but i had the same issue today when i was working with old project
module string has changes in python3
in python 2 'string.letters' was working fine
but in python 3 you need to add 'string.ascii_letters'
in your case probably you need to edit File "/usr/local/lib/python3.6/site-packages/faker/providers/init.py", line 35
in return random.choice( string.letters )
to
return random.choice( string.ascii_letters )

@mohammedyunus009
Copy link

I had the same error in Flask . i just tried "string.ascii_letters" instead of "string.letters"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants