[Python-ideas] "maybe import"?

Nick Coghlan ncoghlan at gmail.com
Fri Dec 27 15:26:50 CET 2013


On 27 December 2013 19:34, Amber Yust <amber.yust at gmail.com> wrote:
> On Fri Dec 27 2013 at 12:33:11 AM, Guido van Rossum <guido at python.org>
> wrote:
>>
>> We shouldn't encourage optional dependencies. They often make code harder
>> to reason about or harder to read.
>
>
> Is there a better alternative for utilizing functionality that may vary in
> availability from platform to platform?

The most common approach I've seen is a compatibility module, so most
code in the application just does something like:

    from myapp.foo_compat import foo

and only foo_compat has the try/except logic. Depending on the number
of optional dependencies and the cost of importing them even when
they're not needed, this can be done on a per-dependency basis, or as
a single compatibility module that handles all the optional
dependencies.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list