简体   繁体   English

vscode 多根工作区:在 env 文件中指定 python 路径

[英]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.

相关问题 使用多根工作区时如何为Python语言服务器指定正确的python版本 - How to specify correct python version for Python Language Server when using multi-root workspace VSCode多项目工作区:如何在工作区根目录下添加.gitignore等单个文件? - VSCode multi-project workspace: how to add individual files such as the .gitignore at the root of the workspace? VS Code 多根工作区,带有一个顶级 git 存储库 - VS Code multi-root workspace with one top level git repo Python VS Code GoTo Definition 在多根工作区中不起作用 - Python VS Code GoTo Definition does not work in Multi-root workspaces 存储库与工作区根目录中的 VSCode Python Linting.pylintrc - VSCode Python Linting .pylintrc in repository vs workspace root 如何在VSCode中更改工作区(而非文件夹)的默认python路径 - How to change the default python path for a workspace (not a folder) in VSCode 如何自动将根目录添加到VSCode中的python路径? - How to automatically add the root directory to the python path in VSCode? 在python中指定文件的路径 - specifying path for a file in python 在vscode调试模式下,进入workspace根目录下的子文件夹? - In vscode debug mode, enter the subfolder of the root directory of the workspace? VSCode中的Python路径 - Python path in VSCode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM