Phonenumberslite is a Python port of Google's `libphonenumber` library, designed for parsing, formatting, storing, and validating international phone numbers. It is a 'lite' version of the main `phonenumbers` library, specifically omitting the larger geocoding, carrier, and timezone data packages to reduce its footprint, making it suitable for environments with memory or space limitations. The library is actively maintained, with the current version being 9.0.27.
pip install phonenumbersliteVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse, validate, and format phone numbers using `phonenumberslite`. It shows how to handle numbers with and without explicit region codes, checks for validity and possibility, and formats numbers into E.164, International, and National formats. It also includes error handling for `NumberParseException` when an invalid string is provided.
If geocoding, carrier, or timezone features are required, `pip uninstall phonenumberslite` and then `pip install phonenumbers`.
Always provide a `region_code` (e.g., `phonenumbers.parse('02083661177', 'GB')`) for numbers not in E.164 format, or handle `NumberParseException` appropriately.Use `phonenumbers.is_valid_number(parsed_number)` for comprehensive validation. Use `is_possible_number()` for a quicker, less strict check when performance is critical (e.g., filtering a large dataset).
Always use `phonenumbers.parse(number_string, region_code)` to obtain a `PhoneNumber` object.
Ensure that either `phonenumbers` or `phonenumberslite` is installed consistently throughout your project's dependencies. If `phonenumberslite` is explicitly desired, verify your import statements are correct (e.g., `import phonenumbers` will work as `phonenumberslite` provides the `phonenumbers` package), and check your Python environment's installed packages. If a dependency truly requires features only in the full `phonenumbers` library, you might need to switch to `pip install phonenumbers` instead of `phonenumberslite`.
When parsing national numbers, always provide a valid two-letter ISO 3166-1 country code as the `region_code` argument, or ensure the number includes the full E.164 international format (e.g., `+CCXXXXXXXXXX`).
```python
import phonenumbers
# Correct: Provide a region code for a national number
number = phonenumbers.parse("02081234567", "GB")
print(number)
# Correct: Use E.164 format (no region code needed)
number = phonenumbers.parse("+442081234567", None)
print(number)
```If your application requires geocoding, carrier, or timezone functionality, you must install the full `phonenumbers` library instead of `phonenumberslite`. ```bash pip uninstall phonenumberslite pip install phonenumbers ``` Then, your imports like `from phonenumbers import geocoder` will work correctly.
Ensure that the input string is a plausible phone number before attempting to parse it. You might want to pre-process the input to remove irrelevant characters or validate its basic structure. Always wrap parsing calls in a `try-except` block to handle invalid inputs gracefully.
```python
import phonenumbers
try:
# Example of an unparseable string
number = phonenumbers.parse("gibberish", None)
print(number)
except phonenumbers.phonenumberutil.NumberParseException as e:
print(f"Error parsing number: {e}")
# A valid, parseable number
try:
number = phonenumbers.parse("+15551234567", None)
print(number)
except phonenumbers.phonenumberutil.NumberParseException as e:
print(f"Error parsing number: {e}")
```No dependency data recorded yet.