question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportError: cannot import name 'format_exception_only'

See original GitHub issue

An error occurred when I import nltk. Please tell me what I should do, thanks.

>>> import nltk
Traceback (most recent call last):
  File "/usr/lib/python3.5/subprocess.py", line 441, in <module>
    import threading
  File "/usr/lib/python3.5/threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
  File "/usr/lib/python3.5/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.5/linecache.py", line 11, in <module>
    import tokenize
  File "/home/july/PythonProjects/test/tokenize.py", line 3, in <module>
    from nltk.tokenize import regexp_tokenize, wordpunct_tokenize, blankline_tokenize
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/__init__.py", line 67, in <module>
    from nltk.tokenize.mwe      import MWETokenizer
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/mwe.py", line 31, in <module>
    from nltk.util import Trie
  File "/usr/local/lib/python3.5/dist-packages/nltk/util.py", line 15, in <module>
    import pydoc
  File "/usr/lib/python3.5/pydoc.py", line 76, in <module>
    from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/nltk/__init__.py", line 89, in <module>
    from nltk.internals import config_java
  File "/usr/local/lib/python3.5/dist-packages/nltk/internals.py", line 11, in <module>
    import subprocess
  File "/usr/lib/python3.5/subprocess.py", line 443, in <module>
    import dummy_threading as threading
  File "/usr/lib/python3.5/dummy_threading.py", line 45, in <module>
    import threading
  File "/usr/lib/python3.5/threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
  File "/usr/lib/python3.5/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.5/linecache.py", line 11, in <module>
    import tokenize
  File "/home/july/PythonProjects/test/tokenize.py", line 3, in <module>
    from nltk.tokenize import regexp_tokenize, wordpunct_tokenize, blankline_tokenize
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/__init__.py", line 67, in <module>
    from nltk.tokenize.mwe      import MWETokenizer
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/mwe.py", line 31, in <module>
    from nltk.util import Trie
  File "/usr/local/lib/python3.5/dist-packages/nltk/util.py", line 15, in <module>
    import pydoc
  File "/usr/lib/python3.5/pydoc.py", line 76, in <module>
    from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3.5/subprocess.py", line 441, in <module>
    import threading
  File "/usr/lib/python3.5/threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
ImportError: cannot import name 'format_exc'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
    import re, traceback
  File "/usr/lib/python3.5/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.5/linecache.py", line 11, in <module>
    import tokenize
  File "/home/july/PythonProjects/test/tokenize.py", line 3, in <module>
    from nltk.tokenize import regexp_tokenize, wordpunct_tokenize, blankline_tokenize
  File "/usr/local/lib/python3.5/dist-packages/nltk/__init__.py", line 89, in <module>
    from nltk.internals import config_java
  File "/usr/local/lib/python3.5/dist-packages/nltk/internals.py", line 11, in <module>
    import subprocess
  File "/usr/lib/python3.5/subprocess.py", line 443, in <module>
    import dummy_threading as threading
  File "/usr/lib/python3.5/dummy_threading.py", line 45, in <module>
    import threading
  File "/usr/lib/python3.5/threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
ImportError: cannot import name 'format_exc'

Original exception was:
Traceback (most recent call last):
  File "/usr/lib/python3.5/subprocess.py", line 441, in <module>
    import threading
  File "/usr/lib/python3.5/threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
  File "/usr/lib/python3.5/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.5/linecache.py", line 11, in <module>
    import tokenize
  File "/home/july/PythonProjects/test/tokenize.py", line 3, in <module>
    from nltk.tokenize import regexp_tokenize, wordpunct_tokenize, blankline_tokenize
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/__init__.py", line 67, in <module>
    from nltk.tokenize.mwe      import MWETokenizer
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/mwe.py", line 31, in <module>
    from nltk.util import Trie
  File "/usr/local/lib/python3.5/dist-packages/nltk/util.py", line 15, in <module>
    import pydoc
  File "/usr/lib/python3.5/pydoc.py", line 76, in <module>
    from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/nltk/__init__.py", line 89, in <module>
    from nltk.internals import config_java
  File "/usr/local/lib/python3.5/dist-packages/nltk/internals.py", line 11, in <module>
    import subprocess
  File "/usr/lib/python3.5/subprocess.py", line 443, in <module>
    import dummy_threading as threading
  File "/usr/lib/python3.5/dummy_threading.py", line 45, in <module>
    import threading
  File "/usr/lib/python3.5/threading.py", line 7, in <module>
    from traceback import format_exc as _format_exc
  File "/usr/lib/python3.5/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.5/linecache.py", line 11, in <module>
    import tokenize
  File "/home/july/PythonProjects/test/tokenize.py", line 3, in <module>
    from nltk.tokenize import regexp_tokenize, wordpunct_tokenize, blankline_tokenize
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/__init__.py", line 67, in <module>
    from nltk.tokenize.mwe      import MWETokenizer
  File "/usr/local/lib/python3.5/dist-packages/nltk/tokenize/mwe.py", line 31, in <module>
    from nltk.util import Trie
  File "/usr/local/lib/python3.5/dist-packages/nltk/util.py", line 15, in <module>
    import pydoc
  File "/usr/lib/python3.5/pydoc.py", line 76, in <module>
    from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

28reactions
Prudenceyyxcommented, Nov 6, 2017

File “/home/july/PythonProjects/test/tokenize.py”, line 3, in <module> from nltk.tokenize import regexp_tokenize, wordpunct_tokenize, blankline_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.

2reactions
randidwiputracommented, Mar 21, 2018

My script name is tokenize.py too and error when running. Thanks dude

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot import name 'format_exception_only' in python - Stack ...
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 ...
Read more >
How to Fix ImportError: Cannot Import Name in Python - Rollbar
The Python ImportError: cannot import name error occurs when an imported class is not accessible or is in a circular dependency.
Read more >
How to Fix : “ImportError: Cannot import name X” in Python?
You can solve the “ ImportError : Cannot import name X” Error by resolving the circular dependencies. You can do that either by...
Read more >
How do I fix the ImportError: cannot import name 'delayed'?
I am using the LORAS package from pyloras for imbalanced learning, but it requires importing the delayed package. I used the pip.main() to ......
Read more >
How to Fix : “ImportError: Cannot import name X” in Python
In Python "ImportError: cannot import name" error generally occurs when the imported class is not accessible, or the imported class is in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found