Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make ignores my python bash alias

Tags:

bash

makefile

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?

like image 504
Kev Avatar asked Oct 26 '11 00:10

Kev


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

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.

like image 139
sarnold Avatar answered Oct 21 '22 21:10

sarnold


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

like image 28
nhed Avatar answered Oct 21 '22 22:10

nhed


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.

like image 27
Markus Dutschke Avatar answered Oct 21 '22 21:10

Markus Dutschke