与系统库同名python脚本文件引起的奇怪错误及其解决

在测试daft python模块的时候,遇到一个奇怪错误。

编写daft1.py文件

python 复制代码
import daft

from daft import Session

# create a session
sess = Session()

# create temp tables
sess.create_temp_table("T", daft.from_pydict({ "a": [ 0, 1 ] }))
sess.create_temp_table("S", daft.from_pydict({ "b": [ 1, 0 ] }))

# execute sql
sess.sql("SELECT * FROM T, S").show()
"""
╭───────┬───────╮
│ a     ┆ b     │
│ ---   ┆ ---   │
│ Int64 ┆ Int64 │
╞═══════╪═══════╡
│ 0     ┆ 1     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1     ┆ 1     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 0     ┆ 0     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1     ┆ 0     │
╰───────┴───────╯
"""

执行报错

复制代码
root@6ae32a5ffcde:/par# python3 pip.pyz install daft[sql] --break-system-packages
Looking in indexes: https://mirrors.aliyun.com/pypi/simple
Collecting daft[sql]
  Downloading https://mirrors.aliyun.com/pypi/packages/2f/2c/7663db73b74507c87a06f669d59286cd4889c3889b2beeff264346e9ec01/daft-0.7.4-cp310-abi3-manylinux_2_24_x86_64.whl (62.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.0/62.0 MB 21.3 MB/s eta 0:00:00
Requirement already satisfied: pyarrow<24.0.0,>=8.0.0 in /usr/local/lib/python3.11/dist-packages (from daft[sql]) (21.0.0)
Requirement already satisfied: fsspec<2026.3.0 in /usr/local/lib/python3.11/dist-packages (from daft[sql]) (2025.10.0)
Requirement already satisfied: tqdm<4.68.0 in /usr/local/lib/python3.11/dist-packages (from daft[sql]) (4.67.1)
Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from daft[sql]) (25.0)
Collecting connectorx<0.5.0,>=0.4.4 (from daft[sql])
  Downloading https://mirrors.aliyun.com/pypi/packages/9a/37/d6dffc001562b7109c8a18604f5a52445187c6bede23e5b737248d172f15/connectorx-0.4.5-cp311-cp311-manylinux_2_28_x86_64.whl (43.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.8/43.8 MB 21.8 MB/s eta 0:00:00
Collecting sqlalchemy<2.1.0 (from daft[sql])
  Downloading https://mirrors.aliyun.com/pypi/packages/21/dd/3b7c53f1dbbf736fd27041aee68f8ac52226b610f914085b1652c2323442/sqlalchemy-2.0.48-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (3.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 20.6 MB/s eta 0:00:00
Collecting sqlglot<28.11.0 (from daft[sql])
  Downloading https://mirrors.aliyun.com/pypi/packages/55/ff/5a768b34202e1ee485737bfa167bd84592585aa40383f883a8e346d767cc/sqlglot-28.10.1-py3-none-any.whl (597 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 597.1/597.1 kB 13.3 MB/s eta 0:00:00
Collecting greenlet>=1 (from sqlalchemy<2.1.0->daft[sql])
  Downloading https://mirrors.aliyun.com/pypi/packages/72/83/3e06a52aca8128bdd4dcd67e932b809e76a96ab8c232a8b025b2850264c5/greenlet-3.3.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (594 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 594.2/594.2 kB 11.9 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions>=4.6.0 in /usr/local/lib/python3.11/dist-packages (from sqlalchemy<2.1.0->daft[sql]) (4.15.0)
Installing collected packages: sqlglot, greenlet, daft, connectorx, sqlalchemy
Successfully installed connectorx-0.4.5 daft-0.7.4 greenlet-3.3.2 sqlalchemy-2.0.48 sqlglot-28.10.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
root@6ae32a5ffcde:/par# python3 daft1.py
Traceback (most recent call last):
  File "/par/daft1.py", line 1, in <module>
    import daft
  File "/usr/local/lib/python3.11/dist-packages/daft/__init__.py", line 58, in <module>
    from daft.catalog import (
  File "/usr/local/lib/python3.11/dist-packages/daft/catalog/__init__.py", line 48, in <module>
    from daft.dataframe import DataFrame
  File "/usr/local/lib/python3.11/dist-packages/daft/dataframe/__init__.py", line 3, in <module>
    from .dataframe import DataFrame, GroupedDataFrame
  File "/usr/local/lib/python3.11/dist-packages/daft/dataframe/dataframe.py", line 36, in <module>
    from daft.execution.native_executor import NativeExecutor
  File "/usr/local/lib/python3.11/dist-packages/daft/execution/native_executor.py", line 16, in <module>
    from daft.event_loop import get_or_init_event_loop
  File "/usr/local/lib/python3.11/dist-packages/daft/event_loop.py", line 3, in <module>
    import asyncio
  File "/usr/lib/python3.11/asyncio/__init__.py", line 16, in <module>
    from .queues import *
  File "/usr/lib/python3.11/asyncio/queues.py", line 218, in <module>
    class PriorityQueue(Queue):
  File "/usr/lib/python3.11/asyncio/queues.py", line 227, in PriorityQueue
    def _put(self, item, heappush=heapq.heappush):
                                  ^^^^^^^^^^^^^^
AttributeError: module 'heapq' has no attribute 'heappush'
root@6ae32a5ffcde:/par#python3 --version
Python 3.11.2

heapq是个系统模块,怎么会缺少属性?我认为是daft的bug, 还到它的github存储库去提issue。

今天执行另外一个python脚本,出现同样的错误,这就和daft无关了,在当前目录搜索,发现我自己的一个脚本名为heapq.py,它引用了系统heapq。

结果python在执行的时候,以为它是我的heapq模块源文件,当然找不到heappush属性。

知道了错因,解决很简单,把自己的脚本改名,改为一个不和系统模块重名的就好了。

现在执行结果如下

复制代码
root@6ae32a5ffcde:/par# python3 daft1.py
╭───────┬───────╮
│ a     ┆ b     │
│ ---   ┆ ---   │
│ Int64 ┆ Int64 │
╞═══════╪═══════╡
│ 0     ┆ 1     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1     ┆ 1     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 0     ┆ 0     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 1     ┆ 0     │
╰───────┴───────╯
相关推荐
这个DBA有点耶4 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
用户7783366132116 小时前
用 React Hook 封装搜索数据:useSerp 的防抖、缓存与错误处理
python·api
这个DBA有点耶7 小时前
从MySQL 5.7到8.0:JSON查询性能差在哪?虚拟列索引vs多值索引怎么选
数据库·mysql·架构
胡写代码7 小时前
数据库审计字段,别再每张表各写一套了——我统一成这 6 个字段
数据库
65岁退休Coder10 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
SelectDB11 小时前
为什么 JSON 正在成为分析数据库新的竞争点?
数据库·json·agent
ClouGence12 小时前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
发霉的馒头12 小时前
ORA-00845: MEMORY_TARGET not supported on this system的解决方法
数据库
ikun_文12 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes12 小时前
Python 基础语法(二):字符串与常用操作
python