[英]vscode multi-root workspace: specifying python path in env files
I have the following folder structure.我有以下文件夹结构。 Project1 and Project2 are part of a multi-root workspace.
Project1 和 Project2 是多根工作区的一部分。
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.我将在 windows 上开发,但在 linux 上运行。所以我想保留不同的环境文件(.env 和 .env_linux),并根据运行的操作系统加载它们。 The.env file looks like this:
.env 文件如下所示:
PYTHONPATH=./src:./src/utils:./src/app:./src/services
My launch.json file looks like this:我的 launch.json 文件如下所示:
{
"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: app.py 中的代码如下所示 - 只有一行试图导入 utils 模块:
from utils import utils
When the code runs, at the above line I get the error "No module named 'utils'"当代码运行时,在上面一行我得到错误“No module named 'utils'”
So i next added the following to my settings.json:所以我接下来将以下内容添加到我的设置中。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:
我认为这种 env 文件方法行不通,然后将 PYTHONPATH 添加到 settings.json 中,如下所示,但我仍然遇到相同的错误:
"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.
我也试过更改.env 文件以引用绝对路径无济于事。 What am i doing wrong???
我究竟做错了什么???
It's interesting to note that pylance is able to find the packages/modules when editing.有趣的是,pylance 能够在编辑时找到包/模块。 just at run time i get that error.
就在运行时,我得到了那个错误。
For the initial problem,对于最初的问题,
We use ;
我们使用
;
to split environment variables instead of :
.拆分环境变量而不是
:
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.