0

I am trying to import imaplib in the Python interpreter and getting the following errors related to nltk.

>>> import imaplib

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File 
"C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\imaplib.py", 
line 25, in <module>
import binascii, errno, random, re, socket, subprocess, sys, time, calendar
File
C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\subprocess.py", 
line 427, in<module>
import threading
File 
"C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\threading.py", 
line 7, in <module>
from traceback import format_exc as _format_exc
File 
"C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\traceback.py", 
line 5, in <module>
import linecache
File 
"C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\linecache.py", 
line 11, in <module>
import tokenize
File "C:\Users\nikhsin2\python\tokenize.py", line 1, in <module>
import nltk
File "C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\site-
packages\nltk\__init__.py", line 114, in <module>
from nltk.collocations import *
File "C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\site-
packages\nltk\collocations.py", line 38, in <module>
from nltk.util import ngrams
File "C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\site-
packages\nltk\util.py", line 15, in <module>
import pydoc
File 
"C:\Users\nikhsin2\AppData\Local\Programs\Python\Python35\lib\pydoc.py", 
line 72, in <module>
from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'

I am trying to create a desktop app for doing some mail related operations. Browsed a lot but didn't find any solution for this. The nltk package is already installed.

Nikhil
  • 65
  • 1
  • 8
  • It's advisable to use anaconda to install `nltk` when on a Windows machine. Please see https://gist.github.com/alvations/0ed8641d7d2e1941b9f9 for a detailed walkthrough. – alvas Jul 31 '17 at 05:47
  • the anaconda also shows the same error File "C:\Users\nikhsin2\AppData\Local\Continuum\Anaconda3\lib\site- packages\nltk\util.py", line 15, in import pydoc File C:\Users\nikhsin2\AppData\Local\Continuum\Anaconda3\lib\pydoc.py", line 72, in from traceback import format_exception_only ImportError: cannot import name 'format_exception_only' – Nikhil Jul 31 '17 at 07:34
  • Can you show how you install conda/pip and how you install `nltk` and `imaplib` ? – alvas Jul 31 '17 at 07:37
  • Can you also do a `pip install -U pydoc` before installing `nltk? – alvas Jul 31 '17 at 07:38
  • It looks like the same problem as https://stackoverflow.com/questions/44030158/how-to-fix-importerror-cannot-import-name-format-exception-only-in-python . – alvas Jul 31 '17 at 07:39
  • Hi alvas went through the links you've provided. The pydoc.py is already installed as the line 72 in pydoc was throwing error unable to import the format_exception only: – Nikhil Jul 31 '17 at 10:51
  • in pydoc line 72: from traceback import format_exception_only. Checked the traceback, the format_exception_only is in __all__ also: __all__ = ['extract_stack', 'extract_tb', 'format_exception', 'format_exception_only', 'format_list', 'format_stack', 'format_tb', 'print_exc', 'format_exc', 'print_exception', 'print_last', 'print_stack', 'print_tb', 'clear_frames', 'FrameSummary', 'StackSummary', 'TracebackException', 'walk_stack', 'walk_tb']. – Nikhil Jul 31 '17 at 10:53

1 Answers1

1

Python's imaplib doesn't depend on the nltk. But it looks like you have a script called tokenize.py in your directory, which is imported instead of an expected dependency: Look carefully at the paths in the trace you included.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • yes alexis I found a script which I created as tokenize.py which was misunderstood for the nltk's tokenize.py. Thank you so much for pointing this out. – Nikhil Jul 31 '17 at 18:12