tinynetrc
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.
Common errors
-
NetrcParseError: ~/.netrc access too permissive: access permissions must restrict access to only the owner (/home/user/.netrc, line 1)
cause The `.netrc` file has file permissions that are too broad, allowing access to users other than the owner.fixChange the permissions of the `.netrc` file to `600` (owner read/write only): `chmod 600 ~/.netrc`. -
KeyError: 'machine.example.com'
cause Attempting to access credentials for a machine (`machine.example.com`) that does not have an entry in the loaded `.netrc` file.fixVerify 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'
cause The `Netrc` object was initialized to load from a `.netrc` file that does not exist, and no explicit creation logic was used.fixEnsure 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.
Warnings
- gotcha The `.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.
- gotcha The 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.
- deprecated While `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.
Install
-
pip install tinynetrc
Imports
- Netrc
from tinynetrc import Netrc
Quickstart
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}")