0

I'm installing some open source software in Python that makes use of the errno module and want to make it installable across multiple environments whose errno modules might differ. What is a good pattern for this?

Here's my specific case. The code uses errno.ETIME. However, ETIME is not in every version of errno. For example, in Anaconda for Python 2 on Windows. (Or maybe this is specific to my Windows installation?)

Now, one way around it is to check for it and assign it if it's not there:

import errno
if 'ETIME' not in dir(errno):
    errno.ETIME = 127

But that feels hacky. Is there a best practice for this? Thanks.

1 Answers1

0

Use a global variable:

ERRNO_ETIME = 127 # Or whatever your default is.

try:
    from errno import ETIME
    ERRNO_ETIME = ETIME # If found, this will just override with correct ETIME if it was found
except ImportError:
    pass # ImportError would be thrown if it wasn't found, and you'd just keep your 127

You could put this in a function:

def get_etime():
    try:
        from errno import ETIME
        return ETIME
    except ImportError:
        return 127

ERRNO_ETIME = get_etime()

This is similar to how django overrides settings.py with local_settings.py for config values specific to a machine, more on that here - Django Local Settings

Community
  • 1
  • 1
Bahrom
  • 4,752
  • 32
  • 41
  • Thanks, @Bahrom. Both of those require changing existing references to errno.ETIME. I am looking for the best way that avoids changing those references. – computational_linguist Apr 27 '16 at 04:49
  • @user3038796 not sure what you mean - you are not overriding the value of `errno.ETIME` itself - you're creating another variable. Do you want to avoid this? – Bahrom Apr 27 '16 at 04:51
  • yes, I'm trying to avoid creating another variable so that the existing references to it don't have to change – computational_linguist Apr 27 '16 at 05:09