Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading