Package cannot be found in vscode virtual environment

Error details:

Traceback (most recent call last):
  File "D:\software\Anaconda3\envs\swim\lib\site-packages\numpy\core\", line 22, in <module>
    from . import multiarray
  File "D:\software\Anaconda3\envs\swim\lib\site-packages\numpy\core\", line 12, in <module>
    from . import overrides
  File "D:\software\Anaconda3\envs\swim\lib\site-packages\numpy\core\", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed: The specified module cannot be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:/code/ViT/models/import", line 4, in <module>
    import numpy
  File "D:\software\Anaconda3\envs\swim\lib\site-packages\numpy\", line 150, in <module>
    from . import core
  File "D:\software\Anaconda3\envs\swim\lib\site-packages\numpy\core\", line 48, in <module>
    raise ImportError(msg)


Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was

We have compiled some common reasons and troubleshooting tips at:

Please note and check the following:

  * The Python version is: Python3.7 from "D:\software\Anaconda3\envs\swim\python.exe"
  * The NumPy version is: "1.21.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed: The specified module cannot be found.

However, there is no problem running the virtual environment in the cmd window

1.python can be used normally
2. The library can be called normally

But there is a problem in vscode.

python module lookup path
Every one in Python The py module is a module, and the python search module path is

1. Current folder path
2. File path corresponding to pythonpath environment variable
3. The standard link library and other folder paths of Python installation
4. Folder path listed in PTH file

Solution 1 is as follows:

1. Print path in vscode

import sys

for i in range(len(sys.path)):


Output result:


My local file is:


Case inconsistency.

So just add these to your environment variables.

But I think it's too troublesome. I have to add environment variables every time I create a virtual environment.

Solution 2 is as follows:

Using Anaconda's virtual environment in VScode, it is found that switching between different virtual environments is quite easy (click the Python... Button in the lower left corner to choose the environment freely). However, whether it's the command line (Terminal) or directly clicking the green button to run the code, it can't run normally, that is, I can't find the third-party library I installed myself. But if I use cmd directly, I can run the program normally.

It is found that my VScode command line does not activate the virtual environment of conda. Not even with conda activate. The search found that I am currently in command line mode of powershell rather than command line cmd mode.

This needs to be set by default The following statement is added to the JSON file to change the powershell mode to cmd mode. In fact, an instruction pointing to the system cmd is added.

"": "C:\\WINDOWS\\System32\\cmd.exe",

Statement addition method -- setting How to open JSON?
1. System setting JSON: click the gear button in the lower left corner, or file preferences settings.

Then add the above statement, exit the software after saving and reopen it. Now you can use the cmd command

Enter conda activate virtual environment in the vscode terminal

Run the program again and no error will be reported.

Keywords: Python IDE Visual Studio Code

Added by fwbbetsy on Thu, 17 Feb 2022 07:39:14 +0200