简体   繁体   English

“ import cntk”可从命令行运行,但不能在PyCharm中运行

[英]“import cntk” works from command line but not in PyCharm

I'm using Anaconda3, python 3.6.1, and PyCharm 2017.1.5. 我正在使用Anaconda3,python 3.6.1和PyCharm 2017.1.5。

Using conda, I can perform the following just fine: 使用conda,我可以执行以下操作:

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

(note: cntk is a conda virtual environment) (注意:cntk是conda虚拟环境)

(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: 进入PyCharm:

I have a file, tutorial101.py , that just contains import cntk . 我有一个文件tutorial101.py ,仅包含import cntk

Running this in PyCharm: 在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? 为什么从命令行执行此操作,但在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. 问题在于,使用Pycharm时设置的系统环境不包括C:\\Anaconda3目录作为Windows文件系统搜索路径的一部分。 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. 该目录必须出现在搜索路径中,因为CNTK DLL将已经安装(假定使用标准的conda安装)到该目录。

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. 通过将C:\\ANACONDA3 (或CNTK的安装路径)添加到系统的PATH环境变量中,可以解决此问题。

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 . 您可以在Windows中的“ System->Advanced System Settings->Environment Variables下访问System->Advanced System Settings->Environment Variables ,然后通过选择它并单击“ Edit将新行添加到系统PATH变量中。

Ian 伊恩

In case anyone else comes across this, I had the same problem using VS2015. 万一其他人遇到这个问题,我在使用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 在环境变量PATH中将路径添加到正在使用的虚拟环境(例如C:\\Users\\<your user name>\\AppData\\Local\\Continuum\\Miniconda2\\envs\\<environment with cntk> )中,然后重新启动Visual Studio解决了该问题

我最终在路径中添加了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. 在Anaconda控制台中切换/激活conda环境时,脚本将设置/更改一系列环境变量。 It seems PyCharm didn't simulate such operation. 似乎PyCharm没有模拟这种操作。 I hope PyCharm could provide better solution, I worked around this issue by adding "C:\\Users\\\\AppData\\Local\\Continuum\\anaconda3\\envs\\" into environment variable. 我希望PyCharm可以提供更好的解决方案,我通过在环境变量中添加“ C:\\ Users \\\\ AppData \\ Local \\ Continuum \\ anaconda3 \\ envs \\”来解决此问题。

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. 在PyCharm导航栏的右上角,有一个下拉菜单,上面带有您的当前型号名称。 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\\" 单击“ +”并添加“ PATH”变量,该值设置为“ C:\\ Users \\\\ AppData \\ Local \\ Continuum \\ anaconda3 \\ envs \\”
  5. Save your change 保存您的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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