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.
Install & Compatibility
Where this runs
tested against v2.178.0 · 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
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 1.264s · 470.5MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 20.9s · import 1.214s · 471MB
489MB installed
● package 489MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
config
✓ import oci.config
✗ from oci import config
The 'config' module is typically imported directly as oci.config, not from the top-level 'oci' package.
IdentityClient
✓ from oci.identity import IdentityClient
ObjectStorageClient
✓ from oci.object_storage import ObjectStorageClient
ServiceError
✓ from oci.exceptions import ServiceError
Paginators
✓ import oci.pagination
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.
import oci
import os
try:
# Load configuration from the default location (~/.oci/config)
# The 'DEFAULT' profile is used if not explicitly specified.
config = oci.config.from_file()
# You can specify a different profile or file:
# config = oci.config.from_file(file_location="~/.oci/config", profile_name="MY_PROFILE")
except oci.exceptions.ConfigFileNotFound:
print("WARNING: OCI config file not found.")
print("Please ensure your OCI config file is set up at ~/.oci/config and contains a 'DEFAULT' profile.")
print("Alternatively, provide credentials via environment variables (e.g., OCI_CONFIG_FILE, OCI_PROFILE) or use Instance Principals for OCI instances.")
exit(1) # Cannot proceed without configuration
# Initialize the IdentityClient for interacting with Identity services
identity_client = oci.identity.IdentityClient(config)
try:
# The 'user' OCID is part of the loaded config (from the API key details).
user_ocid = config["user"]
# Get the current authenticated user's details
user = identity_client.get_user(user_ocid).data
print(f"Successfully authenticated as user: {user.name} (OCID: {user.id})")
print(f"Region: {config['region']}")
# Example of another common operation: List the root compartment details
# root_compartment = identity_client.get_compartment(config["tenancy"]).data
# print(f"Root Compartment: {root_compartment.name} (OCID: {root_compartment.id})")
except oci.exceptions.ServiceError as e:
print(f"OCI Service Error encountered (Code: {e.code}): {e.message}")
if e.code == "NotAuthenticated":
print("Authentication failed. Please check your OCI configuration (~/.oci/config), API key, or credentials.")
elif e.code == "InvalidParameter":
print(f"The user OCID '{user_ocid}' might be invalid or you lack permissions to view it.")
print("To troubleshoot, ensure the user OCID in your config file is correct and the API key is valid.")
exit(1)
except Exception as e:
print(f"An unexpected error occurred: {e}")
exit(1)
oci --version
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'oci'
The 'oci' Python package is not installed in the current Python environment or is not accessible via the Python path.
fixInstall the OCI Python SDK using pip: `pip install oci` or `pip3 install oci` if using Python 3 specifically.
Could not find config file at ~/.oci/config
The OCI SDK cannot locate the configuration file required for authentication, which defaults to `~/.oci/config`.
fixEnsure the `~/.oci/config` file exists with correct credentials (user, fingerprint, tenancy, region, key_file) or provide the configuration programmatically. Use `oci setup config` if the OCI CLI is installed to create it, or manually create the file with the necessary details.
oci.exceptions.ServiceError: {'status': 404, 'code': 'NotAuthorizedOrNotFound'
This error indicates either that the requested OCI resource does not exist, or the authenticated user/principal lacks the necessary IAM permissions to access it. OCI intentionally obfuscates the exact reason for security.
fixVerify the OCID of the resource you are trying to access is correct. Then, review your IAM policies to ensure the user, group, or dynamic group associated with your authentication has the required permissions for the specific resource and compartment.
AttributeError: module 'oci.identity' has no attribute 'identityClient'
This error typically occurs due to incorrect capitalization when instantiating OCI client objects; OCI SDK client classes use PascalCase (e.g., `IdentityClient`).
fixCorrect the client class name to use proper capitalization: `identity_client = oci.identity.IdentityClient(config)`.
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
This error usually indicates an issue with SSL certificate validation, often encountered in corporate environments behind a proxy or with outdated/missing root certificates.
fixIn a corporate proxy environment, configure the `REQUESTS_CA_BUNDLE` environment variable to point to a custom certificate bundle, or set `OCI_CLI_CERT_BUNDLE` to a custom CA certificate file. Alternatively, update your operating system's root certificates.
Audit
Dependencies
No dependency data recorded yet.