Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables for integrated terminal

I am seeking help figuring out how to setup environment variables for integrated terminal within Visual Studio Code. Currently I am able to do so with .env file inside my workspace folder, but I'd like to change the filename for this file and create another one or two, let's say dev.env and prod.env. Setting "python.envFile" for my workspace doesn't do the trick and from what I understand changing things in launch.json is for debugging.

The overall goal of all this (and that is important) is to run flask shell (integrated shell for flask web framework, python) with a certain set of env variables and be able to change them by swapping files. I know I could set these by introducing "terminal.integrated.env.osx" to my workspace settings, but I'd rather get these variables from a file.

Thanks a lot for your time and help.

UPD >>

I guess one way to go about it would be to create two tasks as such:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "FLASK SHELL DEV",
            "type": "shell",
            "command": "source ${workspaceFolder}/dev.env && ${config:python.pythonPath} -m flask shell",
            "problemMatcher": []
        },
        {
            "label": "FLASK SHELL PROD",
            "type": "shell",
            "command": "source ${workspaceFolder}/prod.env && ${config:python.pythonPath} -m flask shell",
            "problemMatcher": []
        }
    ]
}

but the question stands. Is there a way to do the same with integrated terminal?

like image 571
jabbson Avatar asked Oct 28 '22 05:10

jabbson


1 Answers

Yes, you can use one of these settings (depending in your platform):

"terminal.integrated.env.linux": {},
"terminal.integrated.env.windows": {},
"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/src:${env:PYTHONPATH}"
},

However, they will only work for the integrated terminal, not for any other processes the vs code python extension might spawn (tests, linting, debugger). For those you need to use the .env file.

Edit:

It is worth noting that if you start VS Code from the terminal it should inherit any env variables from terminal environment (at least for all the spawned subprocesses, probably not for the integrated terminal).

like image 75
niid Avatar answered Nov 15 '22 05:11

niid