Running a python script under Linux
hansmu at xs4all.nl
Fri Dec 14 14:18:28 CET 2012
On 14/12/12 03:45:18, 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'
> 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
> What am I doing wrong?
You're using an alias. Aliases are not normally exported, and
even if they are (e.g. ksh can be configure to export aliases),
env doesn't recognize them.
What would work, is changing your PATH environment variable
so that the first python on your PATH is the one you want,
or a symlink pointing to it.
The Pythonic way to get what you want, is to be explicit:
If you do that, it will even work in situations where you
can't control PATH, such as CGI scripts and cron jobs.
There are situations where using #!/usr/bin/env makes sense,
but yours isn't one of them.
Hope this helps,
More information about the Python-list