Cntk: ImportError: No module named 'cntk'

Created on 26 Oct 2016  路  18Comments  路  Source: microsoft/CNTK

After installation, when I try to run the first example, I get the following error.

Any guess? I'm not sure if it's the installation issue, or just clashing with my existing Python (Anaconda) installation...

p.s I did run the following command before trying it
C:\local\cntk\scripts\cntkpy34.bat

PS C:repos\CNTK\bindings\python\examples> python NumpyInterop\FeedForwardNet.py
Traceback (most recent call last):
File "NumpyInterop\FeedForwardNet.py", line 10, in
from cntk.device import cpu, set_default_device
ImportError: No module named 'cntk'

All 18 comments

After adding C:repos\CNTK\bindings\python to PYTHONPATH, the error changed to below:


PS C:repos\CNTK\bindings\python\examples> python NumpyInterop\FeedForwardNet.py
Traceback (most recent call last):
File "NumpyInterop\FeedForwardNet.py", line 10, in
from cntk.device import cpu, set_default_device
File "C:repos\CNTK\bindings\python\cntk__init__.py", line 11, in
from . import ops
File "C:repos\CNTK\bindings\python\cntk\ops__init__.py", line 7, in
from . import sequence
File "C:repos\CNTK\bindings\python\cntk\ops\sequence__init__.py", line 6, in
from ...utils import sanitize_input, sanitize_shape, get_data_type, typemap
File "C:repos\CNTK\bindings\python\cntk\utils__init__.py", line 11, in
from .. import cntk_py
ImportError: cannot import name 'cntk_py'

Hi -

it looks like you are executing the batch file
C:\local\cntk\scripts\cntkpy34.bat
out of your powershell environment

Please make sure your run the script from a standard command shell

thx
Wolfgang

Works now. Thanks a lot!

Better to spell out that it's CMD, not Powershell in the instructions.

Acutally, I still have the issue in my desktop, although I followed the same procedure, and found that I have the cntk_py.i file in folder C:repos\CNTK\bindings\python\cntk.


(C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34) c:repos\CNTK\bindings\python\examples>python
Python 3.4.4 |Continuum Analytics, Inc.| (default, Jun 15 2016, 15:25:08) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import cntk
Traceback (most recent call last):
File "", line 1, in
File "C:repos\CNTK\bindings\python\cntk__init__.py", line 11, in
from . import ops
File "C:repos\CNTK\bindings\python\cntk\ops__init__.py", line 7, in
from . import sequence
File "C:repos\CNTK\bindings\python\cntk\ops\sequence__init__.py", line 6, in
from ...utils import sanitize_input, sanitize_shape, get_data_type, typemap
File "C:repos\CNTK\bindings\python\cntk\utils__init__.py", line 11, in
from .. import cntk_py
ImportError: cannot import name 'cntk_py'
import sys
sys.path
['', 'C:\srcroot\lib\py', 'C:\srcroot\PoXo', 'C:repos\CNTK\bindings\python', 'C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34\python34.zip', 'C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34\DLLs', 'C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34\lib', 'C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34', 'C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34\lib\site-packages', 'C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py34\lib\site-packages\setuptools-27.2.0-py3.4.egg']

Hey,
any chance you've got 'C:repos\cntk\bindings\python' in your PYTHONPATH environment variable on your desktop? That will make it try to import CNTK not from the module installed in your environment, but from the checked out source module (bindings\python\cntk).
Best, Mark

I'm having the same issue with python cannot refer to the correct cntk path while running the commandC:\repos\CNTK\bindings\python\examples> python NumpyInterop\FeedForwardNet.py
and the powershell gave me:
File "NumpyInterop\FeedForwardNet.py", line 10, in
from cntk.device import cpu, set_default_device
ImportError: No module named 'cntk'

I ran the C:\local\cntk\scripts\cntkpy34.bat script both from the CMD shell and the PowerShell without the admin permission.

And the I restarted the PowerShell and ran C:\repos\CNTK\bindings\python\examples> python
it gave me the following error:

python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I added the anaconda path to the environment variable with C:\repos\CNTK\bindings\python\examples>$env:path="$env:path;c:\local\Anaconda3-4.1.1-Windows-x86_64" and fixed the error.

After that I ran C:\repos\CNTK\bindings\python\examples> python
The shell gave me:

PS C:\repos\CNTK\bindings\python\examples> python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

At this point I check the sys.path and tried to import cntk

Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64\\python35.zip', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64\\DLLs', 'C:\\
local\\Anaconda3-4.1.1-Windows-x86_64\\lib', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64', 'C:\\local\\Anaconda3-4.1.1-Wi
ndows-x86_64\\lib\\site-packages', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64\\lib\\site-packages\\Sphinx-1.4.1-py3.5.eg
g', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64\\lib\\site-packages\\win32', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64\\
lib\\site-packages\\win32\\lib', 'C:\\local\\Anaconda3-4.1.1-Windows-x86_64\\lib\\site-packages\\Pythonwin', 'C:\\local\
\Anaconda3-4.1.1-Windows-x86_64\\lib\\site-packages\\setuptools-23.0.0-py3.5.egg']
>>> import cntk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cntk'
>>>

python still cannot find the installed cntk path.

@ToxidoLiu: Could you please try running the batch script from a windows CMD prompt instead of a PS.

@sayanpa I tried running script both in CMD prompt and PS and then did the rest of the thing but I don't think that worked.

@mahilleb-msft Thanks. That solved the issue.

how this issue is closed?

I have set the path as below and still seeing the error
C:repos\cntk\CNTK-master\bindings\python\examples>set PYTHONPATH=c:repos\cntk\
bindings\python\examples;%PYTHONPATH%

C:repos\cntk\CNTK-master\bindings\python\examples>python NumpyInterop\FeedForwa
rdNet.py
Traceback (most recent call last):
File "NumpyInterop\FeedForwardNet.py", line 10, in
from cntk.device import cpu, set_default_device
ImportError: No module named 'cntk'

@Nagarc Same thing here. Running from standard command shell, tried to specify PYTHONPATH (was not specified among environment variables). How is the issue closed. Can anyone give a step-by-step solution?

UPD: Ok, there seems to be no step-by-step solution. In my case, the batch file cntkpy34.bat didn't work correctly. I have erased several superfluous varaibles from Path which appeared to be too long, then it worked OK. But now when I try running and got this

c:\repos\CNTK\bindings\python\examples>python NumpyInterop/FeedForwardNet.py
Traceback (most recent call last):
  File "NumpyInterop/FeedForwardNet.py", line 10, in <module>
    from cntk.device import cpu, set_default_device
  File "C:\repos\cntk\bindings\python\cntk\__init__.py", line 11, in <module>
    from . import ops
  File "C:\repos\cntk\bindings\python\cntk\ops\__init__.py", line 7, in <module>
    from . import sequence
  File "C:\repos\cntk\bindings\python\cntk\ops\sequence\__init__.py", line 6, in <module>
    from ...utils import sanitize_input, sanitize_shape, get_data_type, typemap
  File "C:\repos\cntk\bindings\python\cntk\utils\__init__.py", line 10, in <module>
    import scipy.sparse
ImportError: No module named 'scipy'

After that I've tried to execute c:repos\CNTK\bindings\python\examples>set PYTHONPATH=c:repos\cntk\bindings\python\examples;%PYTHONPATH%

Then I've got to the original problem.

the issue was closed because jykim solved his problem. So he closed it ...

not really clear you are seeing the same problem ...

some initial questions -

did you do a binary install, or just a build from source?
you have another anaconda install on your machine?

thx
Wolfgang

You should specify the python path. So add PYTHONPATH C:\local\Anaconda3-4.1.1-Windows-x86_64 to the environment.

Thanks @stevewyl . I have reinstalled CNTK once again, now having the system working. Still don't see PYTHONPATH among my variables, but if something happens I'll try specifying it.

Hey - setting PYTHONPATH should not be necessary for binary install of CNTK.
For dev installs we currently require it to point to the in-source version of the CNTK module. We' re probably going to revisit this and provide a different set of steps (for developers) that doesn't rely on this.

Please, open a new issue if you still experience installation issues (dev or binary).

Thanks, Mark

SOLVED:
This also caused me a lot of pain, but finally got to the bottom of it.
I've installed the binary version of CNTK on Windows10 (manual install, not script)
And like many others I was getting "ImportError: No module named cntk" when running fastfeedforward.py

When I checked my environment variables I had no PYTHONPATH
After much waste of time I simply set PYTHONPATH to C:\local\Anaconda3\Lib\site-packages
and hey presto!

Which makes sense since python needed to know where to find the cntk module (folder)

Please modify the manual installation notes to reflect this

What are recommended settings if I want to use/bind python with binaries I built myself? I was also getting lot of grief like many others and changed to PYTHONPATH to C:\local\Anaconda3\Lib\site-packages which now uses bindings that are downloaded but not the one in my repo.

If I use setx PYTHONPATH c:repos\cntk\bindings\python;%PYTHONPATH%

I get
(cntk-py35) F:\users\localuser\cntk-2.X\CNTK\Tutorials\NumpyInterop>python FeedForwardNet.py
Traceback (most recent call last):
File "FeedForwardNet.py", line 8, in
from cntk.device import cpu, try_set_default_device
File "F:\users\localuser\cntk-2.X\cntk\bindings\python\cntk__init__.py", line 10, in
from . import cntk_py
ImportError: cannot import name 'cntk_py'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rhoens picture rhoens  路  36Comments

youssefhb picture youssefhb  路  27Comments

StevenGann picture StevenGann  路  125Comments

kasungayan picture kasungayan  路  20Comments

cha-zhang picture cha-zhang  路  40Comments