Python Faker Module
Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create beautiful XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.
Installation
pip install Faker
Basic Usage
To use Faker, import the Faker class and create a new instance.
from faker import Faker
fake = Faker()
print(fake.name())
# Output: 'Lucy Cechtelar'
print(fake.address())
# Output: '426 Jordy Lodge
# Cartwrightshire, SC 88120-6700'
print(fake.text())
# Output: 'Sint velit eveniet. Rerum atque repet...'
Localization
Faker supports multiple locales. You can pass a locale string to the constructor.
from faker import Faker
fake = Faker('it_IT')
print(fake.name())
# Output: 'Eldda Palumbo'
print(fake.address())
# Output: 'Via Borgo 59
# 76015 San Paolo (CI)'
You can also pass a list of locales.
fake = Faker(['en_US', 'ja_JP'])
for _ in range(5):
print(fake.name())
Seeding
To generate the same dataset every time (reproducibility), you can seed the generator.
from faker import Faker
Faker.seed(0)
fake = Faker()
print(fake.name())
# Output will be consistent across runs
Common Providers
Faker has many providers for different types of data.
- Profile:
fake.profile(),fake.simple_profile() - Internet:
fake.email(),fake.ipv4(),fake.url() - Credit Cards:
fake.credit_card_number(),fake.credit_card_expire() - Date/Time:
fake.date_of_birth(),fake.future_date()
Unique Values
If you need unique values, use the .unique property.
names = [fake.unique.first_name() for _ in range(10)]