2

I'm wondering what should someone generally know about Python(the interpreter) when using it in the shell in Linux? This is what I have on Gentoo:

# ls -al /usr/bin/python*; file /usr/bin/python; /usr/bin/python-wrapper --version
lrwxrwxrwx 1 root root    14 Dec 26 04:49 /usr/bin/python -> python-wrapper
lrwxrwxrwx 1 root root     9 Dec 26 04:51 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root  6144 Jan  8 21:22 /usr/bin/python2.7
-rwxr-xr-x 1 root root  1450 Jan  8 21:22 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root     9 Dec 26 04:49 /usr/bin/python3 -> python3.3
-rwxr-xr-x 1 root root 10304 Jan 19 20:37 /usr/bin/python3.3
-rwxr-xr-x 1 root root  1753 Jan 19 20:37 /usr/bin/python3.3-config
-rwxr-xr-x 1 root root   217 Dec 26 04:49 /usr/bin/python-config
lrwxrwxrwx 1 root root    16 Jan  8 21:22 /usr/bin/python-config-2.7 -> python2.7-config
lrwxrwxrwx 1 root root    16 Jan 19 20:37 /usr/bin/python-config-3.3 -> python3.3-config
-rwxr-xr-x 1 root root 10384 Dec 26 05:24 /usr/bin/python-wrapper
/usr/bin/python: symbolic link to `python-wrapper'
Python 3.3.3

So in my case calling python launches version 3.3. On most of the distributions I used, there was more than one version installed. And the latest version isn't backward compatible, and some of its features were backported to the "old" one. I thought the two versions situation was something temporary? Now each time I need to run a Python script I try it with both Python versions just to make sure.

Should I know anything else about using Python in the shell i.e. to run scripts; if so, what? Someone who makes a python script and uses env python in the first line, does he/she mean it should be compatible with both versions? What is the use for the python-wrapper in all that?

3

PEP 394 specifies how Python should be installed and what to use in shebang lines. Use #!/usr/bin/env python2 for Python 2 scripts, and #!/usr/bin/env python3 for Python 3 scripts (and #!/usr/bin/env python if your scripts are compatible with both).

In practice, people have been using #!/usr/bin/env python in shebang lines for years, so if you find a script out there that asks for python, chances are that it's a Python 2 script.

Because Python 2 and Python 3 are not compatible, the two versions will coexist for a long time. They're two different languages that just happen to be similar, but not to the point of being compatible for most programs. Most systems have kept python as Python 2 for now, but Arch Linux has switched python to Python 3; this inconsistency is why PEP 394 recommends using an explicit version in the shebang. Unfortunately, because life isn't simple, some older systems may still not have python2 (Debian wheezy acquired it only in extremis).

Note that while “Python 2.x is legacy, Python 3.x is the present and future of the language” from the point of view of the language designers, there is a huge body of Python 2 code out there. From the point of view of someone who uses third-party libraries (the abundance of which is one of the advantages of Python), the present is Python 2.

As for python-wrapper, it's a wrapper to select between Python implementations. It's yesterday's wrapper; today's wrapper is python-exec. I don't know what they do.

  • I thought the wrapper stuff was generic but it seems like it's Gentoo stuff. They have something similar for ruby. What you say confirms a bit my limited experience i.e. that there's a huge pool of python 2 scripts in the wild and that I would benefit from making a symlink to 2.7 when I try scripts. Thanks! – user44370 Apr 4 '14 at 4:42
  • What would you say the level of maturity/obscurity is for pyenv - are its ilk really so short lived? I had no idea. It's just what I've used since some blog told me what to do a couple years ago. Should I change it do you think? – mikeserv Apr 4 '14 at 10:41
  • @mikeserv I've never heard of pyenv. – Gilles Apr 4 '14 at 11:43
  • Well, maybe it's just the next big thing then - I'll try to be positive. – mikeserv Apr 4 '14 at 11:49
3

Short answer: Use Python 3! Seriously, it's great! I wish all the distros made Python 3 the default.

The question of whether you should use Python 2 or Python 3 is not easy to answer without knowing about the users of your programs and which libraries you use. The practice of making it work on both is great exercise.

Use Python 2 if one of these cases applies:

  1. You use a library that has not yet been ported. There are not many libraries that require Python 2 left.
  2. Your deployment targets or customers are still stuck with Python 2.

For more in-depth answers, see The Python Wiki and a web page about this question.

The line #!/usr/bin/env python (as the other answer says) looks up the first python in your $PATH. Virtualenvs are very popular for isolating projects and Python installations so I use #!/usr/bin/env python instead of simple #!/usr/bin/python so that my virtualenvs work. The Python docs online only mention it in one place and use exactly this invocation. It sounds like you might need a virtualenv for your projects that contains a Python 2 interpreter. the #!/usr/bin/env question has been discussed here and here.

  • I am not a software developer but I understand what you're saying. My angle is really about usage; I get a script somewhere and it's not clear which version to use, and if at that moment I forget that my python is basically symlinked to 3.3, I get a syntax error if the script was made for 2.7. Then I proceed to try and correct the "mistakes" in the script - effectively ruining it for both versions loll. I'm trying to remedy this with knowledge. What should I assume of shebang env python... that it's for the latest? Both? What do you use? – user44370 Apr 3 '14 at 6:32
1

When someone use #!/usr/bin/env python in the first line, he/she means the interpreter to run script is the first one found in $PATH, they don't assume script is compatible with both versions.

If you want to specify python version to use in script, you can do like this:

#!/usr/bin/env python2.7

or

#!/usr/bin/env python3.3
  • 1
    I think it's the job of python-wrapper. I don't use Gentoo much, you can see more details here: gentoo.org/proj/en/Python/python-r1/user-guide.xml – cuonglm Apr 3 '14 at 6:54
  • So in my case, env python gives 3.3. Ok, I didn't know that you could also have something like env python2.7 vs. env python3.3 and that would yield the respective programs. So there is no assumption that 3.3 comes first in PATH then when doing env python without specifying. Is there a way to specify for both or is that even possible to have a script that works with both? Thanks! – user44370 Apr 4 '14 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy