Pergunta

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.

Foi útil?

Solução

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)

Outras dicas

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..."
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top