Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'ImportError: DLL load failed' in Visual Studio Code (packages installed with Anaconda)

I have installed Anaconda (with the packages that come with it) and VS Code (from within the Anaconda Navigator). If I open VS Code from the Anaconda Navigator or from The Anaconda Prompt, every import works well. But if I open VS Code directly, it throws an ImportError.

For example:

from PIL import Image

Gives the following error:

Traceback (most recent call last):
    File "c:\MyPrograms\Coding\Scripts\imageOperations.py", line 7, in 
    <module>
        from PIL import Image
    File "C:\MyPrograms\Coding\Software\Python\lib\site- 
    packages\PIL\Image.py", line 93, in <module>
        from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.

How do I fix this problem?

like image 362
Vamoos Avatar asked Jun 27 '19 07:06

Vamoos


People also ask

Does Visual Studio Code work with anaconda?

In Visual Studio Code you can run Python code with Anaconda by using the Anaconda Prompt, updating the Visual Studio Code workspace settings to be aware of your Anaconda installation, or adding Anaconda to the Windows Path variable.

How does Visual Studio Code fix import error in Python?

To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.


1 Answers

  1. (type) CTRL + SHIFT + P
  2. (search for:) open settings
  3. (click:) Preferences: Open Settings (JSON)

Then add three line configuration:

{
    ... # any other settings you have already added (remove this line)

    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    "terminal.integrated.shellArgs.windows": ["/K", "C:\\Anaconda3\\Scripts\\activate.bat C:\\Anaconda3"],
    "python.condaPath": "C:\\Anaconda3\\Scripts\\conda.exe"
}

Finally, Restart your VScode

like image 187
Zhongyi Avatar answered Sep 29 '22 16:09

Zhongyi