Skip to main content

phonenumbers Python Library

Coverage Status

This is a Python port of Google's libphonenumber library It supports Python 2.5-2.7 and Python 3.x (in the same codebase, with no 2to3 conversion needed).

Original Java code is Copyright (C) 2009-2015 The Libphonenumber Authors.

Release HISTORY, derived from upstream release notes.

Documentation

Installation

Install using pip with:

pip install phonenumbers

Example Usage

The main object that the library deals with is a PhoneNumber object. You can create this from a string representing a phone number using the parse function, but you also need to specify the country that the phone number is being dialled from (unless the number is in E.164 format, which is globally unique).

>>> import phonenumbers
>>> x = phonenumbers.parse("+442083661177", None)
>>> print(x)
Country Code: 44 National Number: 2083661177 Leading Zero: False
>>> type(x)
<class 'phonenumbers.phonenumber.PhoneNumber'>
>>> y = phonenumbers.parse("020 8366 1177", "GB")
>>> print(y)
Country Code: 44 National Number: 2083661177 Leading Zero: False
>>> x == y
True
>>> z = phonenumbers.parse("00 1 650 253 2222", "GB")  # as dialled from GB, not a GB number
>>> print(z)
Country Code: 1 National Number: 6502532222 Leading Zero(s): False

The PhoneNumber object that parse produces typically still needs to be validated, to check whether it's a possible number (e.g. it has the right number of digits) or a valid number (e.g. it's in an assigned exchange).

>>> z = phonenumbers.parse("+120012301", None)
>>> print(z)
Country Code: 1 National Number: 20012301 Leading Zero: False
>>> phonenumbers.is_possible_number(z)  # too few digits for USA
False
>>> phonenumbers.is_valid_number(z)
False
>>> z = phonenumbers.parse("+12001230101", None)
>>> print(z)
Country Code: 1 National Number: 2001230101 Leading Zero: False
>>> phonenumbers.is_possible_number(z)
True
>>> phonenumbers.is_valid_number(z)  # NPA 200 not used
False

The parse function will also fail completely (with a NumberParseException) on inputs that cannot be uniquely parsed, or that can't possibly be phone numbers.

>>> z = phonenumbers.parse("02081234567", None)  # no region, no + => unparseable
Traceback (most recent call last):
  File "phonenumbers/phonenumberutil.py", line 2350, in parse
    "Missing or invalid default region.")
phonenumbers.phonenumberutil.NumberParseException: (0) Missing or invalid default region.
>>> z = phonenumbers.parse("gibberish", None)
Traceback (most recent call last):
  File "phonenumbers/phonenumberutil.py", line 2344, in parse
    "The string supplied did not seem to be a phone number.")
phonenumbers.phonenumberutil.NumberParseException: (1) The string supplied did not seem to be a phone number.

Once you've got a phone number, a common task is to format it in a standardized format. There are a few formats available (under PhoneNumberFormat), and the format_number function does the formatting.

>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL)
'020 8366 1177'
>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
'+44 20 8366 1177'
>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164)
'+442083661177'

If your application has a UI that allows the user to type in a phone number, it's nice to get the formatting applied as the user types. The AsYouTypeFormatter object allows this.

>>> formatter = phonenumbers.AsYouTypeFormatter("US")
>>> formatter.input_digit("6")
'6'
>>> formatter.input_digit("5")
'65'
>>> formatter.input_digit("0")
'650'
>>> formatter.input_digit("2")
'650 2'
>>> formatter.input_digit("5")
'650 25'
>>> formatter.input_digit("3")
'650 253'
>>> formatter.input_digit("2")
'650-2532'
>>> formatter.input_digit("2")
'(650) 253-22'
>>> formatter.input_digit("2")
'(650) 253-222'
>>> formatter.input_digit("2")
'(650) 253-2222'

Sometimes, you've got a larger block of text that may or may not have some phone numbers inside it. For this, the PhoneNumberMatcher object provides the relevant functionality; you can iterate over it to retrieve a sequence of PhoneNumberMatch objects. Each of these match objects holds a PhoneNumber object together with information about where the match occurred in the original string.

>>> text = "Call me at 510-748-8230 if it's before 9:30, or on 703-4800500 after 10am."
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...     print(match)
...
PhoneNumberMatch [11,23) 510-748-8230
PhoneNumberMatch [51,62) 703-4800500
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...     print(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164))
...
+15107488230
+17034800500

You might want to get some information about the location that corresponds to a phone number. The geocoder.description_for_number does this, when possible.

>>> from phonenumbers import geocoder
>>> ch_number = phonenumbers.parse("0431234567", "CH")
>>> geocoder.description_for_number(ch_number, "de")
'Zürich'
>>> geocoder.description_for_number(ch_number, "en")
'Zurich'
>>> geocoder.description_for_number(ch_number, "fr")
'Zurich'
>>> geocoder.description_for_number(ch_number, "it")
'Zurigo'

For mobile numbers in some countries, you can also find out information about which carrier originally owned a phone number.

>>> from phonenumbers import carrier
>>> ro_number = phonenumbers.parse("+40721234567", "RO")
>>> carrier.name_for_number(ro_number, "en")
'Vodafone'

You might also be able to retrieve a list of time zone names that the number potentially belongs to.

>>> from phonenumbers import timezone
>>> gb_number = phonenumbers.parse("+447986123456", "GB")
>>> timezone.time_zones_for_number(gb_number)
('Atlantic/Reykjavik', 'Europe/London')

For more information about the other functionality available from the library, look in the unit tests or in the original libphonenumber project.

Memory Usage

The library includes a lot of metadata, potentially giving a significant memory overhead. There are two mechanisms for dealing with this.

  • The normal metadata (just over 2 MiB of generated Python code) for the core functionality of the library is loaded on-demand, on a region-by-region basis (i.e. the metadata for a region is only loaded on the first time it is needed).
  • Metadata for extended functionality is held in separate packages, which therefore need to be explicitly loaded separately. This affects:
    • The geocoding metadata (~19 MiB), which is held in phonenumbers.geocoder and used by the geocoding functions (geocoder.description_for_number, geocoder.description_for_valid_number or geocoder.country_name_for_number).
    • The carrier metadata (~1 MiB), which is held in phonenumbers.carrier and used by the mapping functions (carrier.name_for_number or carrier.name_for_valid_number).
    • The timezone metadata (~100 KiB), which is held in phonenumbers.timezone and used by the timezone functions (time_zones_for_number or time_zones_for_geographical_number).

The phonenumberslite version of the library does not include the geocoder, carrier and timezone packages, which can be useful if you have problems installing the main phonenumbers library due to space/memory limitations.

If you need to ensure that the metadata memory use is accounted for at start of day (i.e. that a subsequent on-demand load of metadata will not cause a pause or memory exhaustion):

  • Force-load the normal metadata by calling phonenumbers.PhoneMetadata.load_all().
  • Force-load the extended metadata by importing the appropriate packages (phonenumbers.geocoder, phonenumbers.carrier, phonenumbers.timezone).

Static Typing

The library includes a set of type stub files to support static type checking by library users. These stub files signal the types that should be used, and may also be of use in IDEs which have integrated type checking functionalities.

These files are written for Python 3, and as such type checking the library with these stubs on Python 2.5-2.7 is unsupported.

Project Layout

  • The python/ directory holds the Python code.
  • The resources/ directory is a copy of the resources/ directory from libphonenumber. This is not needed to run the Python code, but is needed when upstream changes to the master metadata need to be incorporated.
  • The tools/ directory holds the tools that are used to process upstream changes to the master metadata.

Release files for phonenumbers 9.0.39

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for phonenumbers 9.0.39
File Size Uploaded
phonenumbers-9.0.39.tar.gz 2.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for phonenumbers 9.0.39
File Interpreter ABI Platform
phonenumbers-9.0.39-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 4.9 MB

Release files / phonenumbers-9.0.39.tar.gz

Download URL phonenumbers-9.0.39.tar.gz
Size 2.3 MB
Tags Source
SHA-256 checksum
How to use checksums
41a7ff0257d5a7a096a8e263da7efc0a96a6162c84ba64af2ebf8972d09e70e1
BLAKE2b-256 checksum
How to use checksums
41d9f73babd005dd7569a81789f4db9cc82efaf001af018d5752bf6d7ea14e19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / phonenumbers-9.0.39-py2.py3-none-any.whl

Download URL phonenumbers-9.0.39-py2.py3-none-any.whl
Size 2.6 MB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d83b878d0188c81a5f5e8572cedcc7262bd0ea3806eb7c8e35d8666d262a8af0
BLAKE2b-256 checksum
How to use checksums
9953238c99f028b9c638ceae8218a33904e4b84d60e1c3bd707c0ff231b388cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

9.0.39 This release

2 release files

9.0.38

2 release files

9.0.37

2 release files

9.0.35

2 release files

9.0.33

2 release files

9.0.31

2 release files

9.0.29

2 release files

9.0.28

2 release files

9.0.26

2 release files

9.0.25

2 release files

9.0.24

2 release files

9.0.22

2 release files

9.0.21

2 release files

9.0.19

2 release files

9.0.17

2 release files

9.0.16

2 release files

9.0.15

2 release files

9.0.14

2 release files

9.0.13

2 release files

9.0.12

2 release files

9.0.10

2 release files

9.0.9

2 release files

9.0.8

2 release files

9.0.7

2 release files

9.0.6

2 release files

9.0.5

2 release files

9.0.4

2 release files

9.0.3

2 release files

9.0.2

2 release files

9.0.1

2 release files

9.0.0

2 release files

8.13.9

2 release files

8.13.8

2 release files

8.13.6

2 release files

8.13.5

2 release files

8.13.3

2 release files

8.13.1

2 release files

8.12.8

2 release files

8.12.7

2 release files

8.12.6

2 release files

8.12.4

2 release files

8.12.2

2 release files

8.12.1

2 release files

8.12.0

2 release files

8.11.4

2 release files

8.11.2

2 release files

8.11.1

2 release files

8.11.0

2 release files

8.10.8

2 release files

8.10.7

2 release files

8.10.6

2 release files

8.10.4

2 release files

8.10.3

2 release files

8.10.1

2 release files

8.10.0

2 release files

8.9.16

2 release files

8.9.14

2 release files

8.9.12

2 release files

8.9.11

2 release files

8.9.10

2 release files

8.9.9

2 release files

8.9.8

2 release files

8.9.7

2 release files

8.9.6

2 release files

8.9.5

2 release files

8.9.4

2 release files

8.9.3

2 release files

8.9.2

2 release files

8.9.1

2 release files

8.9.0

2 release files

8.8.10

2 release files

8.8.9

2 release files

8.8.8

2 release files

8.8.7

2 release files

8.8.6

2 release files

8.8.5

2 release files

8.8.4

2 release files

8.8.3

2 release files

8.8.2

2 release files

8.8.1

2 release files

8.8.0

2 release files

8.7.1

2 release files

8.7.0

2 release files

8.6.0

2 release files

8.5.2

2 release files

8.5.1

2 release files

8.5.0

2 release files

8.4.3

2 release files

8.4.2

2 release files

8.4.1

2 release files

8.4.0

2 release files

8.3.3

2 release files

8.3.2

2 release files

8.3.1

2 release files

8.3.0

2 release files

8.2.0

2 release files

8.1.0

2 release files

8.0.1

2 release files

8.0.0

2 release files

7.7.5

2 release files

7.7.4

2 release files

7.7.3

2 release files

7.7.2

2 release files

7.7.1

2 release files

7.7.0

2 release files

7.6.1

2 release files

7.6.0

2 release files

7.5.2

2 release files

7.5.1

1 release file

7.5.0

1 release file

7.4.5

1 release file

7.4.4

1 release file

7.4.3

1 release file

7.4.2

1 release file

7.4.1

1 release file

7.4.0

1 release file

7.3.2

1 release file

7.3.1

1 release file

7.3.0

1 release file

7.2.8

1 release file

7.2.7

1 release file

7.2.6

1 release file

7.2.5

1 release file

7.2.4

1 release file

7.2.3

1 release file

7.2.2

1 release file

7.1.1

1 release file

7.1.0

1 release file

7.0.11

1 release file

7.0.9

1 release file

7.0.8

1 release file

7.0.7

1 release file

7.0.6

1 release file

7.0.5

1 release file

7.0.4

1 release file

7.0.2

1 release file

7.0.1

1 release file

7.0.0

1 release file

6.3.0

1 release file

6.2.0

1 release file

6.1.0

1 release file

6.0.0

1 release file

5.9.2

1 release file

5.9.1

1 release file

3.8b1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page