9

I have two scripts: script1.py and script2.py

script1 has its environment (say, python 2), and script2 has its own environment (say, python 3).

How can I make script 1 invoke script 2, with its corresponding environment?

Thanks

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • so, did you solve? – Leonardo Miquelutti Aug 25 '20 at 18:00
  • @LeonardoMiquelutti no. Had to resort to creating a new process and communicating through a message bus. not a good solution, but a working solution is better the a beautiful nothing – Gulzar Sep 24 '20 at 08:32
  • I had to split my script into 3 parts (python 3, 2, and 3 again) but managed to run them all through a single command line. Totally agree with the beautiful nothing part @Gulzar :) – Leonardo Miquelutti Sep 25 '20 at 09:12

1 Answers1

6

A workaround I can think of right now is os.system used to execute other files.

example:

script1.py

#!/usr/bin/env python3
import os

os.system("script2.py")

and

script2.py

#!/usr/bin/env python2

print "script 2 running..."

print "script 2 running..." is great example since python2.X uses print without parentheses and python3.X uses print() with parentheses

It's important to take note of the shebangs (#!/usr/bin/env python3 and #!/usr/bin/env python2) on both scripts which point to the correct entepreters/env for both scripts

os.system can also be used with arguments, like os.system("script2.py data.txt"), which is great if you want to run another script with arguments.

Community
  • 1
  • 1
sassy_rog
  • 1,077
  • 12
  • 30