{"id":8715,"library":"tinynetrc","title":"tinynetrc","description":"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.","status":"maintenance","version":"1.3.1","language":"en","source_language":"en","source_url":"https://github.com/sloria/tinynetrc","tags":["netrc","credentials","authentication","config","file-parsing"],"install":[{"cmd":"pip install tinynetrc","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"Netrc","correct":"from tinynetrc import Netrc"}],"quickstart":{"code":"import os\nfrom tinynetrc import Netrc\n\n# Define the path to the .netrc file (defaults to ~/.netrc)\nnetrc_path = os.path.expanduser('~/.netrc')\n\n# Example: Read and modify a .netrc file using a context manager\n# The file will be automatically saved on exit from the 'with' block\ntry:\n    with Netrc(netrc_path) as netrc:\n        # Add or update credentials for a machine\n        netrc['api.example.com'] = {'login': 'myuser', 'password': os.environ.get('API_EXAMPLE_PASSWORD', 'default_password')}\n        print(f\"Credentials for api.example.com set to: {netrc['api.example.com']}\")\n\n        # Access credentials for an existing machine\n        if 'api.heroku.com' in netrc:\n            heroku_creds = netrc['api.heroku.com']\n            print(f\"Heroku credentials: {heroku_creds['login']} / {'*' * len(heroku_creds['password'])}\")\n        else:\n            print(\"No credentials found for api.heroku.com\")\n\n        # Delete an entry\n        if 'old.host.com' in netrc:\n            del netrc['old.host.com']\n            print(\"Removed old.host.com entry.\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n\n# Example: Explicitly load and save\ntry:\n    netrc = Netrc(netrc_path)\n    netrc.load()\n    if 'another.service.com' not in netrc:\n        netrc['another.service.com'] = {'login': 'serviceuser', 'password': 'servicepass'}\n        netrc.save()\n        print(\"Added and saved credentials for another.service.com\")\nexcept Exception as e:\n    print(f\"An error occurred during explicit load/save: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure the `.netrc` file has appropriate permissions, e.g., `chmod 600 ~/.netrc`.","message":"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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade to Python 3.5 or newer for full support and security patches.","message":"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.","severity":"deprecated","affected_versions":"< 3.5"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change the permissions of the `.netrc` file to `600` (owner read/write only): `chmod 600 ~/.netrc`.","cause":"The `.netrc` file has file permissions that are too broad, allowing access to users other than the owner.","error":"NetrcParseError: ~/.netrc access too permissive: access permissions must restrict access to only the owner (/home/user/.netrc, line 1)"},{"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:`).","cause":"Attempting to access credentials for a machine (`machine.example.com`) that does not have an entry in the loaded `.netrc` file.","error":"KeyError: 'machine.example.com'"},{"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.","cause":"The `Netrc` object was initialized to load from a `.netrc` file that does not exist, and no explicit creation logic was used.","error":"FileNotFoundError: [Errno 2] No such file or directory: '/home/user/.netrc'"}]}