3

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: https://k0nze.dev/posts/python-relative-imports-vscode/ they clearly mention that after adding the paths to both file, I should be able to remove the os.path.append line

In addition I tried to find an answer in the following pages:

VSCode settings for Pylance

Import Sibling Packages with __init__.py doesn't work

Importing modules from parent folder

Thanks for the helpers

Ilan Geffen
  • 179
  • 8

1 Answers1

1

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.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • Thanks for your response! I am familiar with that, but I want to know if it possible to use the optimization folder as as the workspace, since its more convenient for me, without using sys.path.append. – Ilan Geffen Oct 10 '22 at 09:19
  • @Ilan Geffen, I added my answer, you can use the absolute path. – MingJie-MSFT Oct 10 '22 at 09:40
  • @Mingji-MSFT, thanks for trying to help! I tried and it didn't work: "ModuleNotFoundError: No module named 'support_tools'" I added both absolute and relative path to the parent directory. – Ilan Geffen Oct 10 '22 at 13:25
  • This is caused by python's search for the path. You add in settings only to provide the package's identification to obtain intelligent prompts. When you really want to run the code, python will still not find the path of the package, so if you want to run it, you have to put it in the same workspace or use os.paths.append – MingJie-MSFT Oct 11 '22 at 01:12