AttributeError: 模块 ‘babel’ 没有 ‘numbers’ 属性。

huangapple go评论61阅读模式
英文:

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&quot;Invalid locale code: {locale_code}&quot;)
        return &#39;$&#39;

print(get_currency_symbol(&#39;en_US&#39;))
print(get_currency_symbol(&#39;fr_FR&#39;))
print(get_currency_symbol(&#39;invalid_locale&#39;))

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 &quot;/Users/me/GitHub/lumada-catalog/project/tools/del.py&quot;, line 7, in &lt;module&gt;
    print(get_currency_symbol(&#39;en_US&#39;))
  File &quot;/Users/me/GitHub/lumada-catalog/project/tools/del.py&quot;, line 4, in get_currency_symbol
    return babel.numbers.get_currency_symbol(None, locale_code)
AttributeError: module &#39;babel&#39; has no attribute &#39;numbers&#39;

答案1

得分: 3

Import babel.numbers to use the submodule, not just babel.

英文:

Import babel.numbers to use the submodule, not just babel.

huangapple
  • 本文由 发表于 2023年3月31日 15:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定