geopy is a Python client for numerous popular geocoding web services, including OpenStreetMap Nominatim, Google Geocoding API, and Bing Maps. It simplifies the process for Python developers to locate the coordinates of addresses, cities, countries, and landmarks globally, as well as perform reverse geocoding. The library is actively maintained, with its current stable version being 2.4.1, and releases occur regularly to add features and address issues. It supports Python versions 3.7 through 3.12.
pip install geopyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the Nominatim geocoder to convert an address string into geographical coordinates (latitude and longitude) and vice-versa. It highlights the necessity of providing a `user_agent` for Nominatim, as recommended by its usage policy, and safely retrieves it from an environment variable.
Replace `from geopy.distance import vincenty` with `from geopy.distance import geodesic` and update calls accordingly.
If you require multiple results, explicitly set `exactly_one=False` in your `geocode` or `reverse` calls.
Move any service-specific parameters from the geocoder's constructor to the `geocode` or `reverse` method calls.
Migrate to an alternative geocoding service supported by geopy, such as Nominatim, GoogleV3, or MapBox.
Always provide a unique and descriptive `user_agent` when initializing `Nominatim`, e.g., `Nominatim(user_agent='my_awesome_app_name')`.
Ensure an `api_key` is always provided when initializing `GoogleV3` to avoid runtime errors.
Install the package using `pip install geopy`. If the issue persists, rename any local Python files named `geopy.py` or ensure you are in the correct virtual environment where geopy is installed.
Rename any local Python files or directories named `geopy.py` or `geopy` to avoid conflict with the installed library.
Always check if the `location` object returned by `geocode()` or `reverse()` is `None` before attempting to access its attributes. For example: `location = geolocator.geocode('NonExistent Address')` then `if location: print(location.latitude)`.Increase the `timeout` parameter when initializing the geolocator or when calling `geocode`/`reverse`. For example: `geolocator = Nominatim(user_agent='my-app', timeout=10)` or `location = geolocator.geocode(query, timeout=10)`.
Implement rate-limiting (e.g., using `time.sleep()` between requests or `geopy.extra.rate_limiter`), introduce delays between API calls, or consider upgrading to a paid plan with higher request quotas from the geocoding service provider.