AttributeError: module 'babel' has no attribute 'numbers'

问题描述 投票:0回答:1

目标:传递语言环境代码并检索货币符号。

我的方法是使用

babel
库。然而,我在版本
AttributeError
中遇到了一个
2.12.1

文档 | PDF页码:82

代码:

import babel

def get_currency_symbol(locale_code):
    try:
        return babel.numbers.get_currency_symbol(None, locale_code)
    except babel.core.UnknownLocaleError:
        print(f"Invalid locale code: {locale_code}")
        return '$'

print(get_currency_symbol('en_US'))
print(get_currency_symbol('fr_FR'))
print(get_currency_symbol('invalid_locale'))

回溯:

(venv) me@VKY7WPYWV1 project % pip install --upgrade babel
Requirement already satisfied: babel in /Users/me/miniconda3/envs/project/lib/python3.9/site-packages (2.12.1)

(venv) me@VKY7WPYWV1 project % pip show babel
Name: Babel
Version: 2.12.1
Summary: Internationalization utilities
Home-page: https://babel.pocoo.org/
Author: Armin Ronacher
Author-email: [email protected]
License: BSD
Location: /Users/me/miniconda3/envs/project/lib/python3.9/site-packages
Requires: 
Required-by:

(venv) me@VKY7WPYWV1 project % python tools/del.py
Traceback (most recent call last):
  File "/Users/me/GitHub/lumada-catalog/project/tools/del.py", line 7, in <module>
    print(get_currency_symbol('en_US'))
  File "/Users/me/GitHub/lumada-catalog/project/tools/del.py", line 4, in get_currency_symbol
    return babel.numbers.get_currency_symbol(None, locale_code)
AttributeError: module 'babel' has no attribute 'numbers'
python-3.x pip locale attributeerror python-babel
1个回答
0
投票

导入

babel.numbers
使用子模块,而不仅仅是
babel
.

© www.soinside.com 2019 - 2024. All rights reserved.