Pycharm 中的 DLL 导入错误,但在外部工作正常

DLL import error in Pycharm, but works fine outside

我在 Windows 机器上安装了新的 Anaconda 和 PyCharm。

简直运行

import numpy

加注

ImportError: DLL load failed: The specified module could not be found.

在类似的问题(1, 2)中,人们建议检查 PATH 是否有 DLL 的路径。 就我而言-确实如此!但是,当我从 PyCharm 开始时,我在 PATH 前面加上了额外的路径。我认为这可能是问题所在。

当来自 Anaconda 提示的 运行 时,sys.path 是:

C:\Users\usr\Anaconda3\python37.zip
C:\Users\usr\Anaconda3\DLLs
C:\Users\usr\Anaconda3\lib
C:\Users\usr\Anaconda3
C:\Users\usr\Anaconda3\lib\site-packages
C:\Users\usr\Anaconda3\lib\site-packages\win32
C:\Users\usr\Anaconda3\lib\site-packages\win32\lib
C:\Users\usr\Anaconda3\lib\site-packages\Pythonwin

当 python console in Pycharm 中的 运行 时,sys.path 是相同的,除了现在文件开头有两行:

C:\Users\usr\AppData\Local\JetBrains\Toolbox\apps\PyCharm-C\ch-02.4505.26\helpers\pydev
C:\Users\usr\AppData\Local\JetBrains\Toolbox\apps\PyCharm-C\ch-02.4505.26\helpers\pydev

顺便提一下,最初的错误是从C:\Users\usr\AppData\Local\JetBrains\Toolbox\apps\PyCharm-C\ch-02.4505.26\helpers\pydev\_pydev_bundle\pydev_import_hook.py.
引发的 所以我认为这找到了问题所在,但我不知道它为什么会出现,为什么它没有出现在我的其他机器上,更重要的是,如何解决它?


技术数据:

PyCharm 2018.2.4 (Community Edition)
Build #PC-182.4505.26, built on September 19, 2018
JRE: 1.8.0_152-release-1248-b8 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

Windows 10 10.0

Python: 3.7, 3.6.6;

numpy: 1.15.4; 1.14.6

这是一个开放的错误; This answer 来自 Jetbrains 问题跟踪器,暂时为我解决了这个问题。

  • open command line
  • activate conda venv
  • get the correct PATH value by echo %PATH%
  • open PyCharm and set new PATH value
    • for Python console in Settings | Build, Execution, Deployment | Console | Python Console | Environment variables
    • for running scripts in Run | Edit Configurations ... | | Environment variables

UPD: 2019.1修复的bug

我最后从 pydev 找到的是这个解决方法,它类似于 pycharm 但来自 eclipse IDE 作为插件:

"As a note, right now the workaround can be starting Eclipse from that shell, going to the related interpreter and adding a 'PATH' environment variable to match the one which is expected to run conda (you can force environment variables for a specific interpreter)." Link

这可能可以用与 pycharm 相同的方式完成,即。从正确的环境中启动 pycharm,并显式设置路径变量。