15

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'

il_raffa
  • 5,090
  • 129
  • 31
  • 36
Arun
  • 167
  • 1
  • 1
  • 5

6 Answers6

23

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.

Hagbard
  • 3,430
  • 5
  • 28
  • 64
3

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.

Tomáš Cerha
  • 309
  • 1
  • 7
  • Thanks but not get clarified. Could you please share exactly the steps to be followed. – Arun Apr 12 '18 at 09:18
  • Sorry, not really :-(. The exact steps depend heavilly on your environment and as it reveals from forther comments, this is Anaconda which I am not familiar with. The above, however, should still help you to understand your problem. Inspect the differences in sys.path in the anaconda cmd prompt and the anaconda spyder. – Tomáš Cerha Apr 12 '18 at 12:43
3

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.

Fuevo
  • 132
  • 1
  • 10
  • Installed already using anacond command prompt. Please find the cmd prompt responses below. (tensorflow) C:\Users>pip install keras_contrib Requirement already satisfied: keras_contrib in c:\users\app data\local\continuum\anaconda3\envs\tensorflow\lib\site-packages\keras_contrib-2 .0.8-py3.6.egg Requirement already satisfied: keras in c:\users\appdata\loc al\continuum\anaconda3\envs\tensorflow\lib\site-packages (from keras_contrib) Requirement already satisfied: numpy>=1.9.1 in c:\users\arunkumar.natarajan\appd ata\local\continuum\anaconda3\envs\tensorflow\lib\site-pac – Arun Apr 12 '18 at 09:35
  • Thank you. Its working in the anaconda cmd prompt but not in the anaconda spyder. Yet to arrive the solution for this. – Arun Apr 12 '18 at 10:17
  • Could you please add more description to your issue, like what exactly the prompt looks like and more details. – Fuevo Apr 12 '18 at 10:21
  • Installed keras_contrib folder in c:\users path and the tensorflow_windows in path C:\Users\Continuum\anaconda3\envs\tensorflow_windows. I am running python from c:\users\keras_contrib path for the command from keras_contrib.layers import CRF. But in spyder different working directory – Arun Apr 12 '18 at 10:27
  • @Arun I made a very clear about package installation under conda environment, pls check out, it might very possibly solve your problem – Fuevo Apr 15 '18 at 14:51
1

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')

Prady_gupta
  • 61
  • 1
  • 9
1

Simply run:

conda install git+https://www.github.com/keras-team/keras-contrib.git
mece1390
  • 161
  • 2
  • 13
  • That command did not work in my case. I installed it successfully when I opened a command prompt at `\anaconda3\Scripts`, then I did run the command `pip install git+https://www.github.com/keras-team/keras-contrib.git` – Vladimir S. May 24 '21 at 05:27
0

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.

Esraa Abdelmaksoud
  • 1,307
  • 12
  • 25