Eli Johnson
Eli Johnson

Reputation: 360

Environment variable in conda is not aligning with os.environ, possibly because of un-escaped dollar-sign

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:

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.

Upvotes: 0

Views: 67

Answers (2)

Eli Johnson
Eli Johnson

Reputation: 360

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.

Upvotes: 0

Walter Mitty
Walter Mitty

Reputation: 18940

This is a desperate measure. I hope it helps.

Try setting the offending powershell variable to its own name. Something like:

$ValueoftheVariable = '$ValueoftheVariable'

You might have to do his in your profile or something like that.

Then, when powershell evaluates the variable, it yields same value as before.

If your code works ok, this could be a workaround. More likely, it will just allow you to uncover the real problem.

Upvotes: 0

Related Questions