How to resolve Pylance "import could not be resolved" in VSCode Jupyter Notebook? - visual-studio-code

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!

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

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 everything worked as usual and fixed that problem

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:

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__)

Related

python interpreter citing zen of python in the terminal window

I run vscode version 1.65.2.
Today it started citing the whole "The Zen of Python" in the terminal window every time I run a python script in the debugger.
EDIT: It turns out that it is the python interpreter that does it, not vscode. The problem apparently came after I updated ipykernel to version 6.11.0. I run python version 3.9.1 on Windows.
How can I turn it off?
I havent been able to find an answer to this anywhere.
It turned out that an import statement had found its way into my code: "import this as d", and removing it solved the problem. It is a mystery how it ended up there, I certainly haven't put it there knowingly. But problem solved!
That's really odd. I just had the exact same thing. Even the import this as d was on the first line before everything. I use vscode as well. Removing it does help. But I didn't update a kernel prior to this. It happened while I was writing code I think. Python 3.8.10 on Linux.

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

Regarding "torch" package - with anaconda environment using vscode as editor

I am currently working on project,where I use pytorch as deep learning environment. I have serious problem, that .... "When I use anaconda jupyter notebook terminal to run my *.py files. Then, it run properly. But, when I use the same environment within vs-code via python_interpretor, I can not get result and give me error "torch module can not found".
Can anyone have solution?. Actually, I see the packages using "conda list" and it includes torchvision inside environment. I also tried to install torch in base environment But, it does not work.
Please give me some solution. Thank you for your consideration.

VS Code importing the multiarray numpy extension module failed

import numpy
is not working. This started because I couldn't get PyQt5 to work on my home computer. In the Spyder IDE, I can import modules and get everything working fine, but in VS Code, I can't import basic modules. I'm on Windows 10 with Python 3.7. I've Googled a lot and have tried these:
Uninstall Anaconda and VS Code, and reinstall both
In VS Code, press Ctrl+Shift+P, Select Interpreter, and select Python 3.7.1 64-bit ('base':conda), ~\Anaconda3\python.exe
I saw on another similar question that python3.dll may be missing from one of my Anaconda directories, but it is indeed there.
Add environment variable to PATH by going to System Properties > Advanced tab > Environment Variables... > under System Path, Edit, New > C:\Users\myname\Anaconda3\Scripts
Once I did #4 above, the new error message in VS Code when importing numpy is DLL load failed: The specified module could not be found. And in the terminal, I see: The term 'conda' is not recognized as the name of the cmdlet, ...
One thing I notice is in a Python prompt (not Anaconda) I type:
import sys
print(sys.version)
The output is 3.7.2, while in VS Code, the Select Interpreter (see above #2) says Python 3.7.1. Not sure if this is important.
Any help? Everything works on my work computer fine, but it's just my home computer that is being difficult.
What ended up working for me is downgrading my Python installation from 3.7.1 to 3.6.4 with:
conda install python=3.6.4
Hope this helps someone out there.
On another question for the same problem, there was a suggestion that worked for me.
Put this on the variable path.
C:\Users\<Your user name>\Anaconda3\Library\bin
credit to user1024.

Rust autocompletion not shown for items from the standard library

I can't get working autocompletion on VS Code on Ubuntu 16.04.
I've installed rustup from https://www.rustup.rs/ and installed the "rust-lang.rust" package. This extension installed rustfmt and tried to use both stable and nightly toolchains.
If I type std:: no suggestions are shown. Suggestions show on local mod import but not with the standard library. I tried to reinstall by removing via uninstall.sh and manually removing ~/.multirust and ~/.cargo and then installed again, but nothing changed.
What did I do wrong?
I didn't have the RUST_SRC_PATH variable set. In my VS Code settings, I added
"rust.rustLangSrcPath": "/home/ilya/.rustup/toolchains/**your_toolchain**/lib/rustlib/src/rust/src/"
and it did the job. This option comes from the kalitaalexey.vscode-rust package.