behavior of rmagic silently depending on installed packages

Issue #279 resolved
Laurent Gautier created an issue

Currently rmagic has a behavior that depends on the presence or absence of packages.

The beginning of rmagic.py has:

try:
    from rpy2.robjects import pandas2ri as py2ri
except ImportError:
    try:
        from rpy2.robjects import numpy2ri as py2ri
    except ImportError:
        # Give up on numerics
        py2ri = None

While this is providing convenience this is also introducing both a challenge for current unit tests (tests should test with and without the packages) and a possible apparent unpredictability of behavior (apparent: the behavior depends on the presence or absence or other packages without the other being explicitly informed).

An initial fix would be to decide on a hierarchy of dependencies (e.g., "have pandas" > "have numpy" > "other case") and issue warnings when going down the hierarchy at import time.

A second fix would be able to go down the hierarchy even in the presence of the dependencies.

The first fix seems like something minimal (in terms of code change) and make it to release 2.6.0.

How does it look like ? (@davclark)

Comments (2)

  1. Laurent Gautier reporter

    While rpy2 will not commit to have pandas (or even numpy) as a hard dependency (it can take advantage of them on a per-need basis), my understanding is that the ipython part of rpy2 (the "rmagic") is seeing "pandas" as a very strongly recommended package.

    Would anyone oppose to the following modification to the import sequence in rmagic.py ?

    import warnings
    try:
        from rpy2.robjects import pandas2ri as py2ri
    except ImportError:
        try:
            from rpy2.robjects import numpy2ri as py2ri
            warnings.warn(' '.join(("The Python package 'pandas' is strongly recommended when using `rpy2.ipython`.",
                                    "Unfortunately it could not be loaded, but at least we found 'numpy'.")))
        except ImportError:
            # Give up on numerics
            py2ri = None
            warnings.warn(' '.join(("The Python package 'pandas' is strongly recommended when using `rpy2.ipython`.",
                                    "Unfortunately it could not be loaded, and we did not manage to load 'numpy' either.")))
    
  2. Log in to comment