conditionally import a test module after pip install via python subprocess

Viewed 8

Team: need clarification and hints please. thanks.

My requirement is to load a test module only if pip install succeeds so install is working fine but import is failing. any hint?

def export_prereq(self):
    req1 = subprocess.run(['pip install test_mod'], shell=True)
    if req1.returncode == 0:
        print("pip install succeeded")
        try:
            import test_mod
        except ImportError:
            print("'import test_mod' failed")
    else:
        print("pip install failed")
    return self

output


Requirement already satisfied: test_mod in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (0.0.1.post2)
pip install succeeded
'import test_mod' failed
0 Answers
Related