3

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.

3
  • What's the output of import langdetect and then langdetect.detect()? Commented Sep 22, 2017 at 14:55
  • the output is "AttributeError: module 'langdetect' has no attribute 'detect'" I use github.com/Mimino666/langdetect#basic-usage as a tutorial. Commented Sep 22, 2017 at 14:58
  • Then I'd say your detect function doesn't exist with that name... Commented Sep 22, 2017 at 14:59

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I just installed it myself and you're correct with the use so maybe there's something wrong with your installation. What version of python and the package do you have installed? You can always try to uninstall it and install it again
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.