64

I am trying to work on neural networks in Python using the following Keras packages:

from keras.utils import np_utils
from keras.layers.core import Dense, Activation, Dropout
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD

But, I am getting the following error:

 15 import theano
 ---> 16 from theano import gof
 17 from theano.compat.python2x import partial
 18 import theano.compile.mode
 ImportError: cannot import name gof

Installing installed conda install keras. Later I tried to use pip install Theano, but it did not work. I Tried to install using pip install git, but I am getting this error: cannot find command git. So I installed Git and I set the environment variables.

So, is there any procedure to install these packages?

Denis
  • 10,882
  • 13
  • 74
  • 133
pavikirthi
  • 1,461
  • 3
  • 13
  • 21
  • 4
    Possible duplicate of [How to install Theano on Anaconda Python 2.7 x64 on Windows?](http://stackoverflow.com/questions/33687103/how-to-install-theano-on-anaconda-python-2-7-x64-on-windows) – Franck Dernoncourt Dec 05 '15 at 02:39
  • 1
    The key part of the answer to the question this is a duplicate of might be `conda install mingw libpython`. – Daniel Renshaw Dec 05 '15 at 10:04
  • 2
    Not for OP so much as for future googlers: A very comprehensive guide https://github.com/philferriere/dlwin – fiat May 29 '17 at 12:24

8 Answers8

124

It is my solution for the same problem

  • Install TDM GCC x64.
  • Install Anaconda x64.
  • Open the Anaconda prompt
  • Run conda update conda
  • Run conda update --all
  • Run conda install mingw libpython
  • Install the latest version of Theano, pip install git+git://github.com/Theano/Theano.git
  • Run pip install git+git://github.com/fchollet/keras.git
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Yelaman
  • 1,463
  • 1
  • 11
  • 10
  • 14
    This is great, a few comments tho: 1) I had mingw g++ installed and this was causing issues so I had to run `mingw-get remove g++ gcc` to ensure that TDM was being used 2) recommend to run `pip install git+git://github.com/fchollet/keras` instead of `pip install keras`. Gets all the latest goodness. – gatapia Feb 23 '16 at 23:06
  • 1
    Installing the latest version of keras is important as it is developing rapidly! – pir Apr 04 '16 at 12:58
  • 5
    A disclaimer on this: It doesn't work with python 3.5 as of this writing (Theano needs libpython, which no one figured how to get working in 3.5). Use 2.7 or 3.4 for the time being. – bruceoutdoors May 23 '16 at 14:52
  • 6
    I followed these instructions and it appears to work, but for us complete novices, there are two clarifications: (1) The "Anaconda prompt" is found in Windows by Start/All Programs/Anaconda2/Anaconda Prompt, and (2) For some reason, the "pip install" seems to hang. I get the message "successfully installed" but I do not receive the command prompt afterwards. Disconcerting. – Marc Meketon Jul 04 '16 at 13:23
  • 2
    After adding git to my path I get a very long error message when running "pip install git+git://github.com/Theano/Theano.git". The main error seams to be: "no lapack/blas resources found" – Hacky Sep 07 '16 at 06:46
  • 1
    If while doing the pip installs you receive the error - `failed with error code 128 in None`, it may be because you have a git repository listed as a dependency in your requirement.txt file. To resolve, just change `pip install git+git://github.com/Theano/Theano.git` with `pip install git+http://github.com/Theano/Theano.git`. Same goes with the `keras` line – user1993 Dec 06 '16 at 19:49
  • I would like to add that if you use anaconda as the environment with python 2.7, while also using python 3.5+ installed on your system for everything else, use conda install for keras and not pip install – KannarKK Feb 11 '17 at 20:27
  • I have installed git and Command Prompt recognises `git` command. Although, when I use `pip install git+git://github.com/Theano/Theano.git` in Anaconda Prompt, it gives me - _ Error [WinError 2] The system cannot find the file specified while executing command git clone -q git://github.com/Theano/Theano.git C:\Users\Kritika\AppData\Local\Temp\pip-ajpc_pga-build Cannot find command 'git' _ – Krithi07 Feb 01 '18 at 11:50
37

The trick is that you need to create an environment/workspace for Python. This solution should work for Python 2.7 but at the time of writing keras can run on python 3.5, especially if you have the latest anaconda installed (this took me awhile to figure out so I'll outline the steps I took to install KERAS in python 3.5):

Create environment/workspace for Python 3.5

  1. C:\conda create --name neuralnets python=3.5
  2. C:\activate neuralnets

Install everything (notice the neuralnets workspace in parenthesis on each line). Accept any dependencies each of those steps wants to install:

  1. (neuralnets) C:\conda install theano
  2. (neuralnets) C:\conda install mingw libpython
  3. (neuralnets) C:\pip install tensorflow
  4. (neuralnets) C:\pip install keras

Test it out:

(neuralnets) C:\python -c "from keras import backend; print(backend._BACKEND)"

Just remember, if you want to work in the workspace you always have to do:

C:\activate neuralnets

so you can launch Jupyter for example (assuming you also have Jupyter installed in this environment/workspace) as:

C:\activate neuralnets
(neuralnets) jupyter notebook

You can read more about managing and creating conda environments/workspaces at the follwing URL: https://conda.io/docs/using/envs.html

Mel
  • 4,929
  • 10
  • 33
  • 39
Denis
  • 10,882
  • 13
  • 74
  • 133
  • 2
    You will need to install jupyter notebook within the environment as well – KannarKK Feb 13 '17 at 01:27
  • 5
    @KannarKK, you are correct. If you want to use jypiter or other tools you will need to install them in this environment that you created as in `(neuralnets) C:\conda install jypiter`. Thank you for that addition. – Denis Feb 13 '17 at 19:18
  • 2
    I just want to say thank you - i've spent almost an entire day trying out different ways to install keras and get it to work, and this is both the easiest and the only one to actually work for me – Alexvonrass Mar 19 '17 at 21:49
  • 1
    Amen to that! Worked like a charm. Looks like keras picks TensorFlow as the default backend. How do I change it to Theano if needed? – ultrasounder Apr 24 '17 at 18:22
  • 1
    @ultasounder: `%USERPROFILE%\.keras\keras.json` – Denis Apr 28 '17 at 14:33
  • just worked!! Thanks. Looks like the default backend is Tensoflow - it print:Using TensorFlow backend. I wonder then why to install theano then? Keras works with TensorFlow by default. – javed May 07 '17 at 08:37
  • Does not work - I get after doing all these steps: Exception: Compilation failed (return status=1): cc1plus.exe: sorry, unimplemented: 64-bit mode not compiled in. – Brana Mar 09 '19 at 21:58
  • I would suggest you google the exact error message you are seeing – Denis Mar 11 '19 at 03:03
  • In case anyone is wondering about compatibility with Python 3.7, installing keras and tensorflow with the above approach works fine now (haven't tried theano) – madimov May 20 '20 at 13:04
30

In windows with anaconda, just go on conda prompt and use this command

conda install --channel https://conda.anaconda.org/conda-forge keras
Gaurav Khare
  • 1,803
  • 4
  • 21
  • 20
13

I use macOS and used to have the same problem.
Running the following command in the terminal saved me:

conda install -c conda-forge keras tensorflow

Hope it helps.

Kayvan Mazaheri
  • 1,900
  • 18
  • 33
Shawn TIAN
  • 407
  • 4
  • 10
  • I'm on a Windows 10 64bit system with Anaconda 3 installed. This worked perfectly for me. Thanks! EDIT: Apparently works perfectly for Python 3.6, but not with Python 3.7. Since Anaconda 3 installs 3.7 as default, one has to first downgrade Python to 3.6 before installing keras/tensorflow! – kushy Jan 07 '19 at 22:05
3

In case you want to train CNN's with the theano backend like the Keras mnist_cnn.py example:

You better use theano bleeding edge version. Otherwise there may occur assertion errors.

  • Run Theano bleeding edge
    pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
  • Run Keras (like 1.0.8 works fine)
    pip install git+git://github.com/fchollet/keras.git
Randy Welt
  • 140
  • 8
0

install by this command given below conda install -c conda-forge keras

this is error "CondaError: Cannot link a source that does not exist" ive get in win 10. for your error put this command in your command line.

conda update conda

this work for me .

Rudresh Mehta
  • 89
  • 1
  • 11
0

In windows environment with Anconda. Go to anconda prompt from start. Then if you are behind proxy then .copndarc file needs to eb updated with the proxy details.

ssl_verify: false channels: - defaults proxy_servers: http: http://xx.xx.xx.xx:xxxx https: https://xx.xx.xx.xx:xxxx

I had ssl_verify initially marked as 'True' then I was getting ssl error. So i turned it to false as above and then ran the below commands

conda update conda conda update --all conda install --channel https://conda.anaconda.org/conda-forge keras conda install --channel https://conda.anaconda.org/conda-forge tensorflow

My python version is 3.6.7

AKumar
  • 11
  • 3
0

Anaconda with Windows

  • Run anaconda prompt with administrator privilages
  • conda update conda
  • conda update --all
  • conda install mingw libpython
  • conda install theano

After conda commands it's required to accept process - Proceed ([y]/n)?

ElConrado
  • 1,151
  • 1
  • 13
  • 32