Have you ever wondered how you can use Python to track the location from phone number? In this article, we’ll explore how to create a simple phone number location tracker using Python. We’ll leverage Python’s versatility and various libraries to achieve this. Whether you’re a beginner or an experienced developer, this guide will walk you through the process, explaining each step in detail.
Step 1: Setting Up the Environment To begin, make sure you have Python installed on your computer. You’ll also need to install the phonenumbers library, which provides attributes such as geocoders with capabilities to locate the country. You can install it using pip:
pip install phonenumbers
Step 2: Writing the Python Code Now, let’s write the Python code for our mobile tracker. We’ll use the phonenumbers library to get the mobile device’s location.
import phonenumbers
from phonenumbers import geocoder
def track_phone_number_location(phone_number):
# Parse the phone number
phone_number = phonenumbers.parse(phone_number)
# Get the country and region code
country_code = phone_number.country_code
region_code = geocoder.region_code_for_number(phone_number)
# Get the country, region, and state names
country_name = geocoder.country_name_for_number(phone_number, "en")
region_name = geocoder.description_for_number(phone_number, "en")
state_name = geocoder.description_for_number(phone_number, "en")
# Print the location details
print(f"Country Code: +{country_code}")
print(f"Region Code: {region_code}")
print(f"Country Name: {country_name}")
print(f"Region Name: {region_name}")
# Phone number to track (e.g., "+14155552671")
phone_number = input("Enter the phone number to track (with country code): ")
track_phone_number_location(phone_number)
Output:

Now let’s understand how the code is workinng:
import phonenumbers: Imports thephonenumberslibrary, which provides functionality to work with phone numbers.from phonenumbers import geocoder: Imports thegeocodermodule fromphonenumbers, which provides functions to get information about the geographical location of a phone number.def track_phone_number_location(phone_number):: Defines a function calledtrack_phone_number_locationthat takes aphone_numberas input.phone_number = phonenumbers.parse(phone_number): Parses the inputphone_numberusingphonenumbers.parse()to get aPhoneNumberobject.country_code = phone_number.country_code: Gets the country code from the parsed phone number object.region_code = geocoder.region_code_for_number(phone_number): Uses thegeocoder.region_code_for_number()function to get the region code (e.g., state or province code) for the phone number.country_name = geocoder.country_name_for_number(phone_number, "en"): Gets the country name for the phone number in English.region_name = geocoder.description_for_number(phone_number, "en"): Gets the region name (e.g., state or province name) for the phone number in English.state_name = geocoder.description_for_number(phone_number, "en"): Gets the state name for the phone number in English.print(f"Country Code: +{country_code}"): Prints the country code of the phone number.print(f"Region Code: {region_code}"): Prints the region code of the phone number.print(f"Country Name: {country_name}"): Prints the country name of the phone number.print(f"Region Name: {region_name}"): Prints the region name of the phone number.phone_number = input("Enter the phone number to track (with country code): "): Asks the user to enter a phone number to track.track_phone_number_location(phone_number): Calls thetrack_phone_number_locationfunction with the user-entered phone number to track its location.
This code takes a phone number as input, parses it, and then uses the phonenumbers and geocoder modules to get and print information about the location of the phone number, including country code, region code, country name, and region name.
Conclusion: In this article, we’ve learned how to create a simple mobile number location using Python. By leveraging the phonenumbers library, we were able to obtain the device’s location and display it in a user-friendly format. This project demonstrates the power of Python in performing real-time location tracking tasks.





Leave a Reply