{"id":5387,"library":"pyactiveresource","title":"PyActiveResource","description":"PyActiveResource is a Python port of Ruby's ActiveResource project, providing an object-relational mapping (ORM) for RESTful web services. It aims to simplify interaction with REST APIs by mapping remote resources to local Python objects, following a 'convention over configuration' philosophy. The current version is 2.2.2, with its last release in February 2021, suggesting a stable but slow release cadence.","status":"active","version":"2.2.2","language":"en","source_language":"en","source_url":"https://github.com/Shopify/pyactiveresource/","tags":["REST","ORM","ActiveResource","API Client","Shopify"],"install":[{"cmd":"pip install pyactiveresource","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for Python 2/3 compatibility, explicitly required by the library's setup.py.","package":"six","optional":false}],"imports":[{"note":"The common convention shown in documentation is to import the module as 'ar'.","wrong":"from pyactiveresource.activeresource import ActiveResource","symbol":"ActiveResource","correct":"from pyactiveresource import activeresource as ar\n# Then use ar.ActiveResource"},{"note":"Base exception for connection issues, and its subclasses like `UnauthorizedAccess`.","symbol":"ConnectionError","correct":"from pyactiveresource.connection import ConnectionError"}],"quickstart":{"code":"import os\nfrom pyactiveresource import activeresource as ar\n\n# Configure your API base URL (e.g., from environment variables)\nAPI_BASE_URL = os.environ.get('PYACTIVERESOURCE_API_URL', 'http://api.example.com/v1')\n\n# Define a resource class that maps to your REST endpoint\nclass Person(ar.ActiveResource):\n    _site = API_BASE_URL\n    # Optionally set headers for authentication, e.g., Bearer token\n    # _headers = {'Authorization': f'Bearer {os.environ.get(\"API_TOKEN\", \"\")}'}\n\n# Example usage:\ntry:\n    # Find all people\n    all_people = Person.find()\n    print(f\"Found {len(all_people)} people.\")\n    for p in all_people:\n        print(f\"ID: {p.id}, Name: {p.name if hasattr(p, 'name') else 'N/A'}\")\n\n    # Find a specific person by ID\n    if all_people:\n        first_person_id = all_people[0].id\n        person = Person.find(first_person_id)\n        print(f\"\\nFound person: {person.name if hasattr(person, 'name') else 'N/A'} (ID: {person.id})\")\n\n        # Create a new person\n        new_person = Person(name='Alice', age=30)\n        if new_person.save():\n            print(f\"\\nCreated new person: {new_person.name} (ID: {new_person.id})\")\n        else:\n            print(f\"Failed to create person. Errors: {new_person.errors}\")\n\nexcept ar.ResourceNotFound:\n    print(f\"Resource not found at {API_BASE_URL}.\")\nexcept ar.ConnectionError as e:\n    print(f\"Connection error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to define a resource class by inheriting from `pyactiveresource.activeresource.ActiveResource` and setting its `_site` attribute to your REST API's base URL. It then shows how to use class methods like `find()` to retrieve resources and instance methods like `save()` to create new ones, encapsulating typical REST interactions. It also includes basic error handling for common `pyactiveresource` exceptions."},"warnings":[{"fix":"Thoroughly test with your target Python version. Consider evaluating alternatives if facing compatibility issues, as the library may not be actively maintained for newer Python versions.","message":"The PyPI project classifiers officially list support only up to Python 3.4. While it uses `six` for Python 2/3 compatibility, using `pyactiveresource` with modern Python versions (3.5+) might encounter unexpected issues or require manual patching.","severity":"breaking","affected_versions":"<=2.2.2 (all versions)"},{"fix":"Ensure your API endpoints and data structures adhere to RESTful conventions expected by ActiveResource. Consult Ruby's ActiveResource documentation for the underlying philosophy, or explicitly configure `_site`, `_headers`, and custom methods when conventions cannot be followed.","message":"PyActiveResource relies heavily on 'convention over configuration', similar to Ruby's ActiveResource. It expects RESTful conventions like pluralized resource names (e.g., 'Person' maps to '/people'), standard HTTP verbs (GET for find, POST for create, PUT for update), and a specific JSON request/response format. Deviating from these conventions without explicit configuration will likely lead to errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully manage the `_site` URL and ensure consistent path construction. Double-check that `_site` ends without a slash if your resource names are expected to provide the initial path component, or vice versa, to avoid malformed URLs. Always inspect the actual HTTP requests being sent.","message":"The library's implicit URL construction can be a source of errors, especially with trailing slashes or complex API paths. Issues have been reported where incorrectly configured `_site` or resource paths lead to malformed URLs (e.g., double slashes) resulting in `ResourceNotFound` or `BadRequest` exceptions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Prefer using the `ActiveResource` object's public interface (`resource.attribute = value`, `resource.save()`) for data manipulation. If direct JSON loading is necessary, ensure that the data is thoroughly pre-processed to match the expected object structure, and use `resource.load(data)` if available, rather than internal `_update` methods.","message":"Direct manipulation of internal methods, such as `_update`, with raw JSON dictionaries, can lead to `TypeError` (e.g., 'cannot use a string pattern on a bytes-like') or other serialization/deserialization issues if the input data doesn't precisely match the library's internal expectations for attribute assignment, especially with nested objects or different string encodings.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}