how to fix ImportError: cannot import name 'fo

2019-09-19 15:18发布

I am using pycharm community version for python 3.6.1 as I'm beginner when i try to run simple tokenize program -

import nltk
from nltk.tokenize import word_tokenize
word_tokenize(" hellow world im nltk")

I get the following error -

C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\python.exe E:/test/token.py
Traceback (most recent call last):
  File "E:/test/token.py", line 1, in <module>
    import nltk
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\__init__.py", line 89, in <module>
    from nltk.internals import config_java
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\internals.py", line 11, in <module>
    import subprocess
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 126, in <module>
    import threading
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\traceback.py", line 5, in <module>
    import linecache
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\linecache.py", line 11, in <module>
    import tokenize
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\tokenize.py", line 35, in <module>
    from token import *
  File "E:\test\token.py", line 2, in <module>
    from nltk.tokenize import word_tokenize
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\tokenize\__init__.py", line 64, in <module>
    from nltk.tokenize.mwe      import MWETokenizer
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\tokenize\mwe.py", line 31, in <module>
    from nltk.util import Trie
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\util.py", line 13, in <module>
    import pydoc
  File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\pydoc.py", line 72, in <module>
    from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'

Process finished with exit code 1

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-19 15:26

File "E:\test\token.py", line 2, in from nltk.tokenize import word_tokenize

There happen to be another script with the same name Python is looking for. Python got confused about the script from package and your own script because it start searching from the folder where you are. So just change your own script to another name.

查看更多
神经病院院长
3楼-- · 2019-09-19 15:32

Go to command prompt(not necessarily command prompt. You can do it from IDLE also). Do an nltk.download() in the python interpreter and then download all-corpora and book. After you do this, run your program. It should work.

查看更多
登录 后发表回答