【问题标题】:python.cannot import name 'TextBlob'python.无法导入名称'TextBlob'
【发布时间】:2015-06-10 17:04:57
【问题描述】:

我试图将 TextBlob 导入 Python。

当我直接在 shell 中运行命令时,它运行良好: 从文本块导入文本块

但是,当我将它放入 py 文件并运行它时,它不再起作用了,它说:

ImportError: cannot import name 'TextBlob'

请帮帮我,现在很绝望......非常感谢

【问题讨论】:

  • 在执行您的 .py 文件时,您是否检查过它是通过与调用 Python IDLE 或 iPython 时相同的 python 安装完成的,您可以在其中实际导入它?

标签: python textblob


【解决方案1】:

我遇到了同样的问题,通过删除同一文件夹中所有以 test*, text* 开头的文件解决了。

【讨论】:

  • 我的问题是我将文件命名为 textblob.py。重命名它解决了这个问题。
【解决方案2】:

1:导入应该是:from textblob import TextBlob(Python区分大小写,所以导入TextBlob时务必要大写T&B)

2: textblob 应该这样安装: Python2:

$ pip install -U textblob

$ python -m textblob.download_corpora

Python3:

$ pip3 install -U textblob

$ python3 -m textblob.download_corpora

【讨论】:

  • 我必须执行上述操作,我的 python 文件被命名为 textblob.py,我必须重命名它才能使其工作。
【解决方案3】:

确保您没有任何名为 text.py 的文件或文件夹。 如果您的任何文件或文件夹也不起作用

【讨论】:

    【解决方案4】:

    我认为您的项目解释器和控制台的解释器可能不同。确保它们相同。

    确保在您的项目解释器中安装了 TextBlob。

    【讨论】:

      【解决方案5】:

      如果你在 windows 上使用 Pycharms,那么你需要以管理员身份打开 pycharms。

      如果你在 Linux 上安装它,那么 sudo 安装包。

      【讨论】:

        【解决方案6】:
        pip install textblob 
        

        在 jupyter 笔记本上

        【讨论】:

          【解决方案7】:

          我已经通过 pip 使用以下命令安装了 textblog:

          sudo pip install textblob
          

          我已经用命令下载了语料库:

          python -m textblob.download_corpora
          

          这适用于 textblob 网站的示例。

          这是我使用命令运行的示例:./test.py

          就在我使用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();
          

          【讨论】:

          • 我也按照所有说明进行操作,它在另一台计算机上运行良好,但在我的计算机上却无法运行......不知道为什么
          • 你的PYTHONPATH 是什么?写:回声 $PYTHONPATH 。也许你的 python 版本有冲突。这就是为什么我更喜欢用 python-pip 安装 Python 包
          猜你喜欢
          • 1970-01-01
          • 2016-10-08
          • 2016-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多