Skip to content

[BUG] PGL安装脚本中错误地修改 __builtins__ 导致安装失败 #592

Description

@PlumBlossomMaid

[BUG] PGL安装脚本中错误地修改 __builtins__ 导致安装失败

描述

问题现象

在Python 3.11+环境中安装PGL时,执行到 setup.py 中的 finalize_options 函数会报以下错误:

AttributeError: 'dict' object has no attribute '__NUMPY_SETUP__'

复现步骤

  1. 环境:Python 3.11.4, NumPy 1.26.4
  2. 执行 pip install pgl

问题定位

问题出在 setup.pyfinalize_options 函数中:

def finalize_options(self):
    _build_ext.finalize_options(self)
    # Prevent numpy from thinking it is still in its setup process:
    __builtins__.__NUMPY_SETUP__ = False   # ← 问题行
    import numpy
    self.include_dirs.append(numpy.get_include())

根本原因

__builtins__ 在不同上下文中行为不同:

  • 在主模块中:__builtins__ 指向 builtins 模块本身(可添加属性)
  • 在非主模块中(如被导入的模块或安装脚本):__builtins__ 指向 builtins.__dict__(字典,不可添加属性)

安装脚本运行在非主模块环境下,因此 __builtins__ 是一个字典,不能通过 __builtins__.xxx = yyy 的方式添加属性。

影响范围

  • Python 3.0+ 所有版本(因为这个问题从Python 3.0开始就存在)
  • 任何通过 pip install pgl 安装PGL的用户

解决方案

建议修复

将问题行改为使用正确的 builtins 模块导入:

def finalize_options(self):
    _build_ext.finalize_options(self)
    # Prevent numpy from thinking it is still in its setup process:
    import builtins
    builtins.__NUMPY_SETUP__ = False   # ✅ 正确的方式
    import numpy
    self.include_dirs.append(numpy.get_include())

或者直接删除

由于新版本NumPy已经不再需要 __NUMPY_SETUP__ 标志,也可以直接删除这行代码:

def finalize_options(self):
    _build_ext.finalize_options(self)
    import numpy
    self.include_dirs.append(numpy.get_include())

环境信息

  • Python版本: 3.11.4
  • 操作系统: Windows
  • PGL版本: 最新版
  • NumPy版本: 1.26.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions