7

I have a shell script env.sh containing statements like export ENV_VAR1 = 1. On Linux terminal, I can use . env.sh or source env.sh to set the environment variables.

How to set the environment variables in Visual Studio Code (VSCode) via env.sh?

I have tried multiple approaches:

1) I start (VSCode - /usr/share/code/code) after sourcing the script as follows

. env.sh /usr/share/code/code

2) I used the extension "Auto Run Command" to call . env.sh at workspace loading time.

But these do not work. For example, while using the Jupyter extension, I get errors in import matplotlib even though the import works when I use the Python interpreter inside the Linux terminal above.

Gama11
  • 31,714
  • 9
  • 78
  • 100
amit kumar
  • 20,438
  • 23
  • 90
  • 126

3 Answers3

2

You can try using an environment variable definition file.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
1

For bash, you can can create a shell/source script and source it as an argument to the terminal the settings.json in the workspace root.

{
    "terminal.integrated.copyOnSelection": true,
    "terminal.integrated.shellArgs.linux": [
        "--rcfile",
        "bin/init_dev_env.sh"
    ]
}

Refs:

aaronsteers
  • 2,277
  • 2
  • 21
  • 38
  • I have the same issue and I'm trying to use your suggestion but it's not working, I've tried to edit both setting files: `root/.vscode/settings.json` and the main `settings.json` via user setting, What am I missing here? – Elik Oct 19 '20 at 08:44
0

You can also create a profile for the integrated terminal for a specific workspace. For example for zsh:

.vscode/settings.json:

{
  "terminal.integrated.profiles.osx": {
    "Init dev env": {
      "path": "zsh",
      "args": ["-c", "source ${workspaceFolder}/.vscode/init.sh"],
    },
  },
  "terminal.integrated.defaultProfile.osx": "Init dev env"
}

.vscode/init.sh:

echo "Setup dev env"
export TEST=1
exec zsh -i # This makes sure the terminal is interactive and won't be closed.

This way you can start a new terminal with this profile that will automatically run the initialization script.

Jeruntu
  • 178
  • 1
  • 9