Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

like image 425
Gulzar Avatar asked Nov 16 '22 10:11

Gulzar


1 Answers

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.

like image 173
sassy_rog Avatar answered Dec 17 '22 20:12

sassy_rog