Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute another Python instance from different instance

Tags:

python

exec

I currently have a script being executed by a program that comes with its own Python interpreter included. This version does not include libraries I require so I use an os.exec to execute a second script against a locally installed instance of 2.7.

However the second script, when executed manually, works perfectly and doesn't have PYTHONPATH defined. If I log the output of PYTHONPATH when the wrapper is executed and when the second script is loaded they both have PYTHONPATH initialized to the original interpreters, which happens to also be a 2.6 instance. This obviously has led me to believe I have some environmental variables being passed that I didn't anticipate, furthermore on 2 other systems this setup works 100% as expected whilst on this one system this behavior is happening. Is there a cleaner way I could create a wrapper to call the second script? If not how could I avoid this unnecessary passing of env variables.

like image 501
Draineh Avatar asked Sep 23 '26 07:09

Draineh


1 Answers

There are many variants of the os.exec* command:

execl(file, *args)
execle(file, *args, env)
execlp(file, *args)
execlpe(file, *args, env)
execv(path, args)
execve(path, args, env)
execvp(file, args)
execvpe(file, args, env)
  • l variants use *args, v variants use args.
  • p variants use the PATH environment variable to find file
  • e varients replace the environment. Non-e variants use the env of current process.

You could use one of the e variants, such as os.execle, to control the environment variables passed to the new process.

like image 93
unutbu Avatar answered Sep 25 '26 20:09

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!