简体   繁体   中英

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

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

If execute file run.py directly from PyCharm (using the run button), everything works. But I want to use VS Code. When I try the same with VS Code, I get the error: 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 .

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??

I know it should be a config in the settings.json file like this

{
    "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 :

    import sys

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

    import scripts

  2. This is shown in docs . Add the path to to PYTHONPATH by creating an file within your VS Code:

    PYTHONPATH=src

Then set in your file to point to the file you just created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM