如何在Visual Studio Code中扩展Python搜索路径

huangapple go评论54阅读模式
英文:

How to extend Python search path in Visual Studio Code

问题

通过我在社区找到的信息,有两种方法可以扩展Python搜索路径以加载更多库。

  • .env
    例如 PYTHONPATH="C:\path\to\a;C:\path\to\b"
  • launch.json
    例如
{
    // 使用 IntelliSense 了解可能的属性。
    // 悬停以查看现有属性的描述。
    // 有关更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "PYTHONPATH": "${workspaceFolder}/lib"
            }
        }
    ]
}

在多天的搜索后,我仍然没有找到明确的解决方案。有些事情按预期工作,而有些则不是。明确一下,这可能只是对概念的错误理解。因为我在主题上找到了多个线程,但几乎都是相同的答案,对我来说还没有奏效。有关此主题的多个线程,所以我认为有一些一般的兴趣来澄清这个问题。

目前我专门测试了每个选项。并行使用这两个选项在某种程度上并不方便,而且功能也不同。.env 不支持预定义变量,而 launch.json 支持。因此,我在没有在 launch.json 中设置的情况下测试了 .env,以及没有 .env 文件 + 在 launch.json 中设置。

我使用的是VS Code 1.75.1,但在之前的多个版本中也是这样。Anaconda Python3 与不同的虚拟环境。

在使用VS Code中的选项时,我看到以下情况:

是 --> 扩展PYHTONPATH按预期工作
否 --> 扩展PYHTONPATH目录未按预期工作

运行选项 .env 设置 launch.json 设置
在终端中运行Python文件
在交互式窗口中运行
通过F5调试
"调试Python文件" (*)

(*) 通过在编辑器窗口右上角的运行符号的下拉列表中选择“调试Python文件”调试

我假设两个调试选项应该是相同的,但有时候通过F5启动调试可以按预期工作,但使用调试符号报告错误。

不清楚为什么两个调试选项的行为不同。
不知道为什么运行Python终端会完全忽略路径设置。
总的来说,关于这两个选项背后的思想的更详细的信息将非常有帮助。为什么在终端中运行代码和调试之间会有差异。我从其他IDE中了解到启动配置的概念对我来说很有意义。是否有一种方式可以按层次结构定义这些配置,因此只需要为较低级别添加更改,而其余部分可以保持通用。

在OS /命令shell级别上设置所有环境内容是可能的。但我认为,VS Code专门为每个工作区/项目执行此操作。

我做错了什么?在设置VS Code时我错过了什么?或者我对概念/工作流程的理解还不够正确。

我期望能够单独控制每个工作区的库路径设置。可能在工作区区域(.env或launch.jason,或其他任何方式)中的一个地方进行一次配置设置。

任何帮助/评论都感谢,非常感谢,

Mike

英文:

From what I found in the community, there are two options to extend the Python search path with further libraries.

  • .env
    e.g. PYTHONPATH="C:\path\to\a;C:\path\to\b"
  • launch.json
    e.g.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "PYTHONPATH": "${workspaceFolder}/lib"
            }
        }
    ]
  }

After searching again for this topic on multiple days, I did not yet find any clear solution for that.
Some things work as expected, others don't. To be clear, it might be just a wrong understanding of the
concepts. Since I found multipe threads on the topic, but almost just the same answers, which did not
yet work out for me. There are multiple threads aroung that stuff, so I assume some general interest for a clarification.

Currently I tested exlusively each option. Using both things in parallel is somehow not comfortable,
also since the capabilities are different. .env does not support pre-defined variables,
lauch.json, does. So I tested .env without settings in launch.json and no .env file + settings in launch.json.

I use VS Code 1.75.1, but it was the case also for multiple previous version.
Anaconda Python3 with different virtual environments.

What I see is the following, when using the options in VS Code:

yes --> extending the PYHTONPATH worked as expected
no --> extending the PYHTONPATH dir NOT worked as expected

Run option .env setting launch.json setting
Run Python file in terminal no no
Run in interactive Window yes no
Debug via F5 yes yes
"Debug Python File" (*) yes no

(*) Debug via "Debug Python File" in Drop-Down of Run Symbol above right of the editor window

I assumed both debug options should be the same, but I have sometimes situations, where starting Debug via F5 works as expected, but using the Debug Symbol reports an error.

Not clear, why both debug options behave differently.
No idea, why running Python terminal, does ignore path settings completely.
In general more detailed information about the ideas behind both optios, would be very helpful. Why there is a difference for just running code in the terminal and debugging. The concept of launch configurations I know from other IDEs and it makes a lot of sense to me. Is there a way define such conigurations hiarchically, so only changes have to be added for lower levels and the rest can be kept common.

Setting all environment stuff on OS/command shell level, is possible via system/user variables. But I thought, that VS Code is doing that specifically for each workspace/project.

What am I doing wrong? What do have I missed during setup of VS Code? Or where do I not yet understand the concecept/workflow correctly.

I expected to control the library path settings for each workspace seperately. Potenitally in just one place (one configuration setting) in the workspace area (.env or launch.jason, or what ever other way)

Any help/comment is appreciated, thanks a lot,

Mike

答案1

得分: 0

以下是翻译好的部分:

  1. 如果您只对调试Python脚本感兴趣,最简单的方法是在编辑器上运行按钮旁边选择下箭头,然后选择 Debug Python File

  2. 如果您想要调试使用Flask、Django或FastAPI的Web应用程序,Python扩展提供了基于项目结构动态创建的调试配置,可以在 运行和调试视图(f5) 下的“显示所有自动调试配置”选项中找到。

  3. 您可以阅读 文档中的 basic-debugging 获取更多详细信息。

  4. 我建议您使用 虚拟环境。这样可以更容易地管理您的库。

  5. 此外,如果您希望在指定位置导入包,可以在 launch.json 文件中使用 env

    "env": {
      "PYTHONPATH": "path/to/your/package"
    }
    
  6. 另一种方法是使用 .env 文件。将以下代码添加到您的 settings.json,然后在项目文件夹下创建 .env 文件:

    "python.envFile": "${workspaceFolder}/.env",
    

    .env 文件内容如下:

    PYTHONPATH=path/to/your/package
    
  7. 然后您可以使用 Debug Python File 按钮。

请注意,上述内容是按照您的要求提供的翻译,没有包括代码部分。

英文:

For the questions raised in your article, if you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File.

如何在Visual Studio Code中扩展Python搜索路径

If you're looking to debug a web application using Flask, Django or FastAPI, the Python extension provides dynamically created debug configurations based on your project structure under the Show all automatic debug configurations option, through the Run and Debug view(f5).

如何在Visual Studio Code中扩展Python搜索路径

You can read document basic-debugging for more detials.

> I expected to control the library path settings for each workspace
> seperately.

I suggest you using virtual environment. This makes it easier for you to manage your library.

In addition, if you want to import the package in the specified location, you can use env in the launch.json file:

  "env": {
    "PYTHONPATH": "path/to/your/package"
  }

Another way is using .env file. Add the following codes to your settings.json then create .env file under project folder:

"python.envFile": "${workspaceFolder}/.env",

.env:

PYTHONPATH=path/to/your/package

Then you can use Debug Python File button.

huangapple
  • 本文由 发表于 2023年2月16日 17:49:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定