# Environment Variables in interactive window

**URL:** <https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349>\
**Category:** General Usage\
**Created:** [October 27, 2022, 8:20am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349 "2022-10-27T08:20:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [October 27, 2022, 8:20am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/1 "2022-10-27T08:20:23Z")

</div>

In my `.zshrc` startup script I set some environment variables `export FOO="BAR"` which I’d like to use in Julia.

From a terminal I get

```nohighlight
julia> ENV["FOO"]
"BAR"

```

But from a VS code interactive session using shift+enter

```nohighlight
# foo.jl
ENV["FOO"]

```

Gives  
`ERROR: KeyError: key "FOO" not found` so it seems that the interactive session doesn’t execute the `.zshrc`. Previously this didn’t seem to be an issue, I’ve only noticed this since updating to MacOS 13.0, but I cannot confirm the update is related.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 27, 2022, 8:40am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/2 "2022-10-27T08:40:18Z")

</div>

VS Code executes `julia` directly, it doesn’t launch your shell to then execute `julia` from there.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 27, 2022, 8:40am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/3 "2022-10-27T08:40:54Z")

</div>

I don’t know if it’s respected in VS Code, but you could put ENV`s in your `startup.jl` file (which can be disabled when you run Julia):

> <https://stackoverflow.com/questions/52175314/where-is-juliarc-the-julia-startup-file>

```julia
ENV["FOO"] = "something"

```

> [@What is in your startup.jl?](https://discourse.julialang.org/t/what-is-in-your-startup-jl/18228):
>
> I now have a fair bit in my startup.jl I am wondering if anyone else also has cool things. What mine does: Load Revise and OhMyRepl. - installing them if they are not already installed Update all packages - It does this in a silenced worker processes, so as not to waste time - it is supposed to remove that worker afterwards but I am not sure that it does - My packages are now evergreen, like a webbrowser. I used to do stuff with setting stacktrace colors to not uses bold, because bold…

I don’t know about macOS, maybe something changed:

> **[macOS Ventura 13: The default interactive shell is now zsh | Code2care 2023](https://code2care.org/macos/macos-ventura-13-default-interactive-shell-is-now-zsh/)**
>
> What is The default interactive shell is now zsh on macOS Ventura? - Code2care 2023

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [October 27, 2022, 8:57am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/4 "2022-10-27T08:57:35Z")

</div>

> [@fredrikekre](#):
>
> VS Code executes `julia` directly, it doesn’t launch your shell to then execute `julia` from there.

I had a feeling this was the case. It must have been coincidence that I haven’t noticed this previously.

> [@Palli](#):
>
> I don’t know if it’s respected in VS Code, but you could put ENV`s in your `startup.jl` file (which can be disabled when you run Julia):

This would mean I’d be defining environment variables in two distinct places, which I’d rather avoid. Perhaps it’s the only practical option though.

> [@Palli](#):
>
> I don’t know about macOS, maybe something changed:

MacOS has been using zsh as default since [2019](https://www.theverge.com/2019/6/4/18651872/apple-macos-catalina-zsh-bash-shell-replacement-features), I’m not sure why that article makes it look like a 13.0 change.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 27, 2022, 8:58am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/5 "2022-10-27T08:58:32Z")

</div>

> [@this\_josh](#):
>
> I had a feeling this was the case. It must have been coincidence that I haven’t noticed this previously.

Maybe you used to launch VS Code from your shell? Then the environment will propagate.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 27, 2022, 10:55am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/6 "2022-10-27T10:55:36Z")

</div>

> [@this\_josh](#):
>
> This would mean I’d be defining environment variables in two distinct places

Yes, I was thinking could you run your .zshrc from Julia? You CAN… but it would clear out the ENVs when the process exits, as specified. I don’t know if there’s a workaround for that, I tried to google it, but gave up, though I was helpful already.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [October 27, 2022, 11:20am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/7 "2022-10-27T11:20:20Z")

</div>

I presumed this would be possible. Or storing all environment variables in a .json file which both .zshrc and startup.jl can load in.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 27, 2022, 11:37am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/8 "2022-10-27T11:37:01Z")

</div>

Perhaps [`direnv`](https://direnv.net/) might interest you, which can be used by both your shell, and also in VS Code. I wrote a blog post about `direnv` + Julia here: [Project specific Julia configuration with direnv | Fredrik Ekre](https://fredrikekre.se/posts/direnv-with-julia/).

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 27, 2022, 11:43am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/9 "2022-10-27T11:43:57Z")

</div>

Or simply such a file:

```julia
MYENV1=something
MYENV2=other

```

A start for something working in Julia (though I’m not sure why it fails):

```julia
julia> @eval "ENV[\"" * join(split("MYENV1=something", "="), "\"]=\"")
"ENV[\"MYENV1\"]=something"

```

Or you could use evalfile for legal Julia code, and something like above, in reverse, for the shell.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [October 28, 2022, 7:06am UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/10 "2022-10-28T07:06:09Z")

</div>

Thanks this

> [@fredrikekre](#):
>
> [`direnv`](https://direnv.net/)

Thanks I’ve installed this as project level environment variables seems helpful.

> [@Palli](#):
>
> Or simply such a file:

If I went down this approach I’d prefer an established file format, as this seems to involve having to reinvent the wheel a little bit

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 28, 2022, 12:28pm UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/11 "2022-10-28T12:28:00Z")

</div>

> [@this\_josh](#):
>
> involve having to reinvent the wheel a little bit

You can use [DotEnv.jl](https://github.com/vmari/DotEnv.jl) or [ConfigEnv.jl](https://github.com/Arkoniak/ConfigEnv.jl) which parses standard env files.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 28, 2022, 12:29pm UTC](https://discourse.julialang.org/t/environment-variables-in-interactive-window/89349/12 "2022-10-28T12:29:16Z")

</div>

> [@this\_josh](#):
>
> > [@Palli](#):
> >
> > Or simply such a file:
> 
> If I went down this approach I’d prefer an established file format

Well, I was thinking, that’s just standard shell/bash (and I guess `.zshrc`) code. I was aiming for as simple as possible, for you, at least on the shell side, and see it was just “standard env files”. I’ve never parsed JSON in a shell, it wasn’t invented (nor web browsers) when I started using shells… I’m sure it’s possible, just seemed like a heavier dependency (on both sides). Plain text files are as standard as they come (in Unix/LInux, and I guess by now in macOS), JSON (or e.g. XML) only needed for hierarchical, and extensibility which you don’t need there.

You wouldn’t even have to parse it on the shell side:

> <https://stackoverflow.com/questions/10823635/how-to-include-file-in-a-bash-shell-script>

```julia
. FILE # POSIX compliant

```

Actually I forget export, and it should still be trivial to parse (and exclude that) on the Julia side:

> <https://askubuntu.com/questions/58814/how-do-i-add-environment-variables>

```julia
export VARNAME="my value" # shorter, less portable version

```

@fredrikekre I see export isn’t used by DotEnv.jl files, and I think needed in shells, for sub-shells (or some workaround to not need that?).
