VS Code PYTHONPATH for Windows and Linux

Question:

I need to set the PYTHONPATH for a project in Visual Studio Code. I have an .env file specifying the PYTHONPATH. However, since my path consists of a number of directories, I need to use the following on Windows:

PYTHONPATH=./dirA;./dirB;${PYTHONPATH}

But use colon as a separator on Linux

PYTHONPATH=./dirA:./dirB:${PYTHONPATH}

My .env file is stored in the source repository, as I don’t want every person working on the project to figure it out by themselves. I tried setting different env files for Linux and Windows, but setting python.envFile.windows caused the Python extension to fail entirely.

How can I set the Visual Studio Code PYTHONPATH once, in a way that works for developers in both Linux and Windows?

Asked By: zmbq

||

Answers:

Since you are using vscode, you might need to setup a launch.json file for your project, with at least 2 configurations – one for windows and another for Linux (see the documentation here https://code.visualstudio.com/docs/editor/debugging#_launch-configurations)

You will need to set the environment field in each configuration with the right value for PYTHONPATH.

Answered By: P.S.K

There is no OS-specific support for specifying paths to different .env files to specify unique PYTHONPATH values. Please file a feature request at https://github.com/microsoft/vscode-python if you would like to see that implemented.

Answered By: Brett Cannon

Create two .env files. one for linux and one for windows.

In launch.json, add the following:

    "windows": {
        "envFile": "${workspaceFolder}/.env"
    },
    "linux": {
        "envFile": "${workspaceFolder}/.env_linux"
    }
Answered By: mike01010