【问题标题】:How to fix 'ImportError: DLL load failed' in Visual Studio Code (packages installed with Anaconda)如何修复 Visual Studio Code 中的“ImportError: DLL load failed”(随 Anaconda 一起安装的软件包)
【发布时间】:2023-10-27 23:43:02
【问题描述】:

我已经安装了 Anaconda(以及随附的软件包)和 VS Code(从 Anaconda Navigator 中)。如果我从 Anaconda Navigator 或 The Anaconda Prompt 打开 VS Code,每个 import 都可以正常工作。 但是如果我直接打开 VS Code,它会抛出一个ImportError

例如:

from PIL import Image

给出以下错误:

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.

我该如何解决这个问题?

【问题讨论】:

  • 这可能是因为使用 Anaconda 打开 VS Code 可能会使用 conda 拥有的所有包“加载”它(指定 VS Code),而仅打开 VS Code 可能需要项目文件夹中的包因为它不知道从哪里得到它们(不是用 anaconda 打开的)。

标签: python-3.x dll visual-studio-code anaconda importerror


【解决方案1】:
  1. (类型)CTRL + SHIFT + P
  2. (搜索:)打开设置
  3. (点击:)首选项:打开设置(JSON)

然后添加三行配置:

{
    ... # 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"
}

最后,重启你的 VScode

【讨论】:

  • 它有效,但我对 VSCode 的期望更高。