{"id":6383,"library":"ip2location","title":"IP2Location Python Library","description":"This is an IP geolocation library that enables the user to find the country, region, city, latitude and longitude, ZIP code, time zone, ISP, domain name, area code, weather info, mobile info, elevation, usage type, address type and IAB category from an IP address. It supports both IPv4 and IPv6 lookup.","status":"active","version":"8.11.0","language":"en","source_language":"en","source_url":"https://github.com/chrislim2888/ip2location-python","tags":["geolocation","ip address","security","data","networking"],"install":[{"cmd":"pip install IP2Location","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"The `ip2locationio` package is for the IP2Location.io web API, not the local BIN database library.","wrong":"from ip2locationio import IPGeolocation","symbol":"IP2Location","correct":"import IP2Location"}],"quickstart":{"code":"import IP2Location\nimport os\n\n# NOTE: You must download an IP2Location BIN database file separately.\n# For a free LITE database: https://lite.ip2location.com/ip2location-lite\n# Place the .BIN file in a 'data' directory or specify its full path.\nDB_PATH = os.path.join(\"data\", \"IP2LOCATION-LITE-DB1.BIN\") # Replace with your actual database file\n\n# Ensure the database file exists\nif not os.path.exists(DB_PATH):\n    print(f\"Error: IP2Location BIN database not found at {DB_PATH}\")\n    print(\"Please download a database from https://lite.ip2location.com/ip2location-lite\")\nelse:\n    try:\n        # Initialize the IP2Location object with the database file\n        # Use 'FILE_IO' (default) or 'SHARED_MEMORY' if your system has enough RAM (IP2Location.IP2LOCATION_SHARED_MEMORY)\n        database = IP2Location.IP2Location(DB_PATH)\n        \n        # Lookup an IP address\n        ip_address = \"8.8.8.8\" # Google Public DNS\n        rec = database.get_all(ip_address)\n\n        if rec:\n            print(f\"IP Address: {ip_address}\")\n            print(f\"Country Code: {rec.country_short}\")\n            print(f\"Country Name: {rec.country_long}\")\n            print(f\"Region Name: {rec.region}\")\n            print(f\"City Name: {rec.city}\")\n            print(f\"Latitude: {rec.latitude}\")\n            print(f\"Longitude: {rec.longitude}\")\n            print(f\"ISP: {rec.isp}\")\n            # Access other fields like rec.zipcode, rec.timezone, etc.\n        else:\n            print(f\"No data found for IP: {ip_address}\")\n            print(\"This might mean your database does not contain this IP range or is outdated.\")\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n        print(\"Ensure your BIN database is compatible with the library version and not corrupted.\")","lang":"python","description":"This quickstart demonstrates how to load an IP2Location BIN database file and query geolocation information for an IP address. You must download a database file separately and provide its path."},"warnings":[{"fix":"Download the appropriate BIN database from IP2Location (e.g., https://lite.ip2location.com/ip2location-lite for free LITE data) and provide its path during `IP2Location.IP2Location()` initialization.","message":"The library relies on a separate IP2Location BIN database file, which must be downloaded and kept updated by the user. It does not work out-of-the-box without this file.","severity":"breaking","affected_versions":"All versions"},{"fix":"Ensure you are importing `IP2Location` if you intend to use local BIN files. If you need the web API, install and use `ip2location-io` as per its documentation.","message":"There are two distinct Python packages: `ip2location` (this library, for local BIN databases) and `ip2location-io` (for the IP2Location.io web API service). Using `ip2locationio` will require an API key and make web requests, not read local BIN files.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Monitor memory usage when using `SHARED_MEMORY`. If memory is a concern, use the default `FILE_IO` mode, or consider using a smaller database. For large databases, ensure ample RAM is available.","message":"Using the `SHARED_MEMORY` file mode for database loading can significantly speed up lookups but consumes substantial RAM. Ensure your system has sufficient memory to cache the entire database to avoid performance issues or out-of-memory errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For applications with multiple threads accessing the database concurrently, consider initializing a new `IP2Location` object for each thread or process to prevent potential resource conflicts or race conditions. If a single instance is shared, implement explicit synchronization (e.g., `threading.Lock`).","message":"While the underlying C++ library is stated to be thread-safe, the Python wrapper's thread-safety when sharing a single `IP2Location` object across multiple threads for `FILE_IO` operations is not explicitly documented. For concurrent access, creating a separate `IP2Location` instance per thread or using appropriate locking mechanisms is generally safer.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}