<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5" -->

---
title: How to Build a Secure Password Generator with Python
description: Python's random module is unsafe for passwords, so use the secrets module instead. random is a pseudo-random generator driven by a formula and a seed, which...
canonical: https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Build a Secure Password Generator with Python | daily.dev
og:description: Python's random module is unsafe for passwords, so use the secrets module instead. random is a pseudo-random generator driven by a formula and a seed, which...
og:url: https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5
og:image: https://api.daily.dev/og/posts/raTtc1dL5.png
og:image:alt: How to Build a Secure Password Generator with Python
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Build a Secure Password Generator with Python

**[freeCodeCamp](https://daily.dev/sources/freecodecamp)** · 5 min read · 1 upvotes · 0 comments

## Summary

Python's random module is unsafe for passwords, so use the secrets module instead. random is a pseudo-random generator driven by a formula and a seed, which makes its output predictable if the seed is known or enough outputs are seen. secrets pulls from the operating system's entropy source, /dev/urandom on Linux and CryptGenRandom on Windows. The fix is a one-word swap from random.choice to secrets.choice. The tutorial then adds a loop with try/except that asks for a length and enforces a minimum of 8 characters.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.freecodecamp.org/news/how-to-build-a-secure-password-generator-with-python>

## Questions this post answers

### Why shouldn't I use Python's random module to generate passwords?

The random module is a pseudo-random number generator driven by a mathematical formula and a seed. If someone knows or guesses the seed, or observes enough outputs, the sequence becomes predictable. That is fine for shuffling a playlist or rolling dice in a game, but it is a major security risk for passwords.

_Developers learning secure Python habits follow security tutorials like this on daily.dev._

### How do I generate a cryptographically secure password in Python?

Use the built-in secrets module instead of random. It draws randomness from the operating system's entropy source, /dev/urandom on Linux and CryptGenRandom on Windows. Build a character pool from string.ascii_letters, digits and punctuation, then join secrets.choice(all_chars) calls over the desired length. The only change from the naive version is swapping random for secrets.

_Keeping up with secure coding practices in Python is easier with daily.dev._

## Similar posts on daily.dev

- [Selecting random values in Python](https://daily.dev/posts/selecting-random-values-in-python-n1k4qsk7m) · Planet Python · 0 upvotes · 0 comments
- [Generating Random Data in Python \(Guide\) Quiz – Real Python](https://daily.dev/posts/generating-random-data-in-python-guide-quiz-real-python-orm4t7lcd) · Real Python · 0 upvotes · 0 comments
- [Plenty of IDs](https://daily.dev/posts/plenty-of-ids-os1ut1q59) · The Daily WTF · 1 upvotes · 0 comments
- [Ruby Random Numbers: How to Generate Them With Examples](https://daily.dev/posts/ruby-random-numbers-how-to-generate-them-with-examples-flyostzyh) · RUBYLAND · 0 upvotes · 0 comments
- [Has Your Password Leaked? Check It, Then Replace It](https://daily.dev/posts/has-your-password-leaked-check-it-then-replace-it-pmctpjv4w) · Security Boulevard · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#appsec](https://daily.dev/tags/appsec)

[View this post on daily.dev](https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Build a Secure Password Generator with Python","url":"https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5"},"datePublished":"2026-12-01T01:07:28.476Z","dateModified":"2026-12-01T01:07:48.141Z","description":"Python's random module is unsafe for passwords, so use the secrets module instead. random is a pseudo-random generator driven by a formula and a seed, which...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/03be2bc6ea033458eb3503bbed803b6f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/03be2bc6ea033458eb3503bbed803b6f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"freeCodeCamp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"freeCodeCamp","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp","url":"https://daily.dev/sources/freecodecamp"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,appsec","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"freeCodeCamp","item":"https://daily.dev/sources/freecodecamp"},{"@type":"ListItem","position":3,"name":"How to Build a Secure Password Generator with Python"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-build-a-secure-password-generator-with-python-rattc1dl5#faq","mainEntity":[{"@type":"Question","name":"Why shouldn't I use Python's random module to generate passwords?","acceptedAnswer":{"@type":"Answer","text":"The random module is a pseudo-random number generator driven by a mathematical formula and a seed. If someone knows or guesses the seed, or observes enough outputs, the sequence becomes predictable. That is fine for shuffling a playlist or rolling dice in a game, but it is a major security risk for passwords. Developers learning secure Python habits follow security tutorials like this on daily.dev."}},{"@type":"Question","name":"How do I generate a cryptographically secure password in Python?","acceptedAnswer":{"@type":"Answer","text":"Use the built-in secrets module instead of random. It draws randomness from the operating system's entropy source, /dev/urandom on Linux and CryptGenRandom on Windows. Build a character pool from string.ascii_letters, digits and punctuation, then join secrets.choice(all_chars) calls over the desired length. The only change from the naive version is swapping random for secrets. Keeping up with secure coding practices in Python is easier with daily.dev."}}]}
```

