Extria
Extria

Reputation: 373

Can't use langdetect package in python

I am trying to detect language of the string with langdetect package. But it does not work.

from langdetect import detect

word_string = "Books are for reading"
print(detect(word_string))

If I use code above I get error ImportError: cannot import name detect

When I replace detect with *

from langdetect import *

word_string = "Books are for reading"
print(detect(word_string))

I get error: NameError: name 'detect' is not defined

So my question is how can I solve these problems ?

So the problem was that my langdetect package and python file was with the same name.... Thank you for your answers.

Upvotes: 3

Views: 12274

Answers (3)

Prerna Srivastava
Prerna Srivastava

Reputation: 41

Try installing it first by writing :-> !pip install langdetect on terminal and then import langdetect

Upvotes: 4

Chen A.
Chen A.

Reputation: 11328

Your error indicates the Python interpreter couldn't find the module you are importing in it's sys.path.

Add to your code

import os
sys.path.append('absolute_path to your module.py file')

and try again. Another option is to add your PYTHONPATH environment variable the folder containing your module.

Try import langdetect after validating it's path is in your sys.path variable; if this commands succeeds it means you loaded the module successfully. Now you need to address the detect function as langdetect.detect because it resides in the langdetect namespace. If it doesn't find it - it's not there.

Upvotes: 1

Yorian
Yorian

Reputation: 2062

You can not import it because it is probably not there. See if the name is correct. Seeing other questions here on SO I assume you mean detect_langs.

Upvotes: 1

Related Questions