{"id":20712902,"url":"https://github.com/thesis/optional_import","last_synced_at":"2026-04-18T16:34:27.596Z","repository":{"id":57449406,"uuid":"20941852","full_name":"thesis/optional_import","owner":"thesis","description":"Optional imports in Python","archived":false,"fork":false,"pushed_at":"2014-06-19T03:39:24.000Z","size":240,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2026-01-13T17:25:18.874Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thesis.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-17T22:39:05.000Z","updated_at":"2018-08-27T14:12:08.000Z","dependencies_parsed_at":"2022-09-14T07:31:09.717Z","dependency_job_id":null,"html_url":"https://github.com/thesis/optional_import","commit_stats":null,"previous_names":["cardforcoin/optional_import"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thesis/optional_import","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesis%2Foptional_import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesis%2Foptional_import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesis%2Foptional_import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesis%2Foptional_import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thesis","download_url":"https://codeload.github.com/thesis/optional_import/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesis%2Foptional_import/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31976587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T16:27:12.723Z","status":"ssl_error","status_checked_at":"2026-04-18T16:27:11.140Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T02:22:28.341Z","updated_at":"2026-04-18T16:34:27.579Z","avatar_url":"https://github.com/thesis.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":".. figure:: https://circleci.com/gh/cardforcoin/optional_import.png?circle-token=d834124e03717f6619b867f13c8a85f254298df5\n   :alt: Build status\n\n\noptional_import\n===============\n\nOptional imports in Python\n\n.. pypi - Everything below this line goes into the description for PyPI.\n\n\nUsage\n-----\n\nThis library contains only the context manager ``optional_import``:\n\n.. code:: python\n\n    \u003e\u003e\u003e from optional_import import optional_import\n\nA successful import works as usual:\n\n.. code:: python\n\n    \u003e\u003e\u003e with optional_import():\n    ...     import collections\n    \u003e\u003e\u003e type(collections)\n    \u003ctype 'module'\u003e\n\nIf the import does not exist, ``optional_import`` suppresses the\n``ImportError`` that would otherwise be raised.\n\n.. code:: python\n\n    \u003e\u003e\u003e import unicorns\n    Traceback (most recent call last):\n      ...\n    ImportError: No module named unicorns\n\n    \u003e\u003e\u003e with optional_import():\n    ...     import unicorns\n\n    \u003e\u003e\u003e unicorns\n    Traceback (most recent call last):\n      ...\n    NameError: name 'unicorns' is not defined\n\n\nExample: Django local settings\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nA common pattern in Django is to put default settings in ``settings.py``,\nput optional site-specific settings in ``settings_local.py``, and import\n``*`` from the local settings file if it exists.\n\n.. code:: python\n\n    with optional_import():\n        from .settings_local import *\n\n\nWhy not just catch ``ImportError``?\n-----------------------------------\n\nOptional imports can almost be achieved simply by catching ``ImportError``:\n\n.. code:: python\n\n    try:\n        import foo\n    except ImportError:\n        pass\n\nBut this approach introduces a problem: If ``foo`` exists but raises\n``ImportError``, we want that error to be raised, but instead it is\nswallowed by the ``except`` clause.\n\nWith ``optional_import``, the error is raised as desired. In the following\nexample, the ``bad`` module tries to import a nonexistent package ``unicorns``:\n\n.. code:: python\n\n    \u003e\u003e\u003e with optional_import():\n    ...     import bad\n    Traceback (most recent call last):\n      ...\n    ImportError: No module named unicorns\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthesis%2Foptional_import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthesis%2Foptional_import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthesis%2Foptional_import/lists"}