Registry / serialization / tinynetrc

tinynetrc

JSON →
library1.3.1pypypi✓ verified 86d ago

tinynetrc is a Python library designed to read and write .netrc files. It extends the standard library's `netrc` module by adding crucial write functionality, resolving a known bug with .netrc file formatting, and parsing entries into more accessible dictionary values instead of tuples. The library is currently at version 1.3.1 and is in a maintenance status, with infrequent but stable updates.

pip install tinynetrc
INSTALL
IMPORT
SIG · TINYNETRC
T
tinynetrc
serializationpythonv1.3.1
Install
1.7s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.7s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Netrc
from tinynetrc import Netrc

The quickstart demonstrates reading, modifying, and saving a .netrc file using `tinynetrc`. It highlights the use of the context manager for automatic saving and direct dictionary-like access to machine entries. It also includes an example of explicit loading and saving.

import os from tinynetrc import Netrc # Define the path to the .netrc file (defaults to ~/.netrc) netrc_path = os.path.expanduser('~/.netrc') # Example: Read and modify a .netrc file using a context manager # The file will be automatically saved on exit from the 'with' block try: with Netrc(netrc_path) as netrc: # Add or update credentials for a machine netrc['api.example.com'] = {'login': 'myuser', 'password': os.environ.get('API_EXAMPLE_PASSWORD', 'default_password')} print(f"Credentials for api.example.com set to: {netrc['api.example.com']}") # Access credentials for an existing machine if 'api.heroku.com' in netrc: heroku_creds = netrc['api.heroku.com'] print(f"Heroku credentials: {heroku_creds['login']} / {'*' * len(heroku_creds['password'])}") else: print("No credentials found for api.heroku.com") # Delete an entry if 'old.host.com' in netrc: del netrc['old.host.com'] print("Removed old.host.com entry.") except Exception as e: print(f"An error occurred: {e}") # Example: Explicitly load and save try: netrc = Netrc(netrc_path) netrc.load() if 'another.service.com' not in netrc: netrc['another.service.com'] = {'login': 'serviceuser', 'password': 'servicepass'} netrc.save() print("Added and saved credentials for another.service.com") except Exception as e: print(f"An error occurred during explicit load/save: {e}")
Debug
Known issues
gotchaThe `.netrc` file requires strict file permissions (typically 0o600, i.e., owner read/write only). If the permissions are too permissive (e.g., readable by group or others), the underlying standard `netrc` module (and thus `tinynetrc`) may refuse to read the file, leading to `NetrcParseError` or silent failure to retrieve credentials.
fix
Ensure the `.netrc` file has appropriate permissions, e.g., `chmod 600 ~/.netrc`.
affects: All
gotchaThe standard library's `netrc` module has known limitations, such as a lack of write functionality and a bug in formatting, which `tinynetrc` explicitly addresses. Also, `netrc` historically handled multiple entries for the same host by taking the last one, which differs from other tools like `curl` that take the first. `tinynetrc` resolves entries into dictionary values, inherently providing a single entry per machine key.
fix
When migrating from direct `netrc` usage or expecting specific multi-entry behavior, review `tinynetrc`'s dictionary-based parsing. `tinynetrc` provides the intended fixes for standard library issues.
affects: All
deprecatedWhile `tinynetrc` officially supports Python >= 2.7, Python 2.7 reached its End-of-Life in January 2020. Running `tinynetrc` on Python 2.7 is not recommended due to security and compatibility issues with the broader Python ecosystem.
fix
Upgrade to Python 3.5 or newer for full support and security patches.
affects: < 3.5
Errors
Common errors & fixes
NetrcParseError: ~/.netrc access too permissive: access permissions must restrict access to only the owner (/home/user/.netrc, line 1)
The `.netrc` file has file permissions that are too broad, allowing access to users other than the owner.
fix
Change the permissions of the `.netrc` file to `600` (owner read/write only): `chmod 600 ~/.netrc`.
KeyError: 'machine.example.com'
Attempting to access credentials for a machine (`machine.example.com`) that does not have an entry in the loaded `.netrc` file.
fix
Verify the machine name is correct and present in your `.netrc` file, or implement logic to handle missing entries (e.g., `if 'machine.example.com' in netrc:`).
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/.netrc'
The `Netrc` object was initialized to load from a `.netrc` file that does not exist, and no explicit creation logic was used.
fix
Ensure the `.netrc` file exists in the specified path (typically `~/.netrc`) before attempting to read it, or use `tinynetrc`'s write capabilities to create it if it's missing.
Upgrade
Version history
1.3.1latest on PyPI · released Aug 15, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
1
Resources
tinynetrc — pip install tinynetrc · libregistry