简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM