Best way to execute python script in given conda environment

I want to execute a python script executed.pyfrom another python script trigger.pyusing a subprocess package. executed.pymust be performed in a different environment than trigger.py(say and ). What is the best way to do this? My current code is : executed_env trigger_envtrigger.py

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

It returns an error since it executed.pystarts in the environment . trigger_env

+4
source share
1 answer

If you just need to use another python, then I believe that you just need to use the full path to another python in command.

executed_env (.. source activate executed_env Linux) which python. , HOME/.conda/envs/executed_env/bin/python, , - .. command = "HOME/.conda/envs/executed_env/bin/python executed.py --option1 -dir /path/to/dir".

, executed.py python.

  • py27 conda create -n py27 python=2.7, trigger_env.
  • py35 conda create -n py35 python=3.5, executed_env.
  • python py35, source activate py35, which python ( EXECUTED_PYTHON ). source deactivate.
  • executed.py

    import sys
    print(sys.version)
    
  • trigger.py ( , )

    command = "EXECUTED_PYTHON executed.py --option1 -dir /path/to/dir"
    args = shlex.split(command)
    my_subprocess = subprocess.Popen(args)
    
  • trigger_env - source activate py27 python trigger.py.

  • 3.5.2 |Continuum Analytics, Inc. [...] (3.5 - ). , executed_env.
+1

Source: https://habr.com/ru/post/1668741/


All Articles