Security and Identifier Modules

netrc

Parses netrc configuration files containing host-specific login details and optional defaults. Explicitly supply the path to a synthetic file in tests; the default lookup may read real credentials from the user’s home directory. These files can contain plaintext secrets, so restrict access and do not log parsed credential tuples.

Formal Syntax & Structure

python syntaxpython
import netrc

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from netrc import netrcfrom tempfile import TemporaryDirectoryfrom pathlib import Pathwith TemporaryDirectory() as folder:    p = Path(folder)/"fixture.netrc"    p.write_text("machine example.invalid login demo password placeholder\n", encoding="utf-8")    print(netrc(str(p)).authenticators("example.invalid")[0])
Python Reference