How to fix Python 3 AttributeError: 'function' object has no attribute 'urlsplit'

Problem:

When trying to urlsplit an URL in Python 3:

urlsplit_misuse_example.py
from urllib.parse import urlparse

path = urlparse.urlsplit(remote_image_url).path
urlsplit_misuse_example.py
from urllib.parse import urlparse

path = urlparse.urlsplit(remote_image_url).path

you see the following error message:

example.txt
AttributeError                            Traceback (most recent call last)
Input In [30], in <cell line: 3>()
      1 from urllib.parse import urlparse
----> 3 path = urlparse.urlsplit(remote_image_url).path
      4 filename = posixpath.basename(path)

AttributeError: 'function' object has no attribute 'urlsplit'
traceback.txt
AttributeError                            Traceback (most recent call last)
Input In [30], in <cell line: 3>()
    1 from urllib.parse import urlparse
----> 3 path = urlparse.urlsplit(remote_image_url).path
    4 filename = posixpath.basename(path)

AttributeError: 'function' object has no attribute 'urlsplit'

Solution

The equivalent of urlparse.urlsplit() in Python 3 is urllib.parse.urlsplit().

Therefore, a working code example is

example.py
from urllib.parse import urlsplit

path = urlsplit(remote_image_url).path
urlsplit_fix_example.py
from urllib.parse import urlsplit

path = urlsplit(remote_image_url).path

 


Check out similar posts by category: Python