forex-python (version 1.9.2) is an active Python library providing free foreign exchange rates and currency conversion functionalities. It supports listing all currency rates, retrieving Bitcoin prices, converting amounts between currencies, and accessing historical rates since 1999. The library's core data comes from theratesapi.com (sourced from the European Central Bank) for currency rates and CoinDesk for Bitcoin prices. It is regularly updated, though with an irregular release cadence, and is compatible with Python versions >=2.7 and <4, excluding 3.0-3.2.
pip install forex-pythonVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to fetch current exchange rates, convert amounts, get Bitcoin prices, and retrieve currency symbols using the `forex-python` library. It emphasizes using `force_decimal=True` for accurate financial calculations.
Upgrade to the latest version of `forex-python` (e.g., `pip install --upgrade forex-python`).
Initialize `CurrencyRates(force_decimal=True)` or `BtcConverter(force_decimal=True)` and use `decimal.Decimal` for amounts.
Understand that rates are daily, not live. For intra-day real-time data, use a different library or API service.
For critical applications, cross-verify rates with an alternative data source, especially for less common currency pairs or if discrepancies are observed.
Install the library using pip: `pip install forex-python` or `pip3 install forex-python`. Ensure your virtual environment is activated if applicable.
Check the status of `theratesapi.com`. Ensure your `forex-python` library is updated (`pip install --upgrade forex-python`). If requesting historical data, verify the date's validity. Implement `try-except` blocks to handle this specific error gracefully.
First, create an instance of the class before calling its methods. For example, instead of `CurrencyRates.get_rates(...)`, use `c = CurrencyRates(); c.get_rates(...)`.
Verify that the requested currency pair is generally supported by the ECB rates. To prevent the error, use the dictionary's `.get(key, default_value)` method or check for key existence with `if key in dictionary:` before accessing it.