Make ignores my python bash alias

On my CentOS 5.5 server, Python 2.4 and Python 2.7 are installed (prior to /opt/python2.7.2 ). In my ~/.bash_profile , I have two aliases pointing to my Python 2.7 installation, 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

A symbolic link was also created there:

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

I have a Makefile that has the following lines:

  pythonbuild:
         python setup.py build

To my surprise, I found that Python 2.4 is being called, not Python 2.7.

I need to explicitly specify python2.7 :

  pythonbuild:
         python2.7 setup.py build

Are bash aliases ignored by make ? I assume make uses PATH to find the first python executable (which maybe Python 2.4 is)?

+7
source share
3 answers

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). 

Although you can use something like SHELL=/bin/bash -O expand_aliases in your Makefile , I think that keeping the explicit dependency on the newer Python in your Makefile much better than keeping the dependencies hidden in your user ~/.bash_profile file .

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

 pythonbuild: $(PYTHON) setup.py build 

in your rules.

The best part is that 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, you need to update only one line in the Makefile .

+6
source

aliases are usually used only by interactive shells

Pay attention only to this, I think make does not always invoke the shell. It is best to specify which paths you want to use

+1
source

Workaround with grep and awk:

The advantage of this solution is that if I change the alias to ~ / .bash_profil or ~ / .bashrc, it is also automatically accepted by my makefile.

Description:

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

.bashrc

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

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

Makefile

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

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

Note:

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

+1
source

All Articles