13
votes

I am trying to import Keras lib code to execute CRF using the import command below but an error raises as titled. Please share the solution for this.

The command used to execute is

from keras_contrib.layers import CRF

Traceback (most recent call last):

File "", line 1, in from keras_contrib.layers import CRF

ImportError: No module named 'keras_contrib'

6
Have you installed it using pip install git+https://www.github.com/keras-team/keras-contrib.git?FlyingTeller
Instead I used pip install keras in the anaconda command prompt. but still the issue persists.Arun

6 Answers

19
votes

A simple

(sudo) pip install git+https://www.github.com/keras-team/keras-contrib.git

as mentioned in the installation instructions did the trick for me.

3
votes

This error means that Python is unable to find the module in one of the directories defined by Python path. The module is either not installed or is installed in another directory.

If not installed, then see https://github.com/keras-team/keras-contrib for installation instructions.

If installed but not found, you will most likely need to add the directory where it is installed to your Python path. You can find out what your current Python path is by inspecting the variable sys.path (such as python -c 'import sys; print sys.path'). You may need to add another directory to your path by setting the environment variable PYTHONPATH before running your script, but there are other options. See for example PYTHONPATH vs. sys.path for some insight.

3
votes

After struggling for a while, I was so willing to make myself clear of this issue, so I searched for a while, and just figured out and tested.

When you create a new conda env by specifying the python version, it will use the conda_root_python version. And if you didn't install the pip package, and try to use pip under your created conda env, it will only run the conda_root_pip and install the package in the root site_packages.

I know three ways to install python packages only in your created conda env. For a better explanation, we create a conda env with same python version of conda root environment.

conda create -n myenv python

I. One of the officials advise, install package with conda command for specified conda environment,

conda install -n myenv tensorflow

II. Another one of official advise, get into your specified environment and run conda install

source activate myenv
conda install tensorflow

in above two ways you don't need to install extra packages like pip and other pip related packages.

III. For people who really want to pip, just because get used of that. install pip package(just as above two ways did).

conda install -n myenv pip

or

source active myenv
conda install pip

then comes the pip install when you are in your environment

pip install tensorflow

--------new edit above 15.April.2018--------------

Just to make it more clear.

If you are working under anaconda environment, you should also install all the modules and IDE you need in that environment.

Here I just put one example of anaconda env flows:

conda create --name=my_conda_env python=2.7  #create an environment

activate my_conda_env #get into that env
pip install numpy     #install packages you need
...
pip install keras_contrib
pip install spyder   #install IDE

Getting Started with conda

---------

Try install in root

  1. activate root
  2. pip install keras_conrib
  3. go back to your tensorflow
  4. start your spyder and try again

Maybe this is your issue Module installed on Conda, but gives error on importing in Spyder (Python IDE)

----------------- above new answer

It seems you are under conda environment, env-name is "tensorflow", so try to start python and try import again. To make it clear

  1. make sure you have (tensorflow) in front of C:\Users>
  2. type python to start python
  3. import keras_contrib to see if you have keras_contrib in anaconda env (tensorflow) due your comment, it should be
  4. from keras_conrib.layers import CRF (crf or CRF? just try)

If you installed keras_contrib in env "tensorflow", should also start python and do your jobs in the same env, for a new env, you have to install it again.

Here is something for newbie just like me after playing with python for a while and still not familiar with anaconda, I hope you didn't come up with that. As follows:

I used to think in my anaconda env is already in python(actually not yet), so I just type from keras_contrib.layers import CRF when I saw (tensorflow)C:/Users> which is actually wrong

The right way as described up is get into python(step 2.) or ipython or jupyter just for test if you get the package.

--------------------- below is old answer

I think you confused keras with keras_contrib. They are two different modules. try pip install keras_contrib or use other ways to install keras_contrib.

1
votes

Simply run:

conda install git+https://www.github.com/keras-team/keras-contrib.git
0
votes

If you are trying to install the tensorflow-keras version or even the keras version using git cloning and setup.py installing and getting the above error then you might want to add the path of the keras-contrib folder to system path using -

import sys sys.path.append('<remaining_path>/keras_contrib')

0
votes

If you use Tensorflow, you may get over this error by replacing

from keras_contrib.layers import CRF

with

from tensorflow_addons.layers import CRF

If you're still interested in Keras itself, use the following line in Jupyter

!pip install git+https://www.github.com/keras-team/keras-contrib.git

Just note that you may face other errors while using seperate Keras, so I suggest that you use Keras that is supported in Tensorflow.