与系统库同名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     │
╰───────┴───────╯
相关推荐
Jackey_Song_Odd2 小时前
Part 1:Python语言核心 - 内建数据类型
开发语言·python
切糕师学AI2 小时前
编程语言 Erlang 简介
开发语言·erlang
sycmancia2 小时前
C++——C++中的类型识别
开发语言·c++
带娃的IT创业者2 小时前
WeClaw WebSocket 连接中断诊断:从频繁掉线到稳定长连的优化之路
python·websocket·网络协议·php·fastapi·实时通信
还是大剑师兰特2 小时前
Vue3 按钮切换示例(启动 / 关闭互斥显示)
开发语言·javascript·vue.js
GinoWi2 小时前
Chapter 5 Python中的元组
python
星空露珠2 小时前
迷你世界UGC3.0脚本Wiki角色模块管理接口 Actor
开发语言·数据库·算法·游戏·lua
我星期八休息2 小时前
深入理解哈希表
开发语言·数据结构·c++·算法·哈希算法·散列表
IpdataCloud2 小时前
指纹浏览器为什么要自建IP检测?基于IP数据云离线库的架构实践
数据库·网络协议·tcp/ip·架构·edge浏览器