Python ImportError: cannot import name utils

Question:

I’m having this issue running a script and it looks like it missed some dependencies, but as you can see below. After installing the missing libraries, it doesn’t make any sense.

[ericfoss@maverick-fossum-ddns-net packages]$ python -c "import utils"
[ericfoss@maverick-fossum-ddns-net packages]$ python -c "import requests"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 64, in <module>
    from . import utils
ImportError: cannot import name utils
[ericfoss@maverick-fossum-ddns-net packages]$ 

Any idea why utils can be imported, but requests can’t?

Asked By: Eric Fossum

||

Answers:

utils package is not installed

You can install package using

sudo pip install utils
Answered By: Hafees Kazhunkil

Well, after pip uninstall requests and reinstalling, it would no longer work. Luckily, `pip install python-requests’ fixed the whole thing…

Answered By: Eric Fossum

Check if Requests requirements are satisfied:

$ pip show requests
...
Requires: certifi, idna, chardet, urllib3

I hit the same error but I was missing idna. After installing it the issue resolved.

Answered By: Eino Malinen

We may see the unable to import utils error in multiple contexts.
I got this error message when I was migrating scripts from python 2 to 3.
I used the inbuilt python migration automated tool to change the file that is causing the import error using the command 2to3 -w filename
This has resolved the error because the import utils is not back supported by python 3 and we have to convert that code to python 3.

Answered By: Sumanth Vakacharla

I ran into a similar problem when running Jupyter Lab:

$ jupyter-lab --ip 0.0.0.0
Traceback (most recent call last):
  File "/Users/gtholpadi/opt/anaconda3/bin/jupyter-lab", line 6, in <module>
    from jupyterlab.labapp import main
  File "/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/jupyterlab/labapp.py", line 14, in <module>
    from jupyterlab_server import slugify, WORKSPACE_EXTENSION
  File "/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/jupyterlab_server/__init__.py", line 4, in <module>
    from .app import LabServerApp
  File "/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/jupyterlab_server/app.py", line 10, in <module>
    from .handlers import add_handlers, LabConfig
  File "/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/jupyterlab_server/handlers.py", line 18, in <module>
    from .listings_handler import ListingsHandler, fetch_listings
  File "/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/jupyterlab_server/listings_handler.py", line 17, in <module>
    import requests
  File "/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/requests/__init__.py", line 120, in <module>
    from . import utils
ImportError: cannot import name 'utils' from partially initialized module 'requests' (most likely due to a circular import) (/Users/gtholpadi/opt/anaconda3/lib/python3.8/site-packages/requests/__init__.py)

requests was already installed when I got this error. I tried pip install -U requests and that solved the problem.

Answered By: Goutham Tholpadi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.