0

I am trying to source a shell script from python.

I have tried to do that in several ways.

One of which:

def source(script, update=1):
    pipe = Popen(f". {script}; env", stdout=PIPE, shell=True)
    data = pipe.communicate()[0]
    env = dict(str(line).split("=", 1) for line in data.splitlines())
    if update:
        environ.update(env)
    return env

I have also tried exec(file) and os.system. None of which seems to give me the ability to keep the environment variables on the shell from which I run the python script. Does anyone of you have any idea how I can do that?

MortyAndFam
  • 159
  • 1
  • 8
  • 2
    The Python script runs inside its own process, which is a fork of the original process; you can't affect the parent shell directly. – merlin2011 Jul 02 '18 at 23:59
  • The only way you can do it is the same way `ssh-agent` does it: To require the shell that started you to invoke your process in such a way as to read and evaluate its output. – Charles Duffy Jul 02 '18 at 23:59
  • 1
    Related: [Is it possible to change the environment of a parent process in Python?](https://stackoverflow.com/questions/263005/is-it-possible-to-change-the-environment-of-a-parent-process-in-python). Not a Python limitation, a UNIX limitation -- you can't do this (with well-documented and supported interfaces) without the parent process's involvement in C either. – Charles Duffy Jul 03 '18 at 00:01

0 Answers0