linux VSCode:通过脚本设置环境变量

lzfw57am  于 2023-04-05  发布在  Linux
关注(0)|答案(3)|浏览(208)

我有一个shell脚本env.sh,其中包含像export ENV_VAR1 = 1这样的语句。在Linux终端上,我可以使用. env.shsource env.sh来设置环境变量。
如何通过env.sh在Visual Studio Code(VSCode)中设置环境变量?
我尝试了多种方法:
1)在获取脚本源代码后,我启动(VSCode -/usr/share/code/code),如下所示
. env.sh /usr/share/code/code
2)我使用扩展“AutoRunCommand”在工作区加载时调用. env.sh
例如,在使用Jupyter扩展时,我在import matplotlib中得到错误,即使当我在上面的Linux终端中使用Python解释器时导入工作。

toiithl6

toiithl61#

您可以尝试使用环境变量定义文件。

eyh26e7m

eyh26e7m2#

对于bash,您可以创建一个shell/source脚本,并将其作为工作区根目录中终端settings.json的参数。

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

参考文献:

qoefvg9y

qoefvg9y3#

您还可以为特定工作区的集成终端创建配置文件。例如zsh:
.vscode/设置.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.

这样,您就可以用这个配置文件启动一个新的终端,它将自动运行初始化脚本。

相关问题