PyMoneyed provides robust Currency and Money classes for handling monetary values in Python. It is currently at version 3.0 and has a release cadence tied to significant feature additions and Python version support changes, ensuring compatibility and modern practices.
pip install py-moneyedVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create `Money` objects, perform basic arithmetic, and access their components. It highlights the importance of using `Decimal` or string for amounts to maintain precision and shows how `Money` enforces currency-aware operations.
Upgrade your Python environment to 3.7 or newer.
Migrate any usage of `moneyed.localization.format_money` to `moneyed.l10n.format_money`. Ensure Babel is installed for formatting capabilities (`pip install Babel`).
Always provide a `Currency` object or a 3-letter ISO currency code string when creating a `Money` instance (e.g., `Money(100, USD)` or `Money(100, 'USD')`).
Always pass `Decimal` objects or string representations for monetary amounts to the `Money` constructor (e.g., `Money(Decimal('10.20'), USD)` or `Money('10.20', USD)`).Use `Currency.country_codes` or other Babel-based methods for country-related currency information.
These settings are discontinued in 3.0. For formatting, use `moneyed.l10n.format_money` with its locale-aware options, which relies on Babel's CLDR data for proper display.
Convert the non-Money operand to a Decimal or another Money instance of the same currency before performing the operation. For example, `money_object + Money(Decimal('5'), USD)` or `money_object.amount + Decimal('5')` if you only need the amount.Ensure both Money instances have the same Currency before performing the operation. If currency conversion is intended, explicitly convert one of the amounts using a dedicated exchange rate mechanism (not provided by py-moneyed) before creating a new Money object.
Update your code to explicitly provide a `Currency` object when instantiating `Money`, or import and use pre-defined currency constants like `USD`, `EUR`, `GBP` directly from the `moneyed` module (e.g., `from moneyed import Money, USD`).
Always provide a valid `Currency` object (e.g., `USD`, `EUR` imported from `moneyed`) or a three-letter ISO currency code string to the `Money` constructor. For example, `Money('10.00', USD)` or `Money(amount='10.00', currency='USD')`.Ensure the amount argument is a valid numeric string (e.g., '10.50'), an `int`, or a `Decimal` instance. Avoid using `float` directly due to potential precision issues, and ensure the string format matches what `decimal.Decimal` expects.