vscode multi-root workspace: specifying python path in env files

Question:

I have the following folder structure. Project1 and Project2 are part of a multi-root workspace.
enter image description here

I will be developing on windows, but running on linux. so i would like like to keep different environment files (.env and .env_linux), and load them based on the OS running under. The .env file looks like this:

PYTHONPATH=./src:./src/utils:./src/app:./src/services

My launch.json file looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Run App",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder:project1}/src/app/app.py",
            "console": "integratedTerminal",
            "justMyCode": true,
            "windows": {
                "envFile": "${workspaceFolder:project1}/.env"
            },
            "linux": {
                "envFile": "${workspaceFolder:project1}/.env_linux"
            }               
        }
    ]
}

The code in app.py looks like below – just one line trying to import the utils module:

from utils import utils

When the code runs, at the above line I get the error "No module named ‘utils’"

So i next added the following to my settings.json:

"terminal.integrated.env.windows": {
    "python.envFile": "${workspaceFolder:project1}/.env",
},
"terminal.integrated.env.linux": {
    "python.envFile": "${workspaceFolder:project1}/.env_linux",
},

This did not solve the problem. I figured that this env file approach just isnt going to work and then added the PYTHONPATH to the settings.json as seen below, but i still get the same error:

"terminal.integrated.env.windows": {
    "python.envFile": "${workspaceFolder:project1}/.env",
    "python.pythonPath":"${workspaceFolder:project1}/src:${workspaceFolder:project1}/src/app:${workspaceFolder:project1}/utils",
    "PYTHONPATH":"${workspaceFolder:project1}/src:${workspaceFolder:project1}/src/app:${workspaceFolder:project1}/utils",
},

Still the same error. I also tried changing the .env file to reference the absolute path to no avail. What am i doing wrong???

It’s interesting to note that pylance is able to find the packages/modules when editing. just at run time i get that error.

Asked By: mike01010

||

Answers:

For the initial problem,

We use ; to split environment variables instead of :.

Answered By: MingJie-MSFT
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.