[PATCH] Handling of scripts / substitution of python executable path
Hi! For a long time, I have been annoyed by distutils behavior concerning "scripts": I always put #!/usr/bin/env python into the first line in order to let the incredibly useful "env" program start the right python version. I know that it is quite evil to hardcode /usr/bin/python or /usr/local/bin/python; I have seen dozens of #! hacks for finding e.g. perl, and I was delighted to find /usr/bin/env, which solves the problem once and for all. (And yes - it is always in /usr/bin/! ;-) ) Now distutils tries to be intelligent and "destroys" my nice #! lines, which can be quite evil in complex setups, e.g. when you share your home directory via NFS (or rsync/unison) between several environments with different python installations. Furthermore, we are using the "module" system here at our university, so that I can dynamically choose between half a dozen python versions ("module" manages your PATH variables). Replacing the python path turns nice, pure python scrips into platform-specific programs as you can see here: meine@kogspc12:~ head -n1 ~/vigra/interactive/build/scripts-2.4/pyterm #!/software/python-2.4.4/SuSE-9.0/bin/python Note the SuSE-9.0 exec-prefix in the path; we are using several Linux and Solaris versions here: meine@kogspc12:~/tmp/vi3build -> ls -1 /software/python-2.4.4/*/bin/python /software/python-2.4.4/SuSE-10.0/bin/python* /software/python-2.4.4/SuSE-9.0/bin/python* /software/python-2.4.4/SunOS-5.8/bin/python* I see that distutils as it is now does the right thing * on Windows systems, * on any system where /usr/bin/env is missing, or * when the source file has a #! with a broken path. What I propose is a minimal invasive change which keeps /usr/bin/env iff it is in the #! line *and* exists on the current system. (A more brave change would be to always use /usr/bin/env if it exists, but I think that is a topic open for discussion.) Attached you'll find a patch which implements this; I did not yet update tests/test_build_scripts.py however. Comments? (I first posted this to distutils-sig but was told that distutils is a bit neglected there, so I decided to try to push these simple patches in via python-dev.) Ciao, / / /--/ / / ANS
On Fri, 23 Feb 2007 15:36:50 +0100, Hans Meine
Hi!
[snip - distutils should leave #!/usr/bin/env python alone]
Comments? (I first posted this to distutils-sig but was told that distutils is a bit neglected there, so I decided to try to push these simple patches in via python-dev.)
How about a distutils installation command line option to tell it to use this behavior? People with unusual environments can select it. Jean-Paul
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Feb 23, 2007, at 9:55 AM, Jean-Paul Calderone wrote:
On Fri, 23 Feb 2007 15:36:50 +0100, Hans Meine
wrote: Hi!
[snip - distutils should leave #!/usr/bin/env python alone]
Comments? (I first posted this to distutils-sig but was told that distutils is a bit neglected there, so I decided to try to push these simple patches in via python-dev.)
How about a distutils installation command line option to tell it to use this behavior? People with unusual environments can select it.
This would be best I think. There's a related problem here. Many operating systems (e.g. various Linux distros) provide system scripts written in Python and they incorrectly use /usr/bin/env. The problem is that if you have your own version of Python earlier in your $PATH, you can break these scripts. This happens when say your /usr/local/ bin/python doesn't have the packages that your /usr/bin/python expects. I've hit this quite a few times on Gentoo for example. In these cases, you really do want the #! line to point to the OS's version of Python rather than whatever random installation is on your $PATH. - -Barry -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (Darwin) iQCVAwUBRd8//XEjvBPtnXfVAQLZKQP+NvcjMIT4tCZtOIPWo8Kj1JMp5iX0Nqib BBXPgDtncJ6VvITclbYLAmitPhwkDddHXgNIHoj2lY9HJildfOkDQxVpDRTvrEvd hlIW4z7S6SmlrvxJYSB2jGSpkNkVRo8UpZKsmU0BWFBE9t6Tnw+ofItuKIF1TlWS unSo4LceXFo= =wiLG -----END PGP SIGNATURE-----
Hans Meine schrieb:
For a long time, I have been annoyed by distutils behavior concerning "scripts": I always put #!/usr/bin/env python into the first line in order to let the incredibly useful "env" program start the right python version.
I know that it is quite evil to hardcode /usr/bin/python
No. The current distutils behaviour is very deliberate, intentional, and has undergone a number of iterations to arrive at this point. While it is true that you would normally use /usr/bin/env for scripts that you *distribute*, it's not true that you should use that for scripts that you *install*. Instead, the script that you install will likely depend on the specific installation of Python: the script will likely use modules that are only installed in a single location. So the installed scripts need to hard-code the path to the interpreter, or else they break if somebody changes the path and suddenly picks up a different Python. Notice that it may not just be that the Python is a different version (on which the script won't work); it may also be that the it won't work on a different installation of the *same* Python version, since it requires libraries not available in this other installation. So the default must stay as it is. Regards, Martin
participants (4)
-
"Martin v. Löwis"
-
Barry Warsaw
-
Hans Meine
-
Jean-Paul Calderone