Home > Blockchain >  Run Python script that affect parent shell (changes environment variables, runs other scripts, etc.)
Run Python script that affect parent shell (changes environment variables, runs other scripts, etc.)

Time:07-13

Recently I need to affect bash shell that runs python script from python script itseft. I develop a Python utility package that add some additional functionallity to pip. One of the workflows in this package needs to active Virtualenv to work as planned. Here is the problem.

When you run something like:

os.system('/bin/bash ./venv/bin/activate')

Or:

subprocess.Popen(['/bin/bash', './venv/bin/activate')

It doesn't do anything to the shell when the script is executed. Basically because these commands are executed in isolated processes (I guess) and therefore does not affect bash process itself.

Question: how can you affect parent shell that execute python script from inside the script (add some environments, run other script, etc.)?

Thanks in advance.

CodePudding user response:

how can you affect parent shell that execute python script from inside the script (add some environments, run other script, etc.)?

It is not possible to do that, unless your operating system is broken. Process isolation is one of the very basic concepts of an operating system.

Instead, resaerch what venv does and how it works and what activate script does and just add the proper directory to python module search path.

  • Related