Running a python script under Linux
jpiitula at ling.helsinki.fi
Fri Dec 14 08:07:44 CET 2012
Steven D'Aprano writes:
> 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'
> When I call "python some_script.py" 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
The alias is known only to the shell for which it is defined. The
shell does nothing with it when it occurs in an argument position. So
env receives just the six-letter string "python" which it resolves as
a program name by walking the file system along your PATH until it
finds the program.
Why not just set your PATH so that the python that is python2.7 is
found first. If this breaks something, your alias would also have
broken that something if aliases worked the way you wanted. Try.
More information about the Python-list