I am trying to set an environment variable SOME_VAR
in a conda environment. The value begins with a $
character. Initially I didn't properly single-quote the variable value, which might have caused a silent error in powershell because that character has special meaning. Later I thought I'd fixed the problem by re-assigning the variable properly, executing the following commands in anaconda powershell
conda env config vars set SOME_VAR='$ValueOfTheVariable!'
conda env config vars list -n my_env
Conda then properly displays the full SOME_VAR=$ValueOfTheVariable!
. But Spyder, loading that environment, only sees the following:
In [4]: os.environ.get('SOME_VAR')
Out[4]: '!'
Specifically, anaconda displays the whole variable, but Spyder and os
only see the trailing exclamation point. All other environment variables are in agreement.
Things I've tried:
os
, then restarting Spyder (the offending version reappears)$
(per the second answer here)Even after unsetting the variable in powershell, the variable persists in os.environ
, making me think it's looking somewhere else and finding that version first before getting to my conda environment. But I never set that variable anywhere besides conda.
After some troubleshooting I figured out what was happening.
Essentially, if you're going to set environment variables in conda/powershell and use them through os
, then certain special characters may have to be escaped twice. Once to avoid errors when setting in powershell, and the other so that os
reads them properly.
Through some testing, it doesn't appear you can do this with a backslash (at least not any of the ways I tried), but you can do it with backticks.
conda env config vars set MY_VAR='`$ValueOfTheVariable!'
For conda, the single quotes escape the $
. This is passed through to os
in a way that allows the backtick to escape the dollar sign again.
This may still cause some discrepancies if you ever have to reference the variable in powershell / outside of python, but I don't have any use cases like that.