{"id":1597,"library":"oci","title":"Oracle Cloud Infrastructure Python SDK","description":"The Oracle Cloud Infrastructure Python SDK (oci) provides a comprehensive set of APIs to interact with OCI services, allowing Python applications to manage cloud resources. Currently at version 2.170.0, it receives frequent updates to support new services, features, and API versions.","status":"active","version":"2.170.0","language":"en","source_language":"en","source_url":"https://github.com/oracle/oci-python-sdk","tags":["cloud","oracle","oci","sdk","infrastructure"],"install":[{"cmd":"pip install oci","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The 'config' module is typically imported directly as oci.config, not from the top-level 'oci' package.","wrong":"from oci import config","symbol":"config","correct":"import oci.config"},{"symbol":"IdentityClient","correct":"from oci.identity import IdentityClient"},{"symbol":"ObjectStorageClient","correct":"from oci.object_storage import ObjectStorageClient"},{"symbol":"ServiceError","correct":"from oci.exceptions import ServiceError"},{"symbol":"Paginators","correct":"import oci.pagination"}],"quickstart":{"code":"import oci\nimport os\n\ntry:\n    # Load configuration from the default location (~/.oci/config)\n    # The 'DEFAULT' profile is used if not explicitly specified.\n    config = oci.config.from_file()\n    # You can specify a different profile or file:\n    # config = oci.config.from_file(file_location=\"~/.oci/config\", profile_name=\"MY_PROFILE\")\nexcept oci.exceptions.ConfigFileNotFound:\n    print(\"WARNING: OCI config file not found.\")\n    print(\"Please ensure your OCI config file is set up at ~/.oci/config and contains a 'DEFAULT' profile.\")\n    print(\"Alternatively, provide credentials via environment variables (e.g., OCI_CONFIG_FILE, OCI_PROFILE) or use Instance Principals for OCI instances.\")\n    exit(1) # Cannot proceed without configuration\n\n# Initialize the IdentityClient for interacting with Identity services\nidentity_client = oci.identity.IdentityClient(config)\n\ntry:\n    # The 'user' OCID is part of the loaded config (from the API key details).\n    user_ocid = config[\"user\"]\n    \n    # Get the current authenticated user's details\n    user = identity_client.get_user(user_ocid).data\n    print(f\"Successfully authenticated as user: {user.name} (OCID: {user.id})\")\n    print(f\"Region: {config['region']}\")\n\n    # Example of another common operation: List the root compartment details\n    # root_compartment = identity_client.get_compartment(config[\"tenancy\"]).data\n    # print(f\"Root Compartment: {root_compartment.name} (OCID: {root_compartment.id})\")\n\nexcept oci.exceptions.ServiceError as e:\n    print(f\"OCI Service Error encountered (Code: {e.code}): {e.message}\")\n    if e.code == \"NotAuthenticated\":\n        print(\"Authentication failed. Please check your OCI configuration (~/.oci/config), API key, or credentials.\")\n    elif e.code == \"InvalidParameter\":\n        print(f\"The user OCID '{user_ocid}' might be invalid or you lack permissions to view it.\")\n    print(\"To troubleshoot, ensure the user OCID in your config file is correct and the API key is valid.\")\n    exit(1)\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n    exit(1)\n","lang":"python","description":"This quickstart initializes an OCI IdentityClient using configuration loaded from the default `~/.oci/config` file. It then attempts to fetch the current authenticated user's details to verify connectivity and authentication. A missing configuration file or authentication failure will lead to an error message and exit, guiding the user on how to resolve it."},"warnings":[{"fix":"Use `oci.pagination.list_call_get_all_results()` or the `.all()` method on the response object (where available) to iterate through all pages. Example: `for item in oci.pagination.list_call_get_all_results(client.list_resources, compartment_id=ocid).data:`","message":"When performing 'list' operations (e.g., `list_instances`, `list_buckets`), methods typically return only the first page of results. To retrieve all items, you must explicitly use paginators.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `~/.oci/config` is correctly set up with the required `user`, `fingerprint`, `key_file`, `tenancy`, and `region` properties. Verify permissions for the API key and ensure the profile name matches what's used in `oci.config.from_file()`.","message":"The most common issue is misconfiguration of the OCI SDK. This includes missing `~/.oci/config` file, incorrect profile name, invalid API key paths, or incorrect tenancy/user OCIDs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap your OCI API calls in `try...except oci.exceptions.ServiceError as e:` blocks to handle specific error codes (e.g., `e.code == \"NotAuthenticated\"`) gracefully and provide user-friendly feedback.","message":"OCI API calls can raise `oci.exceptions.ServiceError` for issues like authentication failures, permission errors, or invalid parameters. Not catching this specific exception can lead to unhandled runtime errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If your application relies on specific retry behavior, explicitly configure a `retry_strategy` when initializing clients (e.g., `identity_client = oci.identity.IdentityClient(config, retry_strategy=oci.retry.NoneRetryStrategy())`) or update your code to account for the new default exponential backoff.","message":"The default retry strategy for API calls changed from a fixed retry count to an exponential backoff strategy in versions greater than 2.24.0. This might alter the retry behavior of existing applications.","severity":"breaking","affected_versions":">2.24.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}