python.cannot import name 'TextBlob'

18.8k views Asked by At

I was trying to import TextBlob into Python.

When I run the command directly inside shell, it works pretty well: from textblob import TextBlob

However, when I put it into the py file and run it, it doesn't work anymore, it says:

ImportError: cannot import name 'TextBlob'

Please help me out with it, so desperate right now...Thanks a lot

7

There are 7 answers

2
Eric D. On

I have install textblog by pip with the command :

sudo pip install textblob

And i have download the corpus with the command :

python -m textblob.download_corpora

And that works fine with the example of textblob website.

Here is the example that i run with the command : ./test.py

just before i use chmod +755 test.py


#!/usr/bin/env python

def test():
    text = '''

    The titular threat of The Blob has always struck me as the ultimate movie
    monster: an insatiably hungry, amoeba-like mass able to penetrate
    virtually any safeguard, capable of--as a doomed doctor chillingly
    describes it--"assimilating flesh on contact.
    Snide comparisons to gelatin be damned, it's a concept with the most
    devastating of potential consequences, not unlike the grey goo scenario
    proposed by technological theorists fearful of
    artificial intelligence run rampant.
    '''

    blob = TextBlob(text)
    blob.tags           # [('The', 'DT'), ('titular', 'JJ'),
                        #  ('threat', 'NN'), ('of', 'IN'), ...]

    blob.noun_phrases   # WordList(['titular threat', 'blob',
                        #            'ultimate movie monster',
                        #            'amoeba-like mass', ...])

    for sentence in blob.sentences:
        print(sentence.sentiment.polarity)
    # 0.060
    # -0.341

    blob.translate(to="es")  # 'La amenaza titular de The Blob...'

if __name__ == "__main__":
    from textblob import TextBlob
    test();
1
Fenglin Han On

I encountered the same problem, solved it by deleting any file starting with test*, text* in the same folder.

0
sanjay khatwani On

I think your project interpreter and the interpreter for the console, could be different. Make sure they are same.

Make sure TextBlob is installed in your project interpreter.

0
Krishna On

If you are using Pycharms on windows then you need to open pycharms as an administrator.

If you are installing it on Linux then sudo install the packages.

1
Sanjay On

1: The import should be: from textblob import TextBlob (Python is case sensitive, so it's important to import TextBlob with with capital T & B)

2: textblob should be installed like this : Python2:

$ pip install -U textblob

$ python -m textblob.download_corpora

Python3:

$ pip3 install -U textblob

$ python3 -m textblob.download_corpora

0
Spandan Sarkar On
pip install textblob 

on jupyter notebook

0
Fadil Olamyy Wahab On

Make sure you do not have any file or folder named text.py. It also won't works if any file or folder of your