[arch-general] simple trick to make dealing with python 2/3 scripts easier
Lots of python scripts still use #!/usr/bin/python instead of explicitly stating which version of python to use. Here's quick trick to make running various python version 2 or 3 scripts easier: remove the /usr/bin/python symlink and replace with this shell script: #!/usr/bin/env bash exec /usr/bin/"${PYTHON:-python3}" "$@" Now you can set the PYTHON environment variable to be either python2 or python3, depending on which version of python the script expects. Just don't set PYTHON=python, or you'll get a recursive loop! The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
Just put your script in /usr/local/bin -- -- дамјан
Damjan wrote:
The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
Just put your script in /usr/local/bin
But then some scripts use #!/usr/bin/python. Putting it in /usr/local/bin would only work if the script uses #!/usr/local/bin/python or #!/usr/bin/env python.
Ben Booth wrote:
Damjan wrote:
The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
Just put your script in /usr/local/bin
But then some scripts use #!/usr/bin/python. Putting it in /usr/local/bin would only work if the script uses #!/usr/local/bin/python or #!/usr/bin/env python.
Maybe I'll submit a feature request to the python package maintainer to see if they think it's a good idea.
Ben Booth wrote:
Maybe I'll submit a feature request to the python package maintainer to see if they think it's a good idea.
I submitted a feature request in case anyone's interested: https://bugs.archlinux.org/index.php?do=details&action=details.addvote&task_id=31179
On 08/17/2012 04:14 PM, Ben Booth wrote:
Ben Booth wrote:
Maybe I'll submit a feature request to the python package maintainer to see if they think it's a good idea.
I submitted a feature request in case anyone's interested:
https://bugs.archlinux.org/index.php?do=details&action=details.addvote&task_id=31179
Don't know if you did this by accident -- and not a huge deal -- but you shouldn't have included the vote action in the link.
Matthew Monaco <dgbaley27@0x01b.net> wrote:
On 08/17/2012 04:14 PM, Ben Booth wrote:
Don't know if you did this by accident -- and not a huge deal -- but you shouldn't have included the vote action in the link.
Oops, my mistake. In any case, the request was rejected. :(
genius! :) maybe a cronjob? (every 24 hours overwrite the script) (silly one) maybe a startup overwrite? or an aur package? (with interactive question if PYTHON is not set!) -- (\_ /) copy the bunny to your profile (0.o ) to help him achieve world domination. (> <) come join the dark side. /_|_\ (we have cookies.) On Fri, Aug 17, 2012 at 9:09 PM, Ben Booth <benwbooth@gmail.com> wrote:
Lots of python scripts still use #!/usr/bin/python instead of explicitly stating which version of python to use. Here's quick trick to make running various python version 2 or 3 scripts easier:
remove the /usr/bin/python symlink and replace with this shell script:
#!/usr/bin/env bash exec /usr/bin/"${PYTHON:-python3}" "$@"
Now you can set the PYTHON environment variable to be either python2 or python3, depending on which version of python the script expects. Just don't set PYTHON=python, or you'll get a recursive loop! The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
but which of is going to be executed? /usr/bin or /usr/local/bin? -- (\_ /) copy the bunny to your profile (0.o ) to help him achieve world domination. (> <) come join the dark side. /_|_\ (we have cookies.) On Fri, Aug 17, 2012 at 9:09 PM, Ben Booth <benwbooth@gmail.com> wrote:
Lots of python scripts still use #!/usr/bin/python instead of explicitly
stating which version of python to use. Here's quick trick to make running various python version 2 or 3 scripts easier:
remove the /usr/bin/python symlink and replace with this shell script:
#!/usr/bin/env bash exec /usr/bin/"${PYTHON:-python3}" "$@"
Now you can set the PYTHON environment variable to be either python2 or python3, depending on which version of python the script expects. Just don't set PYTHON=python, or you'll get a recursive loop! The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
but which of is going to be executed? /usr/bin or /usr/local/bin?
/usr/local/bin is before /usr/bin in your PATH environment variable, so the shell will first look there for programs. Check "echo $PATH" to see what your path is. -- дамјан
well, you're right... :P so, no cronjobs XD -- (\_ /) copy the bunny to your profile (0.o ) to help him achieve world domination. (> <) come join the dark side. /_|_\ (we have cookies.) On Sat, Aug 18, 2012 at 1:36 AM, Damjan <gdamjan@gmail.com> wrote:
but which of is going to be executed?
/usr/bin or /usr/local/bin?
/usr/local/bin is before /usr/bin in your PATH environment variable, so the shell will first look there for programs. Check "echo $PATH" to see what your path is.
-- дамјан
On Fri, Aug 17, 2012 at 11:09:06AM -0700, Ben Booth wrote:
remove the /usr/bin/python symlink and replace with this shell script:
#!/usr/bin/env bash exec /usr/bin/"${PYTHON:-python3}" "$@"
Bravo! I approve. This solution is 0.99 times as good as the option to just not have Python 3
On 2012/8/17 Ben Booth <benwbooth@gmail.com> wrote:
Lots of python scripts still use #!/usr/bin/python instead of explicitly stating which version of python to use. Here's quick trick to make running various python version 2 or 3 scripts easier:
remove the /usr/bin/python symlink and replace with this shell script:
#!/usr/bin/env bash exec /usr/bin/"${PYTHON:-python3}" "$@"
Now you can set the PYTHON environment variable to be either python2 or python3, depending on which version of python the script expects. Just don't set PYTHON=python, or you'll get a recursive loop! The only problem with this approach is that /usr/bin/python is owned by the python package, so if you upgrade the python package it might create problems. Any one know of some way to work around this problem?
Run your scripts using "python2 script.py" or use setuptools that rewrites shebang automatically. Rémy.
In case anyone's still interested in this, I found a pacman.conf option called NoExtract, which lets you tell pacman not to overwrite certain files and directories in the filesystem. So you could add the following line to /etc/pacman.conf: NoExtract = /usr/bin/python which would prevent pacman from overwriting the custom /usr/bin/python script. Here is the most recent version of the /usr/bin/python script: #!/usr/bin/env bash test "$PYTHON" != "python2" -a "$PYTHON" != "python3" && unset PYTHON exec /usr/bin/"${PYTHON:-python3}" "$@" This version does some input checking to make sure the only valid options for the PYTHON environment variable are "python2" or "python3". Ben
Ben Booth wrote:
In case anyone's still interested in this, I found a pacman.conf option called NoExtract, which lets you tell pacman not to overwrite certain files and directories in the filesystem. So you could add the following line to /etc/pacman.conf:
NoExtract = /usr/bin/python
Actually it should be NoExtract = usr/bin/python without the root slash. sorry for the typo. Ben
Thank you Ben, people who follow up on an issue they have found a solution for are good people. On Aug 23, 2012 1:35 PM, "Ben Booth" <bwbooth@lbl.gov> wrote:
Ben Booth wrote:
In case anyone's still interested in this, I found a pacman.conf option called NoExtract, which lets you tell pacman not to overwrite certain files and directories in the filesystem. So you could add the following line to /etc/pacman.conf:
NoExtract = /usr/bin/python
Actually it should be
NoExtract = usr/bin/python
without the root slash. sorry for the typo.
Ben
participants (8)
-
Anthony ''Ishpeck'' Tedjamulia
-
Ben Booth
-
Ben Booth
-
Damjan
-
Matthew Monaco
-
Patrick Murphy
-
Rémy Oudompheng
-
Δημήτρης Ζέρβας