Python netrc: parse a .netrc file for host authenticators
Answer: netrc parses the netrc format that the Unix ftp program and other FTP clients use.
Loading a netrc file
The class parses the netrc format used by the Unix ftp program and other FTP clients.
With no argument, the reader loads .netrc from the home directory determined by os.path.expanduser.
A missing file raises FileNotFoundError.
Syntactical problems raise NetrcParseError, whose diagnostic information includes the file name, line number, and terminating token.
Version 3.7 started using os.path.expanduser to find .netrc when no file argument is passed.
In version 3.10, netrc tries UTF-8 encoding before using locale specific encoding. Missing tokens default to an empty string, and tokens can contain arbitrary characters, like whitespace and non-ASCII characters.
POSIX permission checks and lookups
On a POSIX system, if you omit the path and the file contains passwords, NetrcParseError is raised when ownership or permissions are insecure.
Insecure means the file is owned by someone other than the process user, or that any other user can read or write it.
That check matches ftp and other programs that use .netrc, and it is available only on platforms that support os.getuid.
An anonymous login name does not trigger the security check.
authenticators(host) returns a (login, account, password) tuple for that host, or the default entry, or None if neither exists.
The hosts attribute maps host names to those tuples, with default stored under the pseudo-host name default.
macros maps macro names to string lists.
__repr__ writes the class data in netrc file format. Comments are discarded, and entries may be reordered.
See also
Claims
- id: C1 claim: The class parses the netrc format used by Unix ftp clients. source: S1 evidence: "parses and encapsulates the netrc file format used by"
- id: C2 claim: On POSIX, an insecure default .netrc with passwords is rejected. source: S2 evidence: "If no argument is specified on a POSIX system"
- id: C3 claim: authenticators returns a login, account, and password tuple. source: S2 evidence: "Return a 3-tuple"
Sources
- [S1] netrc — netrc file processing. Python Software Foundation. https://docs.python.org/3/library/netrc.html . License: PSF License Version 2. Accessed 2026-11-27.
- [S2] netrc documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/netrc.rst . License: PSF License Version 2. Accessed 2026-11-27.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, netrc — netrc file processing; CPython, netrc documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, netrc — netrc file processing; CPython, netrc documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python netrc: parse a .netrc file for host authenticators." iinventedlight Library, iil:note:d95mnxn0, version of 2026-11-27. https://iinventedlight.com/library/notes/python-netrc/. License: CC-BY-4.0.
@misc{iil_note_d95mnxn0,
author = {{iinventedlight}},
title = {{Python netrc: parse a .netrc file for host authenticators}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/python-netrc/},
note = {Permanent ID iil:note:d95mnxn0. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-netrc.schema.json (schema; CC-BY-4.0)
- python-netrc.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-netrc.md.