Python os.path.expandvars only works for some variables

前端 未结 1 932
误落风尘
误落风尘 2021-01-28 17:44

Running os.path.expandvars fails for some env variables. Seems too basic to be real.

$ echo $HOSTTYPE 
x86_64
$ echo $HOME     
/labhome/eladw
$ pyt         


        
相关标签:
1条回答
  • 2021-01-28 18:13

    Environment variables set in a shell are not automatically exported to subprocesses. Just because your shell has a HOSTTYPE variable, doesn't mean this is variable is visible to subprocesses.

    Export the variable first:

    $ export HOSTTYPE
    

    You can combine setting and exporting a variable in one step with:

    $ export HOSTTYPE=x86_64
    

    Demo:

    $ HOSTTYPE=x86_64
    $ python -c 'import os; print os.path.expandvars("$HOSTTYPE")'
    $HOSTTYPE
    $ export HOSTTYPE
    $ python -c 'import os; print os.path.expandvars("$HOSTTYPE")'
    x86_64
    

    See Difference between environment variables and exported environment variables in bash.

    0 讨论(0)
提交回复
热议问题