Import Errors

585 views
Skip to first unread message

Annaliese Paulson

unread,
Feb 21, 2022, 2:52:06 PM2/21/22
to Gensim
I just created a clean Anaconda environment and used pip to install gensim. If I import gensim, I get an ImportError and the below traceback. If I import transformers, I get a similar ImportError but the traceback refers to failing to import gensim. Anyone have any idea how to fix this?

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/site-packages/gensim/__init__.py", line 9, in <module>

    import logging

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/logging/__init__.py", line 26, in <module>

    import sys, os, time, io, re, traceback, warnings, weakref, collections.abc

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/traceback.py", line 5, in <module>

    import linecache

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/linecache.py", line 11, in <module>

    import tokenize

  File "/Users/annaliesepaulson/tokenize.py", line 8, in <module>

    from transformers import AutoTokenizer, AutoModelForQuestionAnswering

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/site-packages/transformers/__init__.py", line 30, in <module>

    from . import dependency_versions_check

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/site-packages/transformers/dependency_versions_check.py", line 36, in <module>

    from .file_utils import is_tokenizers_available

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/site-packages/transformers/file_utils.py", line 49, in <module>

    import requests

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>

    import urllib3

  File "/Users/annaliesepaulson/opt/anaconda3/envs/test/lib/python3.9/site-packages/urllib3/__init__.py", line 9, in <module>

    from logging import NullHandler

ImportError: cannot import name 'NullHandler' from partially initialized module 'logging' (most likely due to a circular import)

Gordon Mohr

unread,
Feb 21, 2022, 4:34:15 PM2/21/22
to Gensim
Your stack shows the import of `gensim` starting to import standard-library package `logging`, which starts-to-import standard-library package `traceback`, which starts-to-import standard-library package `linecache`. All good so far.

But then that Python standard-library package tries to import the standard-library `tokenize` - and it finds your local `tokenize.py` instead. That starts a chain of imports which eventually reaches a `logging`-related import again – even though the original attempted import of `logging` is still in progress.

You've inadvertently created a circular dependency, by having a local file named the same as a Python standard-library package. Renaming your local file should help. 

Separately, though it's not part of the issue here: I believe a standard `anaconda` environment already has a ton of packages installed, which can increase the risk of conflicts, or obscure which non-standard packages are actually required. I usually to start with a minimal `conda` environment instead, as from the `miniconda` tool, instead. It may then take a few more explicit steps to get all relevant packages installed – but I'm certain I know & control each installation, then.

- Gordon

Annaliese Paulson

unread,
Feb 21, 2022, 6:21:21 PM2/21/22
to gen...@googlegroups.com
Thank you for such a quick and helpful response! You are right, there was a tokenize.py file in the directory I was working in and removing it fixed the error.

Best,

--
You received this message because you are subscribed to a topic in the Google Groups "Gensim" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gensim/PE9oh8b-ltQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gensim+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gensim/30c91414-103f-4df3-911a-b19448d7a0f2n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages