Home > Net >  vscode having issues loading modules
vscode having issues loading modules

Time:03-05

Lately I've been having issues with VScode not recognizing installed modules.

I'm using anaconda 2.1.2, the virtual environment has python 3.8.12 and VScode is version 1.61.2 on a win10 os.

The two modules I know do this are kivy and opencv. Pandas, numpy and many many other modules work fine.

When I was working on the kivy script I found that while trying to run the script from VScode I would get a ModuleNotFoundError: No module named 'kivy' error and pylance was saying 'Import "kivy" could not be resolved', with kivy having the squiggly underline. But eventual I discovered that by running the script through the terminal it would work. It was a very simple test script so I just trudged through without a properly working intellisense or the VScode debugging. But now I'm trying to do a more complex program with opencv in a new environment and the same exact thing is happening.

Here is the test script:

import cv2

img = cv2.imread("galaxy.jpg",0)

print(type(img))
print(img)

when run through VScode the output is:

    import cv2
ModuleNotFoundError: No module named 'cv2'

when run through VScode terminal with python script_test.py I get:

<class 'numpy.ndarray'>
[[14 18 14 ... 20 15 16]
 [12 16 12 ... 20 15 17]
 [12 13 16 ... 14 24 21]
 ...
 [ 0  0  0 ...  5  8 14]
 [ 0  0  0 ...  2  3  9]
 [ 1  1  1 ...  1  1  3]]

pip freeze for the environment gives:

certifi==2021.10.8
mkl-fft==1.3.1
mkl-random @ file:///C:/ci/mkl_random_1626186184278/work
mkl-service==2.4.0
numpy @ file:///C:/ci_310/numpy_and_numpy_base_1643798589088/work
opencv-python==4.5.5.62
six @ file:///tmp/build/80754af9/six_1644875935023/work
wincertstore==0.2

I went over several similar posts, but the only answers I saw where to uninstall and reinstall using a different method, i.e.: conda install -c conda-forge opencv,conda install opencv or some variation of a pip command, but none seemed to change anything.

ANY help to solve this issue would be greatly appreciated. If I have to develop this the way I did for my kivy script I will go insane!

CodePudding user response:

I have no idea why this worked, but it did.

I opened vscode by right clicking and launching from the folder with the script. Two windows error boxes popped up, both saying:

"This app can't run on your PC.

To find a version for your PC, check with the software publisher"

After clicking ok for both VScode gave me a warning box in the bottom right corner saying no interrupter was installed. So I hit shift ctrl P, selected the interrupter for the virtual environment and vscode saw opencv and everything worked perfectly.

I closed down vscode tried it again, same windows error boxes, same successful result.

Without opening vscode this way, I get no error box, but shift crtl p, selecting the same exact interpreter has no effect and VScode still can't find opencv.

I can deal with clicking ok on an error box and consider this solved, but if anyone has an answer why this worked I'd be very curious.

  • Related