Environment variables

I am on Windows (if that matters).
Inside my program i.e. myApp.run() I am checking content of a certain env. variable e.g.

env_var = get(ENV, "XYZ", "8073")

From command prompt/terminal in VS Code, before I execute julia, I call

set XYZ=5555

but then inside julia, inside myApp.run(), env_var is set to default 8073, which means it didn’t find any XYZ env. variable. And when I do

println(ENV)

inside that function, XYZ is not listed. However, other env. variables seem to be there.

How should I understand this? What variables are listed, and why is not my XYZ not there? Is there special julia command line argument to enforce env. variable?

Thank you.

1 Like

Did you also export the variable? In Bash (not POSIX): export xyz=1

By the way, you can use x in keys(ENV) and get via ENV[x] since ENV is a Dict

EDIT: This advise didn’t take into account that you’re on Windows. Then, I don’t have an idea what the problem could be

1 Like

Sorry for this small advertisement, but since you said, that you are checking variables inside your application, I presume you want to store some secrets in ENV and use them later. If it is a case, you can use ConfigEnv.jl. I am finalizing documentation, but main idea is that you save necessary environment data in .env (or any other file of your choice), put it in .gitignore and use it to populate ENV with dotenv() command in your application. It’s very simple and working on all platforms.

How do you execute julia in this scenario exactly?

In VSCode:
File → Preferences → Settings

In “Search settings” enter:
terminal.integrated.env

You find a entry called “Terminal › Integrated › Env: Windows”

Click on
“Edit in settings.json”

Add e.g.:

    "terminal.integrated.env.windows": {        
        "TEST": "XXX"
    }

Environment variable TEST is now propagated into any terminal you open in VSCode, including the Julia REPL started with View → Command Palette… “Julia: Start REPL”

2 Likes

Yes, thank you!!

I also found out why SET command didn’t work for me.
In VS Code, terminal is powershell, not command.com, thus SET XYZ=5555 does something very different there. Command to set env. variable there is

$Env:XYZ="5555"

1 Like

And than you need to start julia from inside this powershell, which isn’t the typical way how the REPL is started in VSCode.