From f7813a9385a70b1fcad88bcb150c4a1d7cc73b29 Mon Sep 17 00:00:00 2001 From: yuvipanda Date: Tue, 26 Jun 2018 17:38:56 -0700 Subject: [PATCH] Add function to install packages from pip too --- tests/test_conda.py | 15 +++++++++++++++ tljh/conda.py | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tests/test_conda.py b/tests/test_conda.py index fe0f737..a5a84e0 100644 --- a/tests/test_conda.py +++ b/tests/test_conda.py @@ -51,3 +51,18 @@ def test_ensure_packages(prefix): '-c', 'import numpy' ]) + + +def test_ensure_pip_packages(prefix): + """ + Test installing pip packages in conda environment + """ + conda.ensure_conda_env(prefix) + conda.ensure_conda_packages(prefix, ['pip']) + conda.ensure_pip_packages(prefix, ['numpy']) + # Throws an error if this fails + subprocess.check_call([ + os.path.join(prefix, 'bin', 'python'), + '-c', + 'import numpy' + ]) diff --git a/tljh/conda.py b/tljh/conda.py index a85280d..f1053b1 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -47,3 +47,16 @@ def ensure_conda_packages(prefix, packages): output = json.loads(filtered_output) if 'success' in output and output['success'] == True: return + + +def ensure_pip_packages(prefix, packages): + """ + Ensure pip packages are installed in the given conda prefix. + """ + abspath = os.path.abspath(prefix) + pip_executable = [os.path.join(abspath, 'bin', 'python'), '-m', 'pip'] + + subprocess.run(pip_executable + [ + 'install', + '--no-cache-dir', + ] + packages)