Jak opravit Python 3 AttributeError: 'function' object has no attribute 'urlsplit'

Problém:

Při pokusu urlsplit URL v 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

vidíte následující chybové hlášení:

urlsplit_attributeerror.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'

Řešení

Ekvivalent urlparse.urlsplit() v Python 3 je urllib.parse.urlsplit().

Proto, funkční příklad kódu je

urlsplit_fix_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

Podívejte se na podobné články podle kategorie: Python