Home > front end >  Python, VScode, Importing from sibling directory, without using os.paths.append - Python 3.8
Python, VScode, Importing from sibling directory, without using os.paths.append - Python 3.8

Time:10-11

python 3.8 with VScode. I have two sibling directories, and I want to import the first sibling (support_tools) to the second one.

this is my project hierarchy:

├── support_tools
│    ├── __init__.py
│    └── file_utils.py
└── optimizations
    ├──.vscode
    │    ├── launch.json
    │    └── settings.json
    ├── __init__.py
    └── test1.py

I added the parent path to the launch.json:

{
    // 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",
            "env": {
                "PYTHONPATH": "${workspaceFolder}${pathSeparator}..${pathSeparator}",
            },
        }
    ]
}

and to the settings.json:

{
    "python.analysis.extraPaths": [
        "${workspaceFolder}/../"
    ]
}

The pylance recognize the module support_tools.py, but I cannot import the support_tools module without appending the parent path to the os.paths: sys.path.append("../")

In this tutorial: enter image description here

A simple way is to use the optimizations parent folder as the workspace.

VsCode searches files using the workspace as the root directory. If the file you want to import is not in the workspace, it will not find the content.

Tips:

You can use the absolute path in "python. analytics. extraPaths".

Because in the workspace, the workspace is already the root directory. If you use the relative path, there does not exist the parent directory.

  • Related