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

求Fish Shell中python -m模块调用的TAB自动补全工具

Fish Shell 下 python -m 模块自动补全方案

一、本地模块补全(仅扫描当前目录)

直接在 Fish 的补全配置里添加逻辑,扫描当前目录下可作为模块的文件和目录:

  1. 创建/编辑补全文件:~/.config/fish/completions/python.fish
  2. 写入以下内容:
complete -c python -n '__fish_seen_subcommand_from m' -a '(
    # 扫描当前目录下的.py文件(去掉后缀)
    ls -1 *.py 2>/dev/null | sed "s/\.py$//"
    # 扫描当前目录下含__init__.py的子目录
    find . -maxdepth 1 -type d \( ! -name . \) -exec test -f "{}/__init__.py" \; -print | sed "s/^\.\///"
)' -d "Local Python modules for python -m"

保存后重启 Fish 或执行 source ~/.config/fish/completions/python.fish 即可生效,输入python -m按Tab就能看到当前目录的本地模块补全。

二、全局模块补全(扫描所有可导入的模块)

如果需要全局模块的补全,利用 Python 自身的模块枚举能力,修改上述补全文件为:

complete -c python -n '__fish_seen_subcommand_from m' -a '(python -c "import pkgutil; print('\''\n'\''.join([name for _, name, _ in pkgutil.iter_modules()]))")' -d "Global Python modules for python -m"

这个方案会调用 Python 列出所有可导入的顶层模块,包括标准库和第三方安装的包,补全时会加载这些列表。

如果想要同时支持本地和全局模块,把两个命令的输出合并即可:

complete -c python -n '__fish_seen_subcommand_from m' -a '(
    ls -1 *.py 2>/dev/null | sed "s/\.py$//"
    find . -maxdepth 1 -type d \( ! -name . \) -exec test -f "{}/__init__.py" \; -print | sed "s/^\.\///"
    python -c "import pkgutil; print('\''\n'\''.join([name for _, name, _ in pkgutil.iter_modules()]))"
)' -d "Local and global Python modules for python -m"

内容的提问来源于stack exchange,提问作者EPrivat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 16:32:36