Search code examples
python-3.xpython-2.7subprocesspython-2to3

ImportError: cannot import name getoutput


I am using future to port code from Python 2 to Python 3.

Upon futurizing getoutput, the import changes from

from commands import getoutput to from subprocess import getoutput

And my code uses getoutput in testing a requirements file.

However, when I run the test, I get the following error:

from subprocess import getoutput
ImportError: cannot import name getoutput

How do I avoid this? Or is there any other alternative that could be used for futurizing getoutput from Python2 to Python3


Solution

  • You can get the major version of a Python installation using the sys.version_info object, which has a major attribute. You can then check this value to see if you're running on Python 2 or Python 3+.

    import sys
    
    if sys.version_info.major == 2:
        from commands import getoutput
    else:
        from subprocess import getoutput
    

    This will work if there are few conditional imports and other simple statements. Otherwise, you could look at a compatibility package like six, which is used for letting you run code in both 2 and 3 by giving you certain layers. Six contains a module six.moves, which contains six.moves.getoutput, which will resolve the getoutput properly. (It is equivalent to commands.getoutput in 2.7 and subprocess.getoutput in 3+).

    Another alternative would be to use a try-except block around your imports, and let it resolve itself.

    try:
        from subprocess import getoutput
    except ImportError:
        from commands import getoutput