Search code examples
pythonsubprocesscondainteropenvironment

Best way to execute a python script in a given conda environment


I want to execute a python script executed.py from another python script trigger.py using the subprocess package. The executed.py must be ran in a different conda environment than trigger.py (let say executed_env and trigger_env). What is the best way to do that ? My current code of trigger.py is:

command = "python executed.py --option1 -dir /path/to/dir"
args = shlex.split(command)
my_subprocess = subprocess.Popen(args)

It returns an error since executed.py is ran in the trigger_env environment.


Solution

  • I did some research on any conda built-in methods and found the following. The first is a workaround, and the second the final solution built-in solution (not available at time of writing this post).

    1. conda-wrappers. Guilherme Melo created a wrappers for the python executables within a conda environment. If you set it as the python interpreter in your IDE, e.g. PyCharm, it will activate the conda environment from which it is called, and then call the python interpreter. Look here under section "Creating conda wrappers": https://pypi.org/project/exec-wrappers/

    2. conda run. A long discussion on the conda github page on a standard and fast way to execute a command inside an environment led to the implementation of a new command (actually a re-invocation as it was available before): conda run

    It is described in issue #7320 and will be released in conda-4.6 hopefully in October 2018!