Python ImportError: cannot import name utils

2020-03-03 06:53发布

问题:

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?

回答1:

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.



回答2:

Well, after pip uninstall requests and reinstalling, it no longer would work at all. Luckily, dnf install python-requests fixed the whole thing...



回答3:

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.



回答4:

If you have installed required module and still getting same error. Please restart your terminal window. Make sure you save your previous work. I had same error while importing nlpnet -

ImportError: cannot import name utils

first install these modules from cmd as- python -m pip install name utils

then restart python terminal. This worked for me.



回答5:

utils package is not installed

You can install package using

sudo pip install utils