1

I'm new to APIs and am following the exact instructions of every website explaining how one is supposed to get help of the APIs in Python using the requests module. However, no matter what API link I define (of course I went for many free API samples just to test if they work), I get this error: ImportError: cannot import name 'b64encode' from 'base64' I have no idea what the problem is, or what I'm not getting right. Here's my snippet, just as a sample:

import requests

words = 30
paragraphs = 1
formats = 'text'

response = requests.get(f"https://v2.jokeapi.dev/")
print(response.text)

The complete error I get is like so:

Traceback (most recent call last):
  File "/home/liana/Desktop/testing.py", line 1, in <module>
    import requests
  File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 11, in <module>
    from . import exceptions
  File "/usr/lib/python3/dist-packages/urllib3/exceptions.py", line 3, in <module>
    from six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "/usr/lib/python3/dist-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/lib/python3/dist-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/lib/python3/dist-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/lib/python3.9/http/client.py", line 71, in <module>
    import email.parser
  File "/usr/lib/python3.9/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/usr/lib/python3.9/email/feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "/usr/lib/python3.9/email/_policybase.py", line 7, in <module>
    from email import header
  File "/usr/lib/python3.9/email/header.py", line 17, in <module>
    import email.base64mime
  File "/usr/lib/python3.9/email/base64mime.py", line 37, in <module>
    from base64 import b64encode
ImportError: cannot import name 'b64encode' from 'base64' (/home/liana/Desktop/base64.py)
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in <module>
    from urllib.request import urlopen
File "/usr/lib/python3.9/urllib/request.py", line 88, in <module>
    import http.client
  File "/usr/lib/python3.9/http/client.py", line 71, in <module>
    import email.parser
  File "/usr/lib/python3.9/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/usr/lib/python3.9/email/feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "/usr/lib/python3.9/email/_policybase.py", line 7, in <module>
    from email import header
  File "/usr/lib/python3.9/email/header.py", line 17, in <module>
    import email.base64mime
  File "/usr/lib/python3.9/email/base64mime.py", line 37, in <module>
    from base64 import b64encode
ImportError: cannot import name 'b64encode' from 'base64' (/home/liana/Desktop/base64.py)
Original exception was:
Traceback (most recent call last):
  File "/home/liana/Desktop/testing.py", line 1, in <module>
    import requests
  File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 11, in <module>
    from . import exceptions
  File "/usr/lib/python3/dist-packages/urllib3/exceptions.py", line 3, in <module>
    from six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "/usr/lib/python3/dist-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/lib/python3/dist-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/lib/python3/dist-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/lib/python3.9/http/client.py", line 71, in <module>
    import email.parser
  File "/usr/lib/python3.9/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/usr/lib/python3.9/email/feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "/usr/lib/python3.9/email/_policybase.py", line 7, in <module>
    from email import header
  File "/usr/lib/python3.9/email/header.py", line 17, in <module>
    import email.base64mime
  File "/usr/lib/python3.9/email/base64mime.py", line 37, in <module>
    from base64 import b64encode
ImportError: cannot import name 'b64encode' from 'base64' (/home/liana/Desktop/base64.py)

Any help is appreciated.

Melika
  • 99
  • 7
  • looks like a missing library dependency on your end – Mad Physicist Nov 02 '21 at 19:30
  • @MadPhysicist How can I find out what's missing? – Melika Nov 02 '21 at 19:32
  • Read the error message. Post a traceback. Make sure it's from running this exact snippet. – Mad Physicist Nov 02 '21 at 19:32
  • @MadPhysicist Just updated. – Melika Nov 02 '21 at 19:35
  • 1
    What is `/home/liana/Desktop/base64.py`? It looks like one of yours. – Mad Physicist Nov 02 '21 at 19:38
  • @MadPhysicist That's the full path of the file in which the python snippet is saved. – Melika Nov 02 '21 at 19:40
  • Can you reproduce this for a public API that does not require a key? Something I can copy-and-paste to reproduce on my end? – Mad Physicist Nov 02 '21 at 19:42
  • @MadPhysicist ```https://v2.jokeapi.dev/``` There's this one, which is a public API and displays random jokes. I get the same long list of error though. – Melika Nov 02 '21 at 19:50
  • Could you update your MCVE? I want to reproduce the same error just by copying your snippet into my editor. – Mad Physicist Nov 02 '21 at 19:52
  • 1
    Wait. What's `testing` vs `base64`? Try renaming `base64` to something else. It looks like it's picking up the wrong path entry because of the name conflict. – Mad Physicist Nov 02 '21 at 19:53
  • @MadPhysicist Updated – Melika Nov 02 '21 at 19:55
  • If I copy your code into my REPL, it works fine. I suspect you have a module name conflict. – Mad Physicist Nov 02 '21 at 19:56
  • @MadPhysicist I didn't name anything "base64", I have no idea where that's coming from, and the error occurs no matter with what link. I don't even have anything related to encoding or base64 in my snippet :( – Melika Nov 02 '21 at 19:56
  • @MadPhysicist That's so weird :( – Melika Nov 02 '21 at 19:57
  • 2
    So your folder contains no extra files? The error message pretty clearly says `/home/liana/Desktop/base64.py`... – Mad Physicist Nov 02 '21 at 20:20
  • Does this answer your question? [Getting an ImportError: cannot import name 'Random' from 'random'](https://stackoverflow.com/questions/64821493/getting-an-importerror-cannot-import-name-random-from-random-only-while-run) – wjandrea Nov 03 '21 at 00:58
  • @MadPhysicist Wow I just checked. You are right, there is a file in the same path called like that, and it for sure is not my code, I might have downloaded it online somewhen I couldn't remember, and never noticed it is there anymore. Just deleted that base64.py weird file and now everything is working! All thanks to you :))))) – Melika Nov 03 '21 at 06:11
  • @wjandrea nice find. Too bad I used up my close vote before, when the question was a lot less informative – Mad Physicist Nov 03 '21 at 12:09

1 Answers1

2

If you look carefully at the traceback, you will see that it starts with your script, "/home/liana/Desktop/testing.py", goes through a chain of imports, and ends up crashing trying to import b64encode from base64.py.

The file /home/liana/Desktop/base64.py is something you either created or downloaded. Since it's in the current directory, it supersedes the real base64.py that "/usr/lib/python3.9/email/base64mime.py" is looking for. The file in your current directory clearly does not define the name b64encode, which is the immediate cause of the error.

To fix the problem, make sure the names of the files in your current directory don't conflict with the names of other top level modules. So either rename it delete /home/liana/Desktop/base64.py.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264