Search code examples
pythontcsh

set environment in tcsh opened form python


I work in tc shell and have environment set script with commands like setenv foo bar. I use to run this script by command source set_env.sh and then, when environment is set, run compilation command make my_ target...

I would like to automate the whole process by executing it from Python by calling subprocess.Popen or even os.system. The troubles are:

  1. Shell opened form Python do not recognize command source. My assumption that /bin/bash shell is opened. How do I force Python to open /bin/tcsh?
  2. How do I preserve changed environment setting for second command (make my_ target...)?

Solution

  • If you use the subprocess package in Python then it won't run the shell by default, it will just launch a process (without using the shell). You can add shell=True, but this won't do you much good since environment variables are set for the current process only, and are not sent back to the parent process.

    Python starts a tcsh child process, and child processes can't affect parent processes.

    The solution would be to make your script print out the environment variables, and then parse the output of that in Python.

    So if your script prints something like:

    XX=one
    YY=two
    

    Then in Python do something along the likes of:

    for line in output.split('\n'):
        if line == '':
            continue
        k, v = line.split('=')
        os.environ[k] = v