Skip to main content

An immutable URL class for easy URL-building and manipulation

Project description

A simple, immutable URL class with a clean API for interrogation and manipulation. Supports Pythons 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8 and pypy.

Also supports template URLs as per RFC 6570




From PyPI (stable):

$ pip install purl

From Github (unstable):

$ pip install git+git://



>>> from purl import URL

# String constructor
>>> from_str = URL('')

# Keyword constructor
>>> from_kwargs = URL(scheme='https', host='', path='/search', query='q=testing')

# Combine
>>> from_combo = URL('').path('search').query_param('q', 'testing')

URL objects are immutable - all mutator methods return a new instance.


>>> u = URL('')
>>> u.scheme()
>>> u.domain()
>>> u.username()
>>> u.password()
>>> u.netloc()
>>> u.port()
>>> u.path()
>>> u.query()
>>> u.fragment()
>>> u.path_segment(0)
>>> u.path_segments()
>>> u.query_param('q')
>>> u.query_param('q', as_list=True)
>>> u.query_param('lang', default='GB')
>>> u.query_params()
{'q': ['testing']}
>>> u.has_query_param('q')
>>> u.has_query_params(('q', 'r'))
>>> u.subdomains()
['www', 'google', 'com']
>>> u.subdomain(0)

Note that each accessor method is overloaded to be a mutator method too, similar to the jQuery API. Eg:

>>> u = URL.from_string('')

# Access
>>> u.path_segment(0)

# Mutate (creates a new instance)
>>> new_url = u.path_segment(0, 'tangentlabs')
>>> new_url is u
>>> new_url.path_segment(0)

Hence, you can build a URL up in steps:

>>> u = URL().scheme('http').domain('').path('/some/path').query_param('q', 'search term')
>>> u.as_string()

Along with the above overloaded methods, there is also a add_path_segment method for adding a segment at the end of the current path:

>>> new_url = u.add_path_segment('here')
>>> new_url.as_string()

Couple of other things:

  • Since the URL class is immutable it can be used as a key in a dictionary

  • It can be pickled and restored

  • It supports equality operations

  • It supports equality operations

URL templates can be used either via a Template class:

>>> from purl import Template
>>> tpl = Template("{/list*}")
>>> url = tpl.expand({'list': ['red', 'green', 'blue']})
>>> url.as_string()

or the expand function:

>>> from purl import expand
>>> expand(u"{/list*}", {'list': ['red', 'green', 'blue']})

A wide variety of expansions are possible - refer to the RFC for more details.


v1.6 - 2021-05-15

  • Use pytest insteed of nose.

  • Fix warning around regex string.

v1.5 - 2019-03-10

  • Allow @ in passwords.

v1.4 - 2018-03-11

  • Allow usernames and passwords to be removed from URLs.


  • Ensure paths always have a leading slash.


  • Allow absolute URLs to be converted into relative.


  • Support password-less URLs.

  • Allow slashes to be passed as path segments.


  • Support setting username and password via mutator methods


  • Handle some unicode compatibility edge-cases


  • Fix template expansion bug with no matching variables being passed in. This ensures purl.Template works correctly with the URLs returned from the Github API.


  • Fix bug with special characters in paths not being escaped.


  • Slight tidy up. Document support for PyPy and Python 3.4.


  • Support for RFC 6570 URI templates


  • All internal strings are unicode.

  • Support for unicode chars in path, fragment, query, auth added.


  • Added append_query_param method

  • Added remove_query_param method


  • Added support for Python 3.2/3.3 (thanks @pmcnr and @mitchellrj)


  • Added API docs

  • Added to


  • Modified constructor to accept full URL string as first arg

  • Added add_path_segment method


  • Fixed bug port number in string when using from_string constructor


  • Fixed bug with passing lists to query param setter methods


  • Added support for comparison and equality

  • Added support for pickling

  • Added __slots__ so instances can be used as keys within dictionaries


Clone, create a virtualenv then install purl and the packages required for testing:

$ git clone
$ cd purl
$ mkvirtualenv purl  # requires virtualenvwrapper
(purl) $ make

Ensure tests pass using:

(purl) $ pytest


$ tox

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

purl-1.6.tar.gz (12.2 kB view hashes)

Uploaded Source

Built Distribution

purl-1.6-py2.py3-none-any.whl (10.4 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page