简体   繁体   English

Visual Studio Code 正确配置 settings.json

[英]Visual Studio Code configure settings.json correctly

I have a program with the following structure我有一个具有以下结构的程序

.
└── module/
    ├── submodule1/
    │   ├── __init__.py
    │   └── submodule2/
    │       ├── __init__.py
    │       └── submodule3/
    │           ├── __init__.py
    │           └── submodule4/
    │               ├── __init__.py
    │               ├── run.py
    │               └── scripts.py
    ├── setup.py
    ├── dist
    └── build

File run.py文件run.py

from submodule1.submodule2.submodule3.submodule4 import scripts
.
.

If execute file run.py directly from PyCharm (using the run button), everything works.如果直接从 PyCharm 执行文件run.py (使用运行按钮),一切正常。 But I want to use VS Code.但我想使用 VS Code。 When I try the same with VS Code, I get the error: ModuleNotFoundError: No module named 'submodule1' .当我对 VS Code 进行相同尝试时,我收到错误: ModuleNotFoundError: No module named 'submodule1' I have already configured the correct interpreter.我已经配置了正确的解释器。 In PyCharm under Run/Debug Configurations there are the options Add content roots to PYTHONPATH and Add source roots to PYTHONPATH .在运行/调试配置下的 PyCharm 中有选项将内容根目录添加到 PYTHONPATH将源根目录添加到 PYTHONPATH

My Question:我的问题:

How can I set these variables in VS Code so that it works the same way as in PyCharm using the run button??如何在 VS Code 中设置这些变量,使其工作方式与使用运行按钮在 PyCharm 中的方式相同?

I know it should be a config in the settings.json file like this我知道它应该是settings.json文件中的配置,如下所示

{
    "python.experiments.optInto": [
    
    ],
    "python.envFile": "${workspaceFolder}/.venv",
    "python.autoComplete.extraPaths": [
        // maybe this variable?
    ],
}

Every process has a current directory.每个进程都有一个当前目录。 When a process starts, it simply inherits the current directory from its parent process;当一个进程启动时,它只是从其父进程继承当前目录; and it's not, for example, set to the directory which contains the program you are running.例如,它没有设置为包含您正在运行的程序的目录。

You can read this passage for details about current working directory .您可以阅读这篇文章以了解有关当前工作目录的详细信息。

You have two ways to solve this problem:你有两种方法可以解决这个问题:

  1. add the following code to your setup.py :将以下代码添加到您的setup.py

    import sys

    sys.path.append("./submodule1/submodule2/submodule3/submodule4")

    import scripts

  2. This is shown in docs .这显示在docs中。 Add the path to to PYTHONPATH by creating an file within your VS Code:通过在 VS 代码中创建一个文件来添加到 PYTHONPATH 的路径:

    PYTHONPATH=src PYTHONPATH=src

Then set in your file to point to the file you just created.然后在你的文件中设置指向你刚刚创建的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio Code 中获取 settings.json 的 pylint 部分? - How to get to pylint part of settings.json in Visual Studio Code? Visual Studio Code 忽略 settings.json 字段:python.pythonPath - Visual Studio Code ignores settings.json field: python.pythonPath VSCode Code Runner 不遵循设置。json - VSCode Code Runner not following settings.json 缺少 VSCode Settings.json - VSCode Settings.json is missing Visual Studio Code launch.json 设置用于调试 Python 模块 - Visual Studio Code launch.json settings for debugging Python modules VS Code 代码运行器扩展:由于空格,努力将 Python PATH 添加到 settings.json - VS Code Code runner extension: Struggling to add Python PATH to settings.json due to whitespace 在不使用 settings.json 文件的情况下在 VS Code 中设置本地项目的 Python 路径 - Setting the Python path for local project in VS Code without using the settings.json file 在VSCode中,pygame没有'init'成员,尝试在设置中添加代码。json,仍然有困难 - In VSCode, pygame has no 'init' member, tried adding code to settings.json, still having difficulties 在 VS Code 中,我不想让 Black 格式化我的设置。json - In VS Code I don't want Black to format my settings.json 使用Visual Studio Code在Python中设置虚拟环境 - Settings of Virtual Environment in Python with Visual Studio Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM