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

如何让/bin/sh将python识别为python3以适配Makefile命令执行?

问题:让/bin/sh识别python为python3

背景

我正在使用一个包含Makefile的软件包,这些Makefile通过/bin/sh执行Python命令(Makefile.common里设置了SHELL = /bin/sh),命令格式固定为python some_script.py ...。所以这里必须使用/bin/sh,没法换其他shell。

问题详情

我已经安装了python-is-python3:

axel@axel-ThinkPad-X250:~$ sudo apt install python-is-python3
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python-is-python3 is already the newest version (3.9.2-2).
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

同时在~/.bash_aliases和~/.profile里设置了alias python='python3',这个别名在/bin/bash里正常生效:

axel@axel-ThinkPad-X250:~$ /bin/bash
axel@axel-ThinkPad-X250:~$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
axel@axel-ThinkPad-X250:~$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

但这个别名在/bin/sh里完全没用:

axel@axel-ThinkPad-X250:~$ /bin/sh
$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python
/bin/sh: 2: python: not found
$ 

请问怎么让/bin/sh把python识别成python3?


解决方案

1. 修复系统级软链接(最直接方法)

python-is-python3的核心作用是在/usr/bin下创建python指向python3的软链接。先检查这个链接是否存在:

ls -l /usr/bin/python

如果输出类似lrwxrwxrwx 1 root root 9 ... /usr/bin/python -> python3,说明链接正常;如果不存在,重新安装修复:

sudo apt reinstall python-is-python3

若重装后仍无效,手动创建软链接:

sudo ln -s /usr/bin/python3 /usr/bin/python

这样无论用/bin/sh还是其他shell,输入python都会调用python3。

2. 为什么别名在/bin/sh里无效?

Debian/Ubuntu系统里的/bin/sh默认是dash而非bash,dash不读取~/.bash_aliases这类bash专属配置。更关键的是,make执行shell命令时用的是非交互式非登录shell,这类shell不会加载任何用户配置里的别名——所以靠别名解决根本行不通。

3. 临时用户级解决(不修改系统)

如果不想改动系统级文件,可以在用户目录下创建软链接,然后临时修改PATH:

# 创建用户专属bin目录(不存在则新建)
mkdir -p ~/bin
# 创建python指向python3的软链接
ln -s /usr/bin/python3 ~/bin/python
# 执行make前把~/bin加到PATH最前端
PATH=~/bin:$PATH make

4. 修改Makefile(备选,若允许)

如果软件包允许修改Makefile,可以直接把所有python替换成python3,或者在Makefile里定义变量:

PYTHON := python3

然后把命令里的python替换成$(PYTHON)。不过你提到命令格式固定,这个方法可能不适用,仅作参考。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 05:55:15