Registry / serialization / gmpy2
library2.3.1pypypi✓ verified 21d ago

gmpy2 is an optimized, C-coded Python extension module that provides an interface to the GMP (GNU Multiple Precision Arithmetic Library), MPFR (Multiple-Precision Floating-point Reliable Library), and MPC (Multiple Precision Complex Library). It enables fast arbitrary-precision integer, rational, real, and complex arithmetic in Python. The current stable version is 2.3.0, and it is actively maintained with regular updates to support newer Python versions and fix bugs.

pip install gmpy2
INSTALL
IMPORT
SIG · GMPY2
G
gmpy2
serializationpythonv2.3.1
Install
1.7s avg
Import
110ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.116s · 22.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.104s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mpz
from gmpy2 import mpz
mpq
from gmpy2 import mpq
mpfr
from gmpy2 import mpfr
mpc
from gmpy2 import mpc
get_context
from gmpy2 import get_context
*
from gmpy2 import mpz, mpfr
from gmpy2 import *
Importing all symbols with '*' is not recommended to avoid potential name conflicts with other modules or variables.

This quickstart demonstrates basic arbitrary-precision integer (mpz), real (mpfr), and rational (mpq) arithmetic. It also shows how to manage floating-point precision using `gmpy2.get_context()` and `gmpy2.localcontext()` to control precision settings. Constants should ideally be passed as strings to `mpfr` to ensure full arbitrary precision from the start, avoiding Python's native float limitations.

import gmpy2 from gmpy2 import mpz, mpfr, get_context # Arbitrary-precision integer arithmetic a = mpz(12345678901234567890 ** 2) b = mpz(5) print(f"mpz square: {a}") print(f"mpz division: {a / b}") # Arbitrary-precision floating-point real arithmetic # Use strings for exact representation to avoid Python float precision issues pi_approx = mpfr('3.14159265358979323846264338327950288') print(f"Default precision pi: {pi_approx}") # Manage precision using a context current_context = get_context() original_precision = current_context.precision with gmpy2.localcontext() as ctx: ctx.precision = 100 # Set precision to 100 bits pi_high_prec = gmpy2.const_pi() print(f"High precision pi (100 bits): {pi_high_prec}") print(f"Original precision after context block: {current_context.precision == original_precision}") # Example with rational numbers r = gmpy2.mpq(1, 3) + gmpy2.mpq(1, 4) print(f"mpq sum: {r}")
Debug
Known issues
breakingFuture versions of gmpy2 (e.g., v2.4.0 and later) will drop support for CPython versions older than 3.11. Users currently on Python 3.9 or 3.10 with gmpy2 2.3.0 will need to upgrade their Python interpreter to at least 3.11 before upgrading to gmpy2 v2.4.0 (stable) or newer.
fix
Upgrade Python to 3.11 or newer before updating gmpy2 past version 2.3.0.
affects: >=2.4.0 (alpha/beta versions already affected)
gotchaInstallation via `pip` usually provides pre-compiled binary wheels which include the necessary GMP, MPFR, and MPC C libraries. However, if a wheel is not available for your specific platform or Python version, `pip install gmpy2` will attempt to build from source, requiring these underlying C development libraries (e.g., `libgmp-dev`, `libmpfr-dev`, `libmpc-dev` on Debian/Ubuntu) to be pre-installed on your system.
fix
Ensure GMP, MPFR, and MPC development libraries are installed on your system if pre-compiled wheels are not available.
affects: All versions (when building from source)
gotchaArbitrary-precision arithmetic can be computationally intensive and memory-hungry, especially with extremely large numbers or extensive operations. While `gmpy2` is highly optimized, unmanaged use can still lead to significant performance bottlenecks or memory exhaustion for very demanding computations.
fix
Carefully manage the precision and scale of numbers. Monitor memory usage and CPU time for performance-critical sections. Consider using `xmpz` for mutable integer operations if appropriate.
affects: All versions
gotchaThe `gmpy2.context` object, which controls parameters like floating-point precision, rounding mode, and exception handling, is a global object by default. Directly modifying `gmpy2.get_context()` can cause side effects across different parts of an application.
fix
For temporary or localized changes to context settings, use `with gmpy2.localcontext() as ctx:`, which creates a thread-local copy and restores the previous context upon exiting the `with` block. Alternatively, explicitly create and activate contexts with `gmpy2.set_context(new_context)` and `gmpy2.get_context().copy()`.
affects: All versions
deprecatedThe `mpf` type, which was part of the original `gmpy` library (the predecessor to `gmpy2`), was renamed to `mpfr` in `gmpy2` version 2.0.0b1 to reflect its reliance on the MPFR library. Code migrating from older `gmpy` to `gmpy2` should update `mpf` references to `mpfr`.
fix
Replace all instances of `gmpy.mpf` with `gmpy2.mpfr`.
affects: <2.0.0b1 (for `gmpy` library users)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'gmpy2'
The 'gmpy2' library has not been installed in the Python environment being used, or the Python interpreter cannot locate the installed package.
fix
Install gmpy2 using pip: `pip install gmpy2`
ERROR: command 'cl.exe' failed with exit status 2
This error typically occurs on Windows when attempting to install gmpy2 from source via pip, as it requires a C compiler (like Microsoft Visual C++) and its associated build tools, which are often missing or not correctly configured.
fix
Install the Build Tools for Visual Studio from Microsoft's website. Alternatively, download a pre-compiled wheel (.whl file) for your specific Python version and architecture from a reputable source (e.g., Gohlke's Python Extension Packages for Windows) and install it using pip: `pip install path/to/gmpy2‑X.Y.Z‑cpXX‑cpXXm‑win_amd64.whl`
error: 'PyLongObject' {aka 'struct _longobject'} has no member named 'ob_digit'
This compilation error indicates that the version of gmpy2 being installed is not compatible with internal API changes in newer Python versions (e.g., Python 3.10, 3.12), specifically regarding how Python handles long integers.
fix
Upgrade gmpy2 to a version that officially supports your specific Python interpreter version. If a newer version is not available on PyPI, consider using a pre-compiled wheel if one exists for your Python version, or use a compatible older Python version. `pip install --upgrade gmpy2`
Upgrade
Version history
2.3.1latest on PyPI · released Jun 24, 2026
Audit
Dependencies
gmpoptionalUnderlying C library for arbitrary-precision integer and rational arithmetic. Often bundled in wheels.
mpfroptionalUnderlying C library for correctly rounded multiple-precision floating-point real arithmetic. Required since gmpy2 version 2.1.0a1. Often bundled in wheels.
mpcoptionalUnderlying C library for correctly rounded multiple-precision complex floating-point arithmetic. Required since gmpy2 version 2.1.0a1. Often bundled in wheels.
Agent activity
47 hits · last 30 days
node
46
Resources