joke2k / django-faker

Django-faker uses fake-factory to generate test data for Django models and templates
MIT License
245 stars 48 forks source link

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

Open beeekind opened 7 years ago

beeekind commented 7 years ago

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 commented 6 years ago

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 commented 5 years ago

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