make 忽略我的 python bash 别名

发布于 2024-12-12 02:42:57 字数 772 浏览 0 评论 0原文

我的 CentOS 5.5 服务器同时安装了 Python 2.4 和 Python 2.7(到 /opt/python2.7.2)。在我的 ~/.bash_profile 中,我有两个别名指向我的 Python 2.7 安装,并且我的 PATH 配置为:

alias python=/opt/python2.7.2/bin/python
alias python2.7=/opt/python2.7.2/bin/python
PATH=$PATH:/opt/python2.7/bin

我还创建了一个符号链接:

ln -sf /opt/python2.7.2/bin/python /usr/bin/python2.7

我有一个 < code>Makefile 其中包含以下几行:

pythonbuild:
        python setup.py build

令我惊讶的是,我发现正在调用 Python 2.4,而不是 Python 2.7。

我必须显式指定 python2.7

pythonbuild:
        python2.7 setup.py build

make 会忽略 bash 别名吗?我猜 make 使用 PATH 来定位第一个 python 可执行文件(恰好是 Python 2.4)?

My CentOS 5.5 server has both Python 2.4 and Python 2.7 installed (to /opt/python2.7.2). In my ~/.bash_profile I have two aliases pointing to my Python 2.7 install and my PATH configured as:

alias python=/opt/python2.7.2/bin/python
alias python2.7=/opt/python2.7.2/bin/python
PATH=$PATH:/opt/python2.7/bin

There's also a symbolic link I created as well:

ln -sf /opt/python2.7.2/bin/python /usr/bin/python2.7

I have a Makefile which has the following lines:

pythonbuild:
        python setup.py build

To my surprise I found that Python 2.4 is being invoked and not Python 2.7.

I have to explicitly specify python2.7:

pythonbuild:
        python2.7 setup.py build

Are bash aliases ignored by make? I am guessing make uses PATH to locate the first python executable (which happens to be Python 2.4) instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-12-19 02:42:57

来自 bash(1)

   Aliases are not expanded when the shell is not interactive,
   unless the expand_aliases shell option is set using shopt
   (see the description of shopt under SHELL BUILTIN COMMANDS
   below).

虽然您可能能够在 Makefile 中使用 SHELL=/bin/bash -O Expand_aliases 之类的内容,但我认为在 Makefile 中保持对较新 Python 的显式依赖比将依赖隐藏在用户 ~/. bash_profile 文件。

相反,将 PYTHON=/opt/python2.7/bin/python 放入 Makefile 中,然后您可以

pythonbuild:
    $(PYTHON) setup.py build

在规则中使用:。

最好的部分是您可以轻松更改在命令行上使用的 Python 解释器:

make PYTHON=/tmp/python-beta/bin/python pythonbuild

如果您将其部署到另一个站点,则只需 Makefile 中的一行需要有待更新。

From bash(1):

   Aliases are not expanded when the shell is not interactive,
   unless the expand_aliases shell option is set using shopt
   (see the description of shopt under SHELL BUILTIN COMMANDS
   below).

While you might be able to use something like SHELL=/bin/bash -O expand_aliases in your Makefile, I think keeping an explicit dependency upon the newer Python in your Makefile is much better than keeping the dependency hidden in your user ~/.bash_profile file.

Instead, put PYTHON=/opt/python2.7/bin/python into your Makefile, and then you can just use:

pythonbuild:
    $(PYTHON) setup.py build

in your rules.

The best part is you can easily change which Python interpreter you use on the command line:

make PYTHON=/tmp/python-beta/bin/python pythonbuild

If you deploy it to another site, it is just one line in the Makefile that needs to be updated.

月下凄凉 2024-12-19 02:42:57

别名通常仅由交互式 shell 使用

,请注意,我认为 make 并不总是调用 shell
最好的办法是明确你想要使用的路径

aliases are typically just used by interactive shells

Note only that, I think that make does not always invoke the shell
Your best bet is to be explicit about the paths you want to use

半暖夏伤 2024-12-19 02:42:57

使用 grep 和 awk 的解决方法:

此解决方案的优点是,如果我更改 ~/.bash_profil 或 ~/.bashrc 中的别名,我的 makefile 也会自动采用它。

描述:

我想在我的 makefile 中使用别名 lcw,它的定义与我的 ~/.bashrc 文件中类似。

.bashrc

...
alias lcw='/mnt/disk7/LCW/productiveVersion/lcw.out'
...

我还使用其他解决方案中提供的变量定义,但我使用 grep 和 awk 直接从 bashrc 读取其值。

ma​​kefile

LCW= $(shell grep alias\ lcw= ~/.bashrc | awk -F"'" '{print $2}')

.PHONY: std
std:
    $(LCW)

如您所见,lcw 别名是由 makefile 中的命令 $(LCW) 调用的。

注意:

我的解决方案假设 bashrc 中的别名是在“ ”字符内定义的。

Workaround with grep and awk:

The advantage of this solution is that if I change the alias in the ~/.bash_profil or ~/.bashrc, it is automatically adopted by my makefile as well.

Description:

I want to use the alias lcw in my makefile, which is defined like in my ~/.bashrc file.

.bashrc

...
alias lcw='/mnt/disk7/LCW/productiveVersion/lcw.out'
...

I use also the definition of a varialble as presented in the other solutions, but I directly read its value from the bashrc by using grep and awk.

makefile

LCW= $(shell grep alias\ lcw= ~/.bashrc | awk -F"'" '{print $2}')

.PHONY: std
std:
    $(LCW)

As you see the lcw alias is called by the command $(LCW) from the makefile.

Note:

My solution assumes that the alias in the bashrc is defined within ' ' characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文