简体   繁体   中英

“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 .

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

In case anyone else comes across this, I had the same problem using VS2015. Adding the path to the virtual environment being used (eg C:\\Users\\<your user name>\\AppData\\Local\\Continuum\\Miniconda2\\envs\\<environment with cntk> ) in the Environment variable PATH then restarting visual studio resolved the issue

我最终在路径中添加了D:\\ Docs \\ Anaconda3 \\ Lib \\ site-packages \\ cntk,因为我在D:\\ Docs \\ Anaconda3中安装了anaconda

When switching/activating conda environment in Anaconda Console, the script will set/change bunch of environment variables. It seems PyCharm didn't simulate such operation. I hope PyCharm could provide better solution, I worked around this issue by adding "C:\\Users\\\\AppData\\Local\\Continuum\\anaconda3\\envs\\" into environment variable.

Here is how I deal with it:

  1. At the right corner of PyCharm Navigation bar, there is drop-down menu with your current model name on it. choose "Edit Configurations..."
  2. On the right window pane, you should be able to see "Configuration" tab, and in "Environment" session, there is "Environment variables:" input box
  3. Click "..." to edit Environment variables
  4. Click "+" and add "PATH" variable, the value is set to "C:\\Users\\\\AppData\\Local\\Continuum\\anaconda3\\envs\\"
  5. Save your change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM