Gulzar
Gulzar

Reputation: 27966

How to run a python script with its environment from another script with another environment?

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

Upvotes: 10

Views: 8177

Answers (1)

sassy_rog
sassy_rog

Reputation: 1097

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.

Upvotes: 6

Related Questions