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

Import can not be resolved error in pylance jupyter notebook for importing .py file in same directory as notebook #3017

Closed
jj-github-jj opened this issue Jul 8, 2022 · 34 comments
Assignees
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@jj-github-jj
Copy link

jj-github-jj commented Jul 8, 2022

Environment data

  • Language Server version: 2022.7.20
  • OS and version: win32 x64
  • Python version (and distribution if applicable, e.g. Anaconda): 3.9.5 but also many versions like 3.10 have the same issue
  • python.analysis.indexing: null
  • python.analysis.typeCheckingMode: off

Code Snippet


import myfunctions as mf
#myfunctions.py in same directory as notebook
  1. XXX

Expected behavior

hovering over mf.function() should show docstring etc

Actual behavior

no doc string since pylance reports import can not be resolved error.
Also noticed on a fresh dev machine install of vscode is actually showing the correct behavior for
one file i.e import myfunction as mf is working but in another ipynb file in the same directory opened in the same workspace at the same , import myfunction as mf does not work for pylance but

import myfunction

mf = myfunction

mf.function()
does work as expected in showing doc string for myfunction etc.

seems like workspace files are creating issues. If I open folder containing myfunction.py and the caller.ipynb after deleting any workspace files, things are working as expected. But after adding folders to workspace (a parent folder of the current folder) and saving as workspace file, pylance starts giving import can not be resolved errors.. My workaround is to delete all workspace files, open just the folder and pylance is happy

Logs

XXX
@github-actions github-actions bot added the triage label Jul 8, 2022
@vvvv0208
Copy link

Me too, the same code, after updating vscode, reported such an error “Import "model_2" could not be resolved Pylance(reportMissingImports)”

@debonte debonte self-assigned this Jul 11, 2022
@judej judej added the needs investigation Could be an issue - needs investigation label Jul 11, 2022
@github-actions github-actions bot removed the triage label Jul 11, 2022
@debonte
Copy link
Contributor

debonte commented Jul 11, 2022

@jj-github-jj, can you search for "LSP Notebooks experiment" in the Jupyter log and tell me what that line says. It's likely either:

LSP Notebooks experiment is disabled -- not in treatment group

or

LSP Notebooks experiment is enabled

If you see LSP Notebooks experiment is enabled, can you try setting "python.pylanceLspNotebooksEnabled": false in your VS Code settings, reload VS Code, and see if that fixes the behavior?

@laye0619
Copy link

laye0619 commented Jul 12, 2022

I have exact same issue... set the python.pylanceLspNotebooksEnabled but it didn't work....

@Nufflee
Copy link

Nufflee commented Jul 15, 2022

Setting python.pylanceLspNotebooksEnabled to false has fixed it for me, and I did have it enabled previously according to the log.

I have exact same issue... set the python.pylanceLspNotebooksEnabled but it didn't work....

@laye0619 Did you make sure to set it to false, not true?

@laye0619
Copy link

Setting python.pylanceLspNotebooksEnabled to false has fixed it for me, and I did have it enabled previously according to the log.

I have exact same issue... set the python.pylanceLspNotebooksEnabled but it didn't work....

@laye0619 Did you make sure to set it to false, not true?

I've already set the "python.pylanceLspNotebooksEnabled": false in my setting json file and reloaded... but seems that it was still not working.... searched the 'LSP Notebooks experiment' in Jupiter log and found that LSP Notebooks experiment was still enabled....

@jj-github-jj
Copy link
Author

Tried setting "python.pylanceLspNotebooksEnabled": false in user\settings.json, reloaded vs code and still have cant resolve import errors.

@heejaechang heejaechang self-assigned this Aug 4, 2022
@heejaechang heejaechang added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Aug 4, 2022
@heejaechang
Copy link
Contributor

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

@Yura52
Copy link

Yura52 commented Aug 15, 2022

@heejaechang the issue is still there, though in a different form. This is my project structure:

- lib/
    - __init__.py
    - whatever.py
- myfolder/
    - a.ipynb 
    - a.py
  • import lib in myfolder/a.ipynb causes Import "lib" could not be resolved
  • moving myfolder/a.ipynb to the project root fixes the issue
  • import lib in myfolder/a.py does not cause any issues

@judej judej reopened this Aug 16, 2022
@ccrvlh
Copy link

ccrvlh commented Aug 16, 2022

@debonte solution works for me, although I do have to correct the path when running from a sibling:
With the following folder structure

- app/
    - myfile.py
- notebooks/
    - mynotebook.ipynb

I have to put this on top of the first cell.

import sys, os
sys.path.insert(0, os.path.abspath('..'))

@MrYutz
Copy link

MrYutz commented Aug 18, 2022

The same overall issue for me. Code runs but Pylance isn't happy. It was working last week. Currently on Pylance 2022.8.31

- modules
    - module1.py
- notebooks
    - notebook.ipynb  

@Xabitsuki
Copy link

Same here with a local library installed in editable mode in conda environment: the package is correctly imported and documentations are displayed for all modules in the package but not whenever I import the package in a notebook.
Code runs but pylance complains.

I tried the

  1. "python.pylanceLspNotebooksEnabled": false which has not effect.
  2. adding "python.analysis.extraPaths": ["{workspaceFolder}/mypackage"] to workspace settings

--> did not solved it

About VS Code
Version: 1.70.2 (Universal)
Commit: e4503b30fc78200f846c62cf8091b76ff5547662
Date: 2022-08-16T05:36:37.829Z (1 wk ago)
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Darwin x64 21.6.0

@YDX-2147483647
Copy link

@Xabitsuki Is ["{workspaceFolder}/mypackage"] a typo? I mean there should be a dollar sign (${workspaceFolder}).

@bschnurr
Copy link
Member

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

@Michaelzhouisnotwhite
Copy link

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

Do I still need to diable pylanceLspNotebooksEnabled and change python.analysis.extraPaths in next version? My vscode version now is 1.71.0

@torext
Copy link

torext commented Sep 9, 2022

The issue still persists on my side with the latest versions of VSCode (1.71.0) and Pylance (2022.9.10):

- testworkspace/
    - modules/
        - class_defs.py
    - notebooks/
        - notebook.ipynb

with class_defs.py contents:

class MyClass:
    ...

Peek 2022-09-09 14-36

@antonioalegria
Copy link

antonioalegria commented Sep 13, 2022

With the latest version of VS Code (1.71.0), Pylance 2022.9.10, the problem is still there. Only works after disabling LSP Notebooks experiment and setting the analysis path, and then restarting all the kernels, etc...

@MrYutz
Copy link

MrYutz commented Sep 13, 2022

Same for me. The issue persists.

@debonte
Copy link
Contributor

debonte commented Sep 13, 2022

@MrYutz, @antonioalegria, @torext, it's likely that you are seeing #3208. We'll be shipping a fix for that in prerelease tomorrow.

@debonte
Copy link
Contributor

debonte commented Sep 15, 2022

@MrYutz, @antonioalegria, @torext, it's likely that you are seeing #3208. We'll be shipping a fix for that in prerelease tomorrow.

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

@elashrry
Copy link

elashrry commented Sep 15, 2022

VSCode (1.71.1) and Pylance (2022.9.21).

I tried to confirm the fix, and everything works fine when I create a new workspace. However, with the workspace I have been working on, it doesn't seems to work. I restarted VSCode, I deleted all workspace settings, but no fix. Why is this happening? The current workspace is a git repo, I don't know if this matters.

I also tried it with other workspaces that are git repos, and everything seems to work.

Update

I think I get the reason for the problem, but I don't know how to solve it.

The situation is like

root/
    work/
        test.py
        test.ipynb
        module.py
    tools/
        sub_folder/
            other_module.py
            test.py
            test.ipynb

I use conda environment and I add the tools/ foler to the python path by running command

conda develop ./tools

from the root folder.

Everything work fine in the work folder, but inside tools/sub_folder I get "Import can not be resolved" error inside the notebook and the python file when I try to import from other_module.

@MrYutz
Copy link

MrYutz commented Sep 15, 2022 via email

@torext
Copy link

torext commented Sep 15, 2022

This still isn't fixed for me when I create a new blank unsaved Jupyter notebook via Create: New Jupyter Notebook. In that case I still have the same exact behaviour as shown in the GIF in #3017 (comment)

Below shows the failure in a new notebook and how it's now working in a saved notebook. Also, I keep getting double entries in the autocomplete suggestions regardless of whether I'm in a saved notebook or not:

Peek 2022-09-15 15-21

If I set

"python.pylanceLspNotebooksEnabled": false,
"jupyter.pylanceHandlesNotebooks": false,

in the user settings, then both issues go away.

@MrYutz
Copy link

MrYutz commented Sep 15, 2022

@torext I am having the duplication issue again after the update. It had gone away, but now I am getting duplicate symbol suggestions, but only for native python libraries. See screenshot. dict and dict.update() show duplicates, but none of the other recommendations are duplicated.

Code_IQmYjEiH1h

@debonte
Copy link
Contributor

debonte commented Sep 19, 2022

@elashrry, if conda develop is doing an editable install, this may be related to #3262. Can you please file a new issue?

@torext, @MrYutz, please file new issues for your recent comments. It's extremely difficult to track and discuss what you are seeing when it's posted as comments on closed issues.

@torext
Copy link

torext commented Sep 20, 2022

@torext, @MrYutz, please file new issues for your recent comments. It's extremely difficult to track and discuss what you are seeing when it's posted as comments on closed issues.

The main message of my comment was that the issue under discussion is still not fixed when opening a new notebook. Should I file a new issue for this?

@elashrry
Copy link

@elashrry, if conda develop is doing an editable install, this may be related to #3262. Can you please file a new issue?

Thank you. I created this issue #3365

@debonte
Copy link
Contributor

debonte commented Sep 20, 2022

The main message of my comment was that the issue under discussion is still not fixed when opening a new notebook. Should I file a new issue for this?

@torext, yes please. Pylance handles unsaved notebooks differently than saved notebooks.

Also, I think the completion duplication issue is unrelated. It may be microsoft/vscode-jupyter#11038 as you suggested. I'm not sure.

@svenstehle
Copy link

@torext

I can confirm this. Setting "python.pylanceLspNotebooksEnabled": false, in user-settings solves the local import issue for me with the current release VSCode 1.71.2 and Pylance v2022.9.20

@debonte
Copy link
Contributor

debonte commented Sep 22, 2022

@svenstehle, FYI we will be removing the old notebooks implementation in the future, at which point the "python.pylanceLspNotebooksEnabled": true behavior will be the only option. So setting it to false is only a temporary workaround. If you're having problems in the latest version of Pylance that require you to disable LSP notebooks, please provide details (in a new issue).

@dlefcoe
Copy link

dlefcoe commented Nov 28, 2022

python

what do you mean "in the user settings". where are the user settings or how does one get to the user settings that you are referring to ?

@elashrry
Copy link

@dlefcoe You open the Command Palette by the keyboard shortcut: Command+Shift+P on mac and Ctrl+Shift+P on windows, then write "user settings", you should find user settings as GUI and user settings as JSON. Choose what you prefer.
JSON is easier so that you can copy the settings people are referring to as they wrote them.
In the GUI option, you can search for what you want.

@archongum
Copy link

Clean cache > Python: Clear Cache and Reload Window works for me.
Ref: https://stackoverflow.com/a/74177108/9756912

@thiswind
Copy link

thiswind commented Jul 1, 2023

Clean cache > Python: Clear Cache and Reload Window works for me.
Ref: https://stackoverflow.com/a/74177108/9756912

Great! It works for me!

@zhong-yy
Copy link

zhong-yy commented May 3, 2024

Weird behaviour!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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