10

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

3
  • so, did you solve? Commented Aug 25, 2020 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 Commented Sep 24, 2020 at 8: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 :) Commented Sep 25, 2020 at 9:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Will this also work in windows the same way? just specify the relevant path in the start of each script?
@Gulzar Possible with Python Launcher for Windows.
@Gulzar it should be

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.