{"id":9156,"library":"openshift-client","title":"OpenShift Client for Python","description":"The `openshift-client` library provides a Pythonic interface for interacting with OpenShift clusters, built on top of the official Kubernetes Python client. It simplifies common OpenShift operations like managing projects, applications, and resources. The current version is 2.0.5, and it has an active development cadence with parallel maintenance of 1.x and 2.x branches, though 2.x is the primary focus for new features.","status":"active","version":"2.0.5","language":"en","source_language":"en","source_url":"https://github.com/openshift/openshift-client-python","tags":["openshift","kubernetes","client","devops","automation"],"install":[{"cmd":"pip install openshift-client","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core API interaction with Kubernetes/OpenShift clusters.","package":"kubernetes","optional":false},{"reason":"Used for parsing kubeconfig files and other YAML configurations.","package":"PyYAML","optional":false},{"reason":"HTTP client for API communication.","package":"requests","optional":false},{"reason":"HTTP client library, dependency of requests.","package":"urllib3","optional":false}],"imports":[{"note":"The primary client object `OC` is located within the `openshift.client` module, not directly under `openshift`.","wrong":"import openshift","symbol":"OC","correct":"from openshift.client import OC"}],"quickstart":{"code":"import os\nfrom openshift.client import OC\n\n# Instantiate the OC client. It automatically uses your default kubeconfig context.\n# Or specify a kubeconfig path:\n# oc_client = OC(kubeconfig=os.environ.get('KUBECONFIG_PATH', '~/.kube/config'))\noc_client = OC()\n\n# Change to a specific project (namespace)\nproject_name = os.environ.get('OPENSHIFT_PROJECT', 'default')\noc_client.project(project_name)\n\nprint(f\"Current project: {oc_client.get_project_name()}\")\n\n# List all pods in the current project\ntry:\n    pods = oc_client.selector('pods').objects()\n    if pods:\n        print(f\"Found {len(pods)} pods in project '{project_name}':\")\n        for pod in pods:\n            print(f\"  - {pod.metadata.name} (Status: {pod.status.phase})\")\n    else:\n        print(f\"No pods found in project '{project_name}'.\")\nexcept Exception as e:\n    print(f\"Error listing pods: {e}\")\n\n# Example: Get a specific resource (if it exists)\n# try:\n#     deployment = oc_client.get('deployment', 'my-app-deployment')\n#     print(f\"Found deployment: {deployment.metadata.name}\")\n# except Exception as e:\n#     print(f\"Deployment not found or error: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize the `OC` client, switch to a specific OpenShift project (namespace), and list pods within that project. It assumes a `kubeconfig` file is available in the default location or specified via `KUBECONFIG_PATH` environment variable. It also shows how to retrieve the current project name and handle potential errors."},"warnings":[{"fix":"Migrate your environment and code to Python 3. This library now requires Python >=3.0.","message":"Python 2 support has been officially deprecated starting with versions 2.0.5 and 1.0.24. Future releases will drop Python 2 compatibility entirely.","severity":"breaking","affected_versions":">=2.0.5, >=1.0.24"},{"fix":"Ensure all datetime operations and comparisons in your code are consistently using timezone-aware datetime objects (e.g., using `pytz` or `zoneinfo` modules). Convert naive datetimes to aware ones before use, typically UTC or the timezone of your cluster.","message":"Starting with versions 2.0.2 and 1.0.22, the library internally uses timezone-aware datetime objects instead of timezone-naive `datetime.utcnow()`. This can lead to `TypeError: can't compare offset-naive and offset-aware datetimes` if your application code mixes naive and aware datetimes when interacting with API objects or their properties.","severity":"gotcha","affected_versions":">=2.0.2, >=1.0.22"},{"fix":"To use a specific context, instantiate with `OC.from_context('my-context-name')`. To use a different kubeconfig file, pass the path: `OC(kubeconfig='/path/to/my/kubeconfig')`.","message":"The `OC` client defaults to using the currently active context in your `kubeconfig` file. If you need to interact with a specific context or a different `kubeconfig` file, you must explicitly specify it.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change your import statement from `import openshift` or `from openshift import OC` to `from openshift.client import OC`.","cause":"The package `openshift-client` is installed, but the top-level import for the main client module is `openshift.client`, not just `openshift`.","error":"ModuleNotFoundError: No module named 'openshift'"},{"fix":"Verify the resource name and kind are correct. Ensure the `OC().project()` method has been called with the correct project name, or that the default context points to the desired project.","cause":"This typically means the requested resource (e.g., pod, deployment) does not exist in the specified project (namespace), or the project itself does not exist or is inaccessible.","error":"kubernetes.client.rest.ApiException: (404) Not Found"},{"fix":"Ensure all datetime objects involved in comparisons or operations are consistently timezone-aware. Use `datetime.now(timezone.utc)` or `datetime.fromtimestamp(ts, tz=timezone.utc)` to create timezone-aware datetimes.","cause":"You are attempting to compare a timezone-naive datetime object in your code with a timezone-aware datetime object returned by the `openshift-client` library (which became timezone-aware in recent versions).","error":"TypeError: can't compare offset-naive and offset-aware datetimes"}]}