0

I have a script which I execute within a python3 virtual environment and inside this script there is a subrocess.Popen command which starts an application that works only with python2. Is there a way to change the environment only for this process such that this subprocess finds python2 only?

So far I have tried the following which haven't work out.

os.environ["PATH"] = "/path/to/python2/bin:"+os.environ["PATH"] 
Alejandro
  • 879
  • 11
  • 27
  • Did you try to use subprocess as follow: `subprocess.Popen(["path/to/python2/exectubale", "path/to/your/script"])` ? – Sylvaus May 30 '20 at 02:05
  • the problem is the subprocess command I use is like: Popen(['roslaunch", "pkg1", "launchfile.launch"]) and it is the roslaunch command that will use python under the hood. – Alejandro May 30 '20 at 12:18
  • Does this https://stackoverflow.com/questions/2231227/python-subprocess-popen-with-a-modified-environment help ? – PN7 May 31 '20 at 14:59

1 Answers1

0

Easiest solution I found:

venv = os.environ.copy()
venv.update({"PATH": os.environ["PATH"].replace(venv['VIRTUAL_ENV'], 
                     'other/python/version/path')}) # might have to tweak this a bit only


proc2 = subprocess.Popen(["cmd",
                         env=venv,
                         stdout=sys.stdout,
                         stderr=sys.stderr,
                             )
Alejandro
  • 879
  • 11
  • 27