Let's suppose that I have a project with this file structure:
project_dir
└── src
├── package1
│ └── module1.py
└── package2
└── module2.py
When I want to use some class from module1 in some other module I type something like
class SomeNewClass(ClassFromModule1): # here I press ctrl-space to make auto-import
...
When Pylance makes auto-import it always takes it starting from the root directory of the project, so I got this auto-import:
from src.package1.module1 import ClassFromModule1
The problem is that my working directory is src and I want to get
from package1.module1 import ClassFromModule1
Is there any way to fix root path of the auto-import Pylance feature?
CodePudding user response:
By default, if you import a module in src
, it should look like the following (same effect as you want).
So please check if the following settings in vscode are modified: Open Settings and search for Analysis: Auto Search Paths.
This item is enabled (checked) by default. If it was modified to be off, turn it back on.
Here are some examples:
Folder structure:
Project_dir
├─ folderone
│ └─ package3
│ └─ moudle3.py # --class C
├─ src
│ ├─ package1
│ │ └─ moudle1.py # --class A
│ └─ package2
│ └─ moudle2.py # --class B
└─ Test.py
If you import the modules in the
src
folder (the above settings are enabled by default):# import code will not include `src` (like the picture shown above) from package1.moudle1 import A
If the above features are turned off, the import code is as follows:
If importing modules under other folders: The import code will start from the first level folder under the current workspace.
Note: vscode uses the currently open folder as the workspace. So which level of folder you open will also affect the code in the import part.
The above example is to open the Project_dir
folder as a workspace in vscode.
If I put Project_dir
in a new folder ImpDemo
and open the ImpDemo
folder in vscode. Then the code of the import part is like this: