2

I am distributing a python program and get complaints that in environments where the link /usr/bin/python opints to python3 people have to edit the shebang line or call the script with python2 explicitly. I could write python2 in the shebang line but I fear incompatibilities.

Is python2 present in every (or at least every Linux) environment?

I am using autotools, so I also appreciate autotools magic tipps.

steffen
  • 8,572
  • 11
  • 52
  • 90
  • No. Most distributions do *not* use the name `python2`. Most distributions use `python` instead, using `python3` for a 3.x release. Those that use `python` to mean `python3` are going against the tide. – Martijn Pieters Jul 26 '13 at 09:44
  • all the linux distros i have used so far have `python` for `python2.x` and `python3` for `python3.x`, and then you can add more python distributions like `python27` for `python2.7.5`. – abhishekgarg Jul 26 '13 at 09:52
  • @MartijnPieters abhishekgarg: The affected distros are gentoo and arch. I will recommend to set the links as you described. – steffen Jul 26 '13 at 10:01
  • Have a look at the [`AM_PATH_PYTHON`](http://www.gnu.org/software/automake/manual/automake.html#Python) macro. – Brett Hale Jul 29 '13 at 03:08

2 Answers2

3

I find /usr/bin/env isn't a perfect, granular Python detector. However, I just found it is possible to limit AM_PATH_PYTHON to major version 2.

Look through your aclocal.m4 file for the variable "_AM_PYTHON_INTERPRETER_LIST". In my Mac environment, the list of Python binaries looks like this:

  m4_define_default([_AM_PYTHON_INTERPRETER_LIST],
[python python2 python3 python3.3 python3.2 python3.1 python3.0 python2.7 dnl
 python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python2.0])

If you trim the list by issuing this call immediately before AM_PATH_PYTHON, you can limit the checked-for Python binaries. So, this effectively limits you to python2.6+:

m4_define_default([_AM_PYTHON_INTERPRETER_LIST],[python2 python2.7 python2.6])
AM_PATH_PYTHON(2.6)
1

Python2 binary is present on nearly every linux distro. However, it doesn't have to be named "python" and be in /usr/bin/python - eg. on Arch Linux /usr/bin/python points to python3.3. The most safe way to get this binary is /usr/bin/env python2, as env binary is nearly always here. Just type in the first line of executable:

#!/usr/bin/env python2
"""rest of yours python code here..."
psorek
  • 65
  • 1
  • 7