CNTK(微软认知工具包)导入错误
CNTK (Microsoft Cognitive Toolkit) ImportError
代码:
import cntk
n = cntk.minus([1, 2, 3], [4, 5, 6]).eval()
print(n)
问题:
If run the code line by line in python command line, it's ok.
If run "Python main.py", it's also ok.
But if run the code in PyCharm or Visual Studio Code, error occurs, PyCharm error dump:
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.
环境:
- 蟒蛇:4.3.1
- Python: 3.5.3(anaconda 中的虚拟环境)
- CNTK:windows amd64 GPU
的 2.0beta12
- PyCharm: 2016.3.2
- Visual Studio代码:1.10.2
已解决
因为找不到带CNTK的DLL文件(如CNTKLibrary-2.0.dll、EvalDll.dll),安装后这些DLL都在"python.exe"的同一目录下CNTK 的。
有2种解决方案:
(suppose PYTHONDIR 是 python.exe 所在的目录)
更改系统环境 PATH 以包含 PYTHONDIR
在"import cntk"前添加如下代码:
进口os; os.environ['PATH'] = PYTHONDIR + ';' + os.environ['PATH']
代码:
import cntk
n = cntk.minus([1, 2, 3], [4, 5, 6]).eval()
print(n)
问题:
If run the code line by line in python command line, it's ok.
If run "Python main.py", it's also ok.
But if run the code in PyCharm or Visual Studio Code, error occurs, PyCharm error dump:
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.
环境:
- 蟒蛇:4.3.1
- Python: 3.5.3(anaconda 中的虚拟环境)
- CNTK:windows amd64 GPU 的 2.0beta12
- PyCharm: 2016.3.2
- Visual Studio代码:1.10.2
已解决
因为找不到带CNTK的DLL文件(如CNTKLibrary-2.0.dll、EvalDll.dll),安装后这些DLL都在"python.exe"的同一目录下CNTK 的。
有2种解决方案:
(suppose PYTHONDIR 是 python.exe 所在的目录)
更改系统环境 PATH 以包含 PYTHONDIR
在"import cntk"前添加如下代码:
进口os; os.environ['PATH'] = PYTHONDIR + ';' + os.environ['PATH']