python中的CNTK(Microsoft Cognitive Toolkit)会导致ImportError

时间:2017-03-13 12:51:21

标签: python machine-learning cntk

代码:

import cntk
n = cntk.minus([1, 2, 3], [4, 5, 6]).eval()
print(n)

问题:

  

如果在python命令行中逐行运行代码,那就没问题。

     

如果运行" Python main.py",它也可以。

     

但是如果在PyCharm或Visual Studio Code中运行代码,则会发生错误,PyCharm错误转储:

Traceback (most recent call last):
File "D:\Apps\Anaconda3\envs\Python3.53\lib\site-packages\cntk\cntk_py.py", line 18, in swig_import_helper
  return importlib.import_module(mname)
File "D:\Apps\Anaconda3\envs\Python3.53\lib\importlib\__init__.py", line 126, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'cntk._cntk_py'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Dev/machine_learning/Test_CNTK/main.py", line 1, in <module>
    import cntk
  File "D:\Apps\Anaconda3\envs\Python3.53\lib\site-packages\cntk\__init__.py", line 11, in <module>
    from .core import *
  File "D:\Apps\Anaconda3\envs\Python3.53\lib\site-packages\cntk\core.py", line 10, in <module>
    from . import cntk_py
  File "D:\Apps\Anaconda3\envs\Python3.53\lib\site-packages\cntk\cntk_py.py", line 21, in <module>
    _cntk_py = swig_import_helper()
  File "D:\Apps\Anaconda3\envs\Python3.53\lib\site-packages\cntk\cntk_py.py", line 20, in swig_import_helper
    return importlib.import_module('_cntk_py')
  File "D:\Apps\Anaconda3\envs\Python3.53\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed: The specified module could not be found.

环境:

  • Anaconda:4.3.1
  • Python:3.5.3(anaconda中的虚拟环境)
  • CNTK:2.0 amd12 for windows amd64 GPU
  • PyCharm:2016.3.2
  • Visual Studio代码:1.10.2

1 个答案:

答案 0 :(得分:3)

解决

因为它无法找到带有CNTK的DLL文件(例如CNTKLibrary-2.0.dll,EvalDll.dll),所以这些DLL在安装CNTK后位于“python.exe”的同一目录中。

有两种解决方案:

(假设PYTHONDIR是python.exe所在的目录)

  1. 将系统环境路径更改为包含PYTHONDIR

  2. 在“import cntk”之前添加以下代码:

    导入os; os.environ ['PATH'] = PYTHONDIR +';' + os.environ ['PATH']