Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pylance unable to resolve local import placed in the same directory #1167

Closed
jaepil-choi opened this issue Apr 17, 2021 · 11 comments
Closed
Labels
waiting for user response Requires more information from user

Comments

@jaepil-choi
Copy link

jaepil-choi commented Apr 17, 2021

Environment data

  • Language Server version: v2021.4.1
  • OS and version: Windows 10 Education 20H2 19042.928
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.5 Anaconda 4.10.1

Expected behaviour

Pylance imports .py files placed in the same directory and show code hints etc like Jedi.

Actual behaviour

Pylance fails to do so and gives:
Import "dataset" could not be resolved (Pylance report MissingImports)

The only way to resolve this (temporarily) is adding

    "python.analysis.extraPaths": [
        "./code", 
        "./code/**" <- doesn't work
        // "./code/baseline" <- works, but have to do it for each subfolder
    ],

to settings.json file but the problem is that I have to add every single nested directory like ./code/baseline. Glob pattern like ./code/** does not work at all.

This is particularly annoying when you have many subfolders in your code/ directory.

For example:

my_project/
    code/
        baseline/
        utils/
        foo/
        bar/
        ...

If my main source code is running in code/, it is impossible to import any of the local .py files from baseline/, utils/, foo/, ...
I have to manually add all of them one by one in settings.json.

Is this a bug?

Logs

N/A

Code Snippet / Additional information

N/A
@jakebailey
Copy link
Member

This is a dupe of #68 or #253, and is working as it's currently designed (but we're working on it). It's expected to that extraPaths needs to be set (and globs aren't supported, #973). See: https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#unresolved-import-warnings

But, if you could try out this WIP option, it may help your case:

"python.analysis.useImportHeuristic": true

I'm going to close this as a dupe of those other issues.

@jakebailey jakebailey added the duplicate This issue or pull request already exists label Apr 17, 2021
@github-actions github-actions bot removed the triage label Apr 17, 2021
@jaepil-choi
Copy link
Author

@jakebailey
Thanks for letting me know about the duplicate. Reading the previous issue seems like this requires a design decision from Pylance devs.

One thing I want to add though is that some of my friends using VSCode take this as Pylance's bug and stopped using it and reverted to Jedi. I think #68 (comment) made an excellent point because even though Pylance fails to import the script, it runs without any problem in the importing script.

Also, I tried "python.analysis.useImportHeuristic": true, option but to no avail. Pylance still fails to resolve the import. One peculiar thing is that "python.analysis.useImportHeuristic": true, is greyed out in settings.json, like when you have unused variable or imports. Does this indicate that "python.analysis.useImportHeuristic": true, option is actually not being used?

@jakebailey
Copy link
Member

Is your project available somewhere so we could test it? Or could you provide a tree listing of how the project is laid out so we can see how your case differs from others?

That option is hidden, it's expected for it to be grayed out.

@jakebailey jakebailey reopened this Apr 19, 2021
@jakebailey jakebailey removed the duplicate This issue or pull request already exists label Apr 19, 2021
@bschnurr bschnurr added triage waiting for user response Requires more information from user labels Apr 19, 2021
@github-actions github-actions bot removed the triage label Apr 19, 2021
@jaepil-choi
Copy link
Author

jaepil-choi commented Apr 19, 2021

@jakebailey
I can't give you a public link to the project but here's a screenshot of how directory tree looks like.

Oops, uploaded the wrong screenshot. Changed it.

image

@jakebailey
Copy link
Member

Hm, can you give an example of an import that doesn't work? Is it as simple as import dataset or similar?

@jaepil-choi
Copy link
Author

jaepil-choi commented Apr 19, 2021

@jakebailey

Sure, here an example of only adding one directory to the python.analysis.extraPaths and leaving the other commented out.
Even though I have python.analysis.useImportHeuristic": true, you can see that the script inside code/baseline/ is not recognized properly by Pylance.

image

Oops, there's a typo in the description.
Correction:

# code_sandbox.py is inside ./code/baseline --> # baseline_sandbox.py is inside ./code/baseline

@jakebailey
Copy link
Member

Ah, you have extraPaths set with the heuristic, can you comment that out and just use the heuristic? We don't perform the heuristic in paths that are in extraPaths (for now).

@jaepil-choi
Copy link
Author

@jakebailey

Oh, I tried removing extraPaths and only leave heuristics and it is now working fine as expected.
Thank you so much!

We don't perform the heuristic in paths that are in extraPaths (for now).

To point out though, I only had ./code/ inside extraPaths and commented out ./code/baseline/ as you can see in the screenshot. Pylance was unable to recognize imported script in ./code/baseline/.

Removing the whole extraPaths and only leaving heuristics solved the problem.
Maybe using two options at the same time is causing the problem?

@jakebailey
Copy link
Member

Yes, you can't use both at the moment; we need to ensure that we don't apply this heuristic for non-user code, and extraPaths interferes with those checks (as they can reasonably point anywhere).

@jakebailey
Copy link
Member

I'm going to close this issue (as before) as things are working as intended, and other work will be in the scope of the other issues.

@I2BKeit
Copy link

I2BKeit commented Jan 13, 2023

I was getting similar issue when I imported bot3 it threw an error <Import "boto3" could not be resolvedPylance> I did try Ctrl+Shitf+` To reset my path directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests

4 participants