MGRS Coordinate Conversion for Python
The `mgrs` Python library (version 1.5.4) provides a simple `ctypes` wrapper around selected MGRS-related C functions from GeoTrans (version 2.4.2), an internal copy of which is included within the library. It facilitates conversions to and from Military Grid Reference System (MGRS) coordinates and decimal degrees (latitude/longitude), offering various precision levels. The library is actively maintained, with a recent release in February 2026.
Common errors
-
mgrs.core.MGRSError: Unable to load libmgrs.cpXX-win_amd64.pyd
cause This error typically occurs when using application packagers like PyInstaller on Windows. The underlying shared library (`.pyd` file) for `mgrs` is not correctly located or bundled in the final executable.fixWhen using PyInstaller, you often need to explicitly include the `mgrs` package's shared libraries in your `.spec` file. A common fix is to add a `datas` entry to copy the `libmgrs*.pyd` file. For example: `datas=[('path/to/venv/Lib/site-packages/mgrs', 'mgrs')]` (adjust paths as necessary to include the `.pyd` file). -
ImportError: cannot import name 'MGRS' from 'mgrs' (or similar during `import mgrs`)
cause This usually indicates an incomplete or corrupted installation of the `mgrs` package, or a conflict if another package named `mgrs` exists in the Python path. In older versions, it could also stem from failed compilation of the C components.fixPerform a clean reinstall of the package: `pip uninstall mgrs` followed by `pip install mgrs --no-cache-dir`. Ensure your Python environment meets the `>=3.9` requirement and that no other `mgrs.py` or `mgrs` package shadows the correct one. -
TypeError: toMGRS() missing 1 required positional argument: 'longitude'
cause The `toMGRS` method requires both `latitude` and `longitude` arguments. This error means one or both were omitted or passed incorrectly.fixEnsure both latitude and longitude are provided as numeric arguments: `m.toMGRS(latitude, longitude)`.
Warnings
- deprecated The `RTreeError` exception was deprecated and aliased to `MGRSError` in version 1.4.0. Direct usage of `RTreeError` should be replaced with `MGRSError` for future compatibility.
- breaking Earlier versions (prior to 1.3.4) were reported to incorrectly round MGRS coordinates instead of truncating them, leading to precision errors. MGRS requires truncation.
- gotcha For older Python environments or manual compilation, the `mgrs` library, being a Ctypes wrapper around C code, historically had complex installation challenges on Windows without pre-built wheels. This is largely mitigated by modern `pip` and wheel distributions.
Install
-
pip install mgrs
Imports
- MGRS
import mgrs; mgrs.MGRS()
from mgrs import MGRS
Quickstart
from mgrs import MGRS
m = MGRS()
# Convert Latitude/Longitude to MGRS
latitude = 42.0
longitude = -93.0
mgrs_coords = m.toMGRS(latitude, longitude)
print(f"({latitude}, {longitude}) -> {mgrs_coords}")
# Convert MGRS to Latitude/Longitude
lat_lon_coords = m.toLatLon(mgrs_coords)
print(f"{mgrs_coords} -> {lat_lon_coords}")
# Convert DMS to Decimal Degrees
dms_str = '321942.29N'
dec_deg = m.dmstodd(dms_str)
print(f"{dms_str} -> {dec_deg}")
# Convert Decimal Degrees to DMS
d, minute, s = m.ddtodms(dec_deg)
print(f"{dec_deg} -> (D:{d}, M:{minute}, S:{s}))")