How to test for situation where a specific library is missing in Python
Question: I have some packages that have soft dependencies on other packages with a fall
back to a default (simple) implementation.

The problem is that this is very hard to test for using unit tests. I could
set up separate virtual environments, but that is hard to manage.

Is there a package or a way to achieve the following: have

    
    
    import X
    

work as usual, but

    
    
    hide_package('X')
    import X
    

will raise an ImportError.

I keep having bugs creep into the fall-back part of my code because it is hard
to test this.


Answer: One way is to edit sys.path, especially if your packages install into
different directories/zipfiles (e.g. if you are using eggs). Before importing,
drop the ones you don't want from sys.path.

If that's not feasible (because all components live in a single sys.path
entry), you can hack suppression into the packages themselves. E.g. have a
global variable (environment, or something patched into the sys module) list
the packages whose import you want to fail:

    
    
    sys.suppressed_packages=set()
    sys.suppressed_packages.add('X')
    

Then, in each package, explicitly raise an ImportError:

    
    
    # X.py
    import sys
    if 'X' in sys.suppressed_packages:
        raise ImportError, 'X is suppressed'
    

Of course, instead of using the sys module, you can make your own
infrastructure for that, along with a hide_package function.

