[英]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:你有两种方法可以解决这个问题:
add the following code to your setup.py
:将以下代码添加到您的
setup.py
:
import sys
sys.path.append("./submodule1/submodule2/submodule3/submodule4")
import scripts
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.