Search code examples
pythonanacondacntk

"import cntk" works from command line but not in PyCharm


I'm using Anaconda3, python 3.6.1, and PyCharm 2017.1.5.

Using conda, I can perform the following just fine:

(C:\Anaconda3) C:\Users\tim>activate cntk

(note: cntk is a conda virtual environment)

(cntk) C:\Users\tim>python
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:25:24) 
[MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cntk

Here it waits for a while... but works.

>>> print(cntk.__version__)
2.0

So, working fine.

On to PyCharm:

I have a file, tutorial101.py, that just contains import cntk.

Running this in PyCharm:

C:\Anaconda3\envs\cntk\python.exe C:/coding/python/cntk-sandbox/tutorial101.py
Traceback (most recent call last):
  File "C:\Anaconda3\envs\cntk\lib\site-packages\cntk\cntk_py.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Anaconda3\envs\cntk\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cntk._cntk_py'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/coding/python/cntk-sandbox/tutorial101.py", line 1, in <module>
    import cntk
  File "C:\Anaconda3\envs\cntk\lib\site-packages\cntk\__init__.py", line 10, in <module>
    from . import cntk_py
  File "C:\Anaconda3\envs\cntk\lib\site-packages\cntk\cntk_py.py", line 21, in <module>
    _cntk_py = swig_import_helper()
  File "C:\Anaconda3\envs\cntk\lib\site-packages\cntk\cntk_py.py", line 20, in swig_import_helper
    return importlib.import_module('_cntk_py')
  File "C:\Anaconda3\envs\cntk\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.

Process finished with exit code 1

Why does this work from the command line, but not in PyCharm? What am I doing wrong? I can't find a satisfactory answer anywhere.


Solution

  • The issue is that the system environment that is setup when you are using Pycharm does not include the C:\Anaconda3 directory as part of the Windows file system search path. It is necessary for this directory to appear in the search path because the CNTK DLLs will have been installed (assuming a standard conda install) to that directory.

    You will be able to fix this issue by adding C:\ANACONDA3 (or the path where CNTK was installed) to the PATH environment variable for your system.

    You can access the environment variables in Windows under System->Advanced System Settings->Environment Variables, then add a new row to the system PATH variable by selecting it and clicking Edit.

    Ian