You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入brightway2遇AttributeError:numpy无bool属性的解决求助

问题
  • 操作流程:从Anaconda进入brightway环境,打开终端启动Jupyter-Lab,执行from brightway2 import *时触发AttributeError;直接在Anaconda终端执行该命令也会出现相同错误。
  • 已尝试的解决动作:更新brightway及相关依赖、新建Anaconda环境重试、测试numpy 1.19.0、1.20.0等版本,问题仍未解决。
  • 设备信息:MacBook Pro 2017(2.3 GHz 双核Intel Core i5、Intel Iris Plus Graphics 640 1536 MB、16 GB 2133 MHz LPDDR3、macOS 13.6.1 (22G313))
  • 错误信息:
AttributeError                            Traceback (most recent call last)
Cell In[1], line 1
----> 1 from brightway2 import *

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/brightway2/__init__.py:2
      1 # -*- coding: utf-8 -*----> 2 from bw2data import *
      3 from bw2calc import *
      4 from bw2io import *

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/__init__.py:64
     61 config.p = preferences
     63 from .serialization import JsonWrapper
----> 64 from .database import DatabaseChooser as Database, get_activity
     65 from .data_store import DataStore, ProcessedDataStore
     66 from .method import Method

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/database.py:6
      3 from eight import *
      5 from . import databases, config
----> 6 from .backends.single_file import SingleFileDatabase
      7 from .backends.json import JSONDatabase
      8 from .backends.peewee import SQLiteBackend

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/backends/__init__.py:5
      2 from __future__ import print_function, unicode_literals
      3 from eight import *
----> 5 from .base import LCIBackend
      6 from .peewee import SQLiteBackend
      7 from .json import JSONDatabase

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/backends/base.py:12
      3 from eight import *
      5 from .. import (
      6     config,
      7     databases,
   (...)
     10     projects,
     11 )
----> 12 from ..data_store import ProcessedDataStore
     13 from ..errors import UntypedExchange, InvalidExchange, UnknownObject
     14 from ..query import Query

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/data_store.py:151
    147         self.validator(data)
    148         return True
--> 151 class ProcessedDataStore(DataStore):
    152     """
    153 Brightway2 data stores that can be processed to NumPy arrays. In addition to ``metadata`` and (optionally) ``validator``, subclasses should define:
    154 
   (...)
    160 
    161     """
    162     dtype_fields = None

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/data_store.py:172, in ProcessedDataStore()
    162 dtype_fields = None
    163 # Numpy columns names can't be unicode
    164 base_uncertainty_fields = [
    165     (numpy_string('uncertainty_type'), np.uint8),
    166     (numpy_string('amount'), np.float32),
    167     (numpy_string('loc'), np.float32),
    168     (numpy_string('scale'), np.float32),
    169     (numpy_string('shape'), np.float32),
    170     (numpy_string('minimum'), np.float32),
    171     (numpy_string('maximum'), np.float32),
--> 172     (numpy_string('negative'), np.bool),
    173 ]
    175 @property
    176 def dtype(self):
    177     """Returns both the generic ``base_uncertainty_fields`` plus class-specific ``dtype_fields``. ``dtype`` determines the columns of the :ref:`processed array <processing-data>`.'""

File ~/anaconda3/envs/bw/lib/python3.11/site-packages/numpy/__init__.py:324, in __getattr__(attr)
    319     warnings.warn(
    320         f"In the future `np.{attr}` will be defined as the "
    321         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    323 if attr in __former_attrs__:
--> 324     raise AttributeError(__former_attrs__[attr])
    326 if attr == 'testing':
    327     import numpy.testing as testing

AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  • 疑问:是否有其他人遇到过该问题?如何解决?有什么推荐方法?我该如何修复这个错误?
解决方案

这个问题是brightway2依赖的bw2data库使用了numpy已弃用的np.bool别名,在numpy 1.20+版本中该别名被移除导致的,以下是可行修复方法:

  • 方法一:安装兼容的numpy版本
    确保安装numpy 1.19.5及以下版本(1.20.0开始标记np.bool为弃用,部分环境仍会报错),执行命令:

    conda install numpy=1.19.5
    

    安装完成后重新导入brightway2测试。

  • 方法二:手动修改bw2data源码
    找到报错文件~/anaconda3/envs/bw/lib/python3.11/site-packages/bw2data/data_store.py,定位到第172行,将np.bool替换为bool或np.bool_:

    # 原代码
    (numpy_string('negative'), np.bool),
    # 修改为二选一
    (numpy_string('negative'), bool),
    # 或
    (numpy_string('negative'), np.bool_),
    

    保存文件后重新启动Jupyter-Lab或终端,再次导入brightway2即可。

  • 方法三:升级到适配新版本的brightway分支
    考虑迁移到brightway25,该版本已适配numpy最新变化,执行更新命令:

    pip install brightway25 --upgrade
    

    迁移前请备份好现有项目数据。

内容的提问来源于stack exchange,提问作者Büşra Haliloğlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:12:06