英文:
AttributeError: module 'babel' has no attribute 'numbers'
问题
我的方法是使用babel
库。但是,我在版本2.12.1
中遇到了AttributeError
。
代码:
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: 国际化工具
Home-page: https://babel.pocoo.org/
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
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'
英文:
Goal: Pass a locale code and retrieve a currency symbol.
My approach is to use babel
library. However, I encounter an AttributeError
in version 2.12.1
.
Documentation | PDF page number: 82
Code:
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'))
Traceback:
(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: armin.ronacher@active-4.com
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'
答案1
得分: 3
Import babel.numbers
to use the submodule, not just babel
.
英文:
Import babel.numbers
to use the submodule, not just babel
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论