15

I am trying to import function query_creative inside of file query_creative.py into the notebook Notebook.ipynb. The file hierarchy is like this:

    ├───playground
    │    Notebook.ipynb
    ├───src
    ├───queries
         query_creative.py

The code I use in my notebook goes like this:

import sys
sys.path.append('..')
from src.query.query_creative import query_creative

and the thing works. However, the line with the function import is underlined by Pylance with the message: "Import could not be resolved" and when I use this function later on, it works but Pylance underlines it again. I've tried modifying the setting.json, but nothing seems to work. I thank in advance for any help!

6 Answers 6

35

For me the following fixes this: make sure you have correct python environment selected as the kernel. Then press F1, type "reload" and select "Developer: Reload Window". It will restart VSCode, reopen all your files and all imports are now detected.

Sometimes, you can select the correct kernel, close your current vscode tab tab and press Ctrl+Shift+T to reopen it, which is a little bit faster. This seems to only work if you have a workspace opened.

Sign up to request clarification or add additional context in comments.

Comments

5

I managed to solve it eventually by downgrading Jupyter extension to VS Code to v2022.4.1001071005. Thanks everyone for help!

2 Comments

Hmm, downgrading my Jupyter extension to the most recent v2022.4 version (v2022.4.1021342353) also fixed my import issue, where Pylance was not detecting a script import in the same folder when using Jupyter for VS Code. Newer versions (v2022.5 and up, until v2022.7.1002181843, the most recent release to date) all failed to work.
I downgraded to 2022.4.1001252103 and it worked fine
2

Which version of vs code you have? I faced with same issue after updating to 1.70.

UPD.

Downgrading to 1.69.2 fixed that issue. However, I have another laptop with 1.70 with exact same notebook there is no such issue, so if you want to stick around 1.70 it is worth to try to completely remove vscode and install it again.

UPD2.

After a while my downgrade didn't help on another machine, so I compared Pylance output difference between them and find out, that "working" instance used legacy jupyter notebook support, while "buggy" with "LSP". After disabling option Pylance Lsp Notebooks Enabled enter image description here everything worked as usual and fixed that problem

2 Comments

Hi, thanks! I downgraded it but it still doesn't work :(
yeah, I have got the same after enabling pylance, so little investigation showed this LSP feature, disabling which helped (see Upd2.) will appreciate your attempt to test it.
2

For anyone still interested: This was a known issue and has been fixed for VS-Code 1.74 and later with the latest Jupyter, PyLance and Python extensions. So update, reload all extensions and restart Jupyter kernel should fix it.

See these two issues for reference:
https://github.com/microsoft/pylance-release/issues/3035
https://github.com/microsoft/vscode-jupyter/issues/11038

Comments

0

It seems that pylance is unable to determine that the code is importable. Instead of modifying your path at the beginning of the script, you could create a Python package with your code, then do:

cd your-project
pip install -e .

You can see a guide to create packages here.

With these settings, pylance and any other editor/IDE will be able to identify your project correctly.

Beware that in VSCode, you can switch the interpreter, so ensure you select one where your code is installed:

enter image description here

2 Comments

Hi! Thanks for your answer! I see what you mean; however, I would like to be able to change contents inside of queries folder as I go, and that is somewhat impractical because I need to repack it into package every time I make a change.
You don’t have to repack. If you use the -e option with pip, it’ll install locally, meaning you’ll be able to see the changes
0

https://blog.csdn.net/qq_41019681/article/details/111193080

"python.analysis.extraPaths": [
        "D:\\ProgramFiles\\Anaconda3-2021.05\\lib\\site-packages"
    ]

add "python.analysis.extraPaths" by

import package_name as pkg 
print(pkg.__file__)

1 Comment

Thanks for the base in a case 2024 the fall it works perfectly well at all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.