Why can't environmental variables set in python persist?

前端 未结 7 775
南旧
南旧 2020-11-27 03:55

I was hoping to write a python script to create some appropriate environmental variables by running the script in whatever directory I\'ll be executing some simulation code,

7条回答
  •  孤城傲影
    2020-11-27 04:18

    As others have pointed out, the reason this doesn't work is that environment variables live in a per-process memory spaces and thus die when the Python process exits.

    They point out that a solution to this is to define an alias in .bashrc to do what you want such as this:

    alias export_my_program="export MY_VAR=`my_program`"
    

    However, there's another (a tad hacky) method which does not require you to modify .bachrc, nor requires you to have my_program in $PATH (or specify the full path to it in the alias). The idea is to run the program in Python if it is invoked normally (./my_program), but in Bash if it is sourced (source my_program). (Using source on a script does not spawn a new process and thus does not kill environment variables created within.) You can do that as follows:

    my_program.py:

    #!/usr/bin/env python3
    
    _UNUSED_VAR=0
    _UNUSED_VAR=0 \
    << _UNUSED_VAR
    #=======================
    # Bash code starts here
    #=======================
    '''
    _UNUSED_VAR
    export MY_VAR=`$(dirname $0)/my_program.py`
    echo $MY_VAR
    return
    '''
    
    #=========================
    # Python code starts here
    #=========================
    print('Hello environment!')
    

    Running this in Python (./my_program.py), the first 3 lines will not do anything useful and the triple-quotes will comment out the Bash code, allowing Python to run normally without any syntax errors from Bash.

    Sourcing this in bash (source my_program.py), the heredoc (<< _UNUSED_VAR) is a hack used to "comment out" the first-triple quote, which would otherwise be a syntax error. The script returns before reaching the second triple-quote, avoiding another syntax error. The export assigns the result of running my_program.py in Python from the correct directory (given by $(dirname $0)) to the environment variable MY_VAR. echo $MY_VAR prints the result on the command-line.

    Example usage:

    $ source my_program.py
    Hello environment!
    $ echo $MY_VAR
    Hello environment!
    

    However, the script will still do everything it did before except exporting, the environment variable if run normally:

    $ ./my_program.py
    Hello environment!
    $ echo $MY_VAR
                                    <-- Empty line
    

提交回复
热议问题