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

Unexpected pylance "reportMissingImports" in notebook for local package #3208

Closed
AwePhD opened this issue Aug 17, 2022 · 14 comments
Closed

Unexpected pylance "reportMissingImports" in notebook for local package #3208

AwePhD opened this issue Aug 17, 2022 · 14 comments
Assignees
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@AwePhD
Copy link

AwePhD commented Aug 17, 2022

Hello,

I think there is a problem with the Python Analysis feature and Notebooks. I searched on SO and in the issues and I did not found anything similar.

Description of the problem

I have the following minimal set-up :

├── lib
│   ├── data.py
│   ├── __init__.py
├── notebook_root.ipynb
└── notebooks
    └── notebook.ipynb

data.py contains class A: pass

In notebook.ipynb and notebook_root.ipynb have a single cell containing: from lib.data import A

Then I have the following :

  • I can successfully execute BOTH cells.
  • In notebook_root.ipynb, there is no yellow warning.
  • In notebook.ipynb, there is a pylance warning "reportMissingImports"

Additional information

Intellicode properly detects the A class while I am typing the import line

I add the following extra settings :

  {
    "jupyter.notebookFileRoot": "${workspaceFolder}",
    "python.languageServer": "Default",
    "python.analysis.extraPaths": [
        "${workspaceFolder}"
    ],
    "python.autoComplete.extraPaths": [
        "${workspaceFolder}",
    ],
}

What did I try

I tried to change my option to see if the ${workspaceFolder} is set. If I change the jupyter.notebookFileRoot to ${foo}, then the notebook.ipynb's cell cannot be run anymore because it cannot find the local package.
Although, the other settings do not seem to have any effect. If I change ${workspaceFolder} to ${foo} then there is no difference for both notebooks.

Ask me if you need any additional information.

Have a nice day,
Mathias.

@anthonykim1 anthonykim1 transferred this issue from microsoft/vscode-python Aug 17, 2022
@judej judej added the needs investigation Could be an issue - needs investigation label Aug 17, 2022
@bschnurr
Copy link
Member

does this happen on the pre-release pylance build?

@AwePhD
Copy link
Author

AwePhD commented Aug 18, 2022

No, I do not use the Pre-Release version of the plugin. The version that I am using is v2022.8.30

@OllieKampo
Copy link

OllieKampo commented Aug 18, 2022

I am having the same issue, I wonder if it is related to this issue #3017. This comment seems to be something similar. I wonder if that fix has broken absolute imports with "jupyter.notebookFileRoot": "${workspaceFolder}" in settings.json.

In my case, I have a folder structure like this:

  • a/
    • file_in_different_dir.py
  • b/
    • file_in_same_dir.py
    • notebook.ipynb

If I do this in notebook.ipynb (in a cell):

import a.file_in_different_dir
import b.file_in_same_dir

The jupyter notebook file runs correctly, but pylance reports "reportMissingImports" for both the imports.

In the past (about a week ago) this worked just fine, no errors being reported. I don't think I have changed anything in my settings.

If I do:

import file_in_same_dir

Pylance does not report the error and the file runs.

@YDX-2147483647
Copy link

I met this issue on August 7. (It was the last version of Pylance)

I followed the trouble-shooting guide, and found a direct evidence of the working space (aka. the root path, cwd, or whatever.).

IMG_1085

  • The sidebar on the left shows the file structure.

    To clarify: outer.py's absolute path: D:/DevelopProjects/temp/temp/outer.py.

  • Content of that.py: print('This is that.py.').

    Remarks: I import this (which is beyond the screenshot) to make sure that standard libraries work fine. Therefore I call it that.py.

The selected row: the root path is VS Code's workspace (…/temp/) not notebook's (…/temp/inner/).

@AwePhD
Copy link
Author

AwePhD commented Aug 22, 2022

For additional information, I did the same procedure as @YDX-2147483647. I put the log file on a pastebin.

At the beginning of the log file there are these lines:

[Info  - 8:43:44 AM] (12624) Search paths for /home/reusm-admin/code/issues_min/notebook_import
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/.vscode/extensions/ms-python.vscode-pylance-2022.8.30/dist/typeshed-fallback/stdlib
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/code/issues_min/notebook_import
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/code/issues_min/notebook_import/${toto}
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/code/issues_min/notebook_import/typings
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/.vscode/extensions/ms-python.vscode-pylance-2022.8.30/dist/typeshed-fallback/stubs/...
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/.vscode/extensions/ms-python.vscode-pylance-2022.8.30/dist/bundled/stubs
[Info  - 8:43:44 AM] (12624)   /usr/lib/python3.8
[Info  - 8:43:44 AM] (12624)   /usr/lib/python3.8/lib-dynload
[Info  - 8:43:44 AM] (12624)   /home/reusm-admin/.local/lib/python3.8/site-packages
[Info  - 8:43:44 AM] (12624)   /usr/local/lib/python3.8/dist-packages
[Info  - 8:43:44 AM] (12624)   /usr/lib/python3/dist-packages

It seems that the workspace folder, /home/reusm-admin/code/issues_min/ here, is not included in the patch search.

@AwePhD
Copy link
Author

AwePhD commented Aug 29, 2022

The pre-release v2022.8.41 did not fixed the bug. Which makes this issue different from #3017.

@OllieKampo
Copy link

OllieKampo commented Aug 29, 2022

@AwePhD I was asking whether it is possible that the fix, or attempted fix, to #3017 may have somehow caused this bug. It seemed more than a coincidence that this bug was not present prior to v2022.8.12, and then appeared suddenly after the release. It is the only change I can see related to imports around the time the bug appeared, seemed like something worth pointing out.

@debonte debonte self-assigned this Aug 29, 2022
@marcinwrochna
Copy link

marcinwrochna commented Sep 6, 2022

Note these imports get resolved correctly in normal python (non-notebook) files (at least for me), i.e. for a file "notebooks/example.py" with "import lib.data".

I noticed I get reportMissingImports when "python.analysis.extraPaths" is not set or contains any of "${workspaceFolder}", "", ".",
but it finds the import when it contains either of "${workspaceFolder}/..", ".." (regardless of whatever "jupyter.notebookFileRoot" is set). So it seems the workspaceFolder is somehow resolved to the notebook's folder rather than the workspace folder (or notebookFileRoot) in the context of notebooks.

I still have this bug in pre-release v2022.8.51

@torext
Copy link

torext commented Sep 9, 2022

I confirm I also still see the same issue with Pylance 2022.9.10, see #3017 (comment)

@debonte
Copy link
Contributor

debonte commented Sep 15, 2022

This issue has been fixed in prerelease version 2022.9.21, which we've just released. You can find the changelog here: CHANGELOG.md

@debonte debonte closed this as completed Sep 15, 2022
@torext
Copy link

torext commented Sep 15, 2022

FYI this doesn't seem fixed when creating and working in a new Jupyter notebook via Create: New Jupyter Notebook, see #3017 (comment).

@memeplex
Copy link

I'm experiencing this in a project. pylance keeps saying that a python module in the same folder than my notebook could not be resolved. I tried to reproduce it in a minimal project of similar structure, to no avail. Then I duplicated the entire project into a new folder, opened it and suddenly it works, but if I now open the original project again it keeps failing to resolve the import as before. It seems like there is some state persisting for the workspace, but I couldn't find any way of cleaning it up. Clear Cache and Reload Window improves nothing.

@debonte
Copy link
Contributor

debonte commented Jul 18, 2024

@memeplex this issue has been closed for almost two years. It's likely you're seeing something unrelated. Could you please open a new issue?

@memeplex
Copy link

Not so sure it's unrelated since I see the issue popping up from time to time and it's always difficult to reproduce, as in my case. FWIW I went for the nuclear option and removed the entire workspaceStorage (I wish there was a more reasonable way but I'm afraid there isn't), opened my workspace again and pylance started behaving ok. So I don't have any hope to reproduce the issue in a way that's worth reporting other than a vague heads up about inconsistent state that is not purged without nuking the workspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

10 participants