Running a python script under Linux

Andrew Robinson andrew3 at
Fri Dec 14 00:57:30 CET 2012

On 12/13/2012 06:45 PM, Steven D'Aprano wrote:
> I understand this is not exactly a Python question, but it may be of
> interest to other Python programmers, so I'm asking it here instead of a
> more generic Linux group.
> I have a Centos system which uses Python 2.4 as the system Python, so I
> set an alias for my personal use:
> [steve at ando ~]$ which python
> alias python='python2.7'
>          /usr/local/bin/python2.7
> When I call "python" from the command line, it runs under
> Python 2.7 as I expected. So I give the script a hash-bang line:
> #!/usr/bin/env python
> and run the script directly, but instead of getting Python 2.7, it runs
> under Python 2.4 and gives me system errors.
> When I run env directly, it ignores my alias:
> steve at ando ~]$ /usr/bin/env python -V
> Python 2.4.3
> What am I doing wrong?

After seeing the lecture on Bad Ideas ... this might backfire on me.... :)

But ... if you really want to make the alias show up in all bash shells, 
you can put it in your ~/.bashrc file which is executed every time a 
shell is created.
alias python='python2.7'

However, alias expansions do not work in non-interactive shells -- so I 
don't think it will launch with the #!/bin/env technique.

OTOH -- Shell functions DO operate in non-interactive mode, so you could 
add something like:

function python() {
     python3 # whichever version of python you want as default
# eof of function example to add to ~/.bashrc

In a bash shell, you can also do:

function python {
python3 # or 2.7 ...
export -f python

And that would give the same effect as an alias, but funtions can be 
exported to child processes.

It's your system, and we're adults here -- screw it up however you want to.

--Jesus Christ is Lord.

More information about the Python-list mailing list