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

Time:12-13

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.

CodePudding user response:

For the initial problem,

We use ; to split environment variables instead of :.

  • Related