Skip to contentSkip to conversation

python: make python2 and python3 alternatives to python

PATCH In review Dmitry Rozhkov 3 participants 6 messages Apr 13, 2017 – Apr 18, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 python: make python2 and python3 alternatives to python

If only python3 is installed on an image nothing provides /usr/bin/python even though many scripts compatible with both python2 and python3 just state #!/usr/bin/env python in their shebang line.

Make python and python3 recipes provide alternatives for /usr/bin/python and /usr/bin/python-config. By default python2 has higher priority over python3.

---
 meta/recipes-devtools/python/python3_3.5.2.bb | 9 +++++++++
 meta/recipes-devtools/python/python_2.7.13.bb | 7 +++++++
 2 files changed, 16 insertions(+)
meta/recipes-devtools/python/python3_3.5.2.bb +9 -0
index 2ff7c9e..310d43f 100644
@@ -221,4 +221,13 @@ FILES_${PN}-misc = "${libdir}/python${PYTHON_MAJMIN}"
 PACKAGES += "${PN}-man"
 FILES_${PN}-man = "${datadir}/man"
 
+inherit update-alternatives
+
+ALTERNATIVE_PRIORITY = "80"
+ALTERNATIVE_${PN}-core = "python python_config"
+ALTERNATIVE_LINK_NAME[python] = "${bindir}/python"
+ALTERNATIVE_LINK_NAME[python_config] = "${bindir}/python-config"
+ALTERNATIVE_TARGET[python] = "${bindir}/python3"
+ALTERNATIVE_TARGET[python_config] = "${bindir}/python3-config"
+
 BBCLASSEXTEND = "nativesdk"
meta/recipes-devtools/python/python_2.7.13.bb +7 -0
index 4ef9952..0cf7498 100644
@@ -168,4 +168,11 @@ do_install_ptest() {
 PACKAGES += "${PN}-man"
 FILES_${PN}-man = "${datadir}/man"
 
+inherit update-alternatives
+
+ALTERNATIVE_PRIORITY = "100"
+ALTERNATIVE_${PN}-core = "python python_config"
+ALTERNATIVE_LINK_NAME[python] = "${bindir}/python"
+ALTERNATIVE_LINK_NAME[python_config] = "${bindir}/python-config"
+
 BBCLASSEXTEND = "nativesdk"
Signature · 1 line
2.9.3
#1 lore raw
Quoted 8 lines · Dmitry Rozhkov

If only python3 is installed on an image nothing provides /usr/bin/python even though many scripts compatible with both python2 and python3 just state #!/usr/bin/env python in their shebang line.

Make python and python3 recipes provide alternatives for /usr/bin/python and /usr/bin/python-config. By default python2 has higher priority over python3.

This runs against upstream recommendation and common distro practice. And it will break scripts that have not been ported to Python 3. Please fix the scripts instead to refer to python 3 whenever possible.

Alex

#2 lore raw
Quoted 6 lines · Alexander Kanavin
Quoted 5 lines · Dmitry Rozhkov

If only python3 is installed on an image nothing provides /usr/bin/python even though many scripts compatible with both python2 and python3 just state #!/usr/bin/env python in their shebang line.

Quoted 7 lines
Quoted 3 lines

Make python and python3 recipes provide alternatives for /usr/bin/python and /usr/bin/python-config. By default python2 has higher priority over python3.

This runs against upstream recommendation and common distro practice. And it will break scripts that have not been ported to Python 3. Please fix the scripts instead to refer to python 3 whenever possible.

Also, if there are scripts on the image that refer to 'python', yet python 2.7 is not installed into the image, that's a missing RDEPENDS in the scripts recipe.

Alex

#3 lore raw
In reply to Alexander Kanavin
Quoted 4 lines · Alexander Kanavin

This runs against upstream recommendation and common distro practice. And it will break scripts that have not been ported to Python 3. Please fix the scripts instead to refer to python 3 whenever possible.

Agreed. If a script uses /usr/bin/python and gets py3, 99% of the time you'll get errors.

Ross

#4 lore raw
Quoted 11 lines · Ross
Quoted 5 lines · Alexander Kanavin

This runs against upstream recommendation and common distro practice. And it will break scripts that have not been ported to Python 3. Please fix the scripts instead to refer to python 3 whenever possible.

Agreed.  If a script uses /usr/bin/python and gets py3, 99% of the time you'll get errors.

I've just checked PEP-349. It recommends that

"- The more general python command should be installed whenever any version of Python 2 is installed and should invoke the same version of Python as the python2 command (however, note that some distributions have already chosen to have python implement the python3 command; see the Rationale and Migration Notes below)."

The proposed patch doesn't violate this recommendation, because "whenever" Python 2 is installed the python command still invokes python2.

Also, there are other recommendations like

"- In order to tolerate differences across platforms, all new code that needs to invoke the Python interpreter should not specify python , but rather should specify either python2 or python3 (or the more specific python2.x and python3.x versions; see the Migration Notes ). This distinction should be made in shebangs, when invoking from a shell script, when invoking via the system() call, or when invoking in any other context.
- One exception to this is scripts that are deliberately written to be source compatible with both Python 2.x and 3.x. Such scripts may continue to use python on their shebang line without affecting their portability."

If I understood correctly OE-core is not a distribution, but a tool to create "a custom one". I would rather allow people to create distributions where python3 is the default like in Arch Linux.

BR,
Dmitry

#5 lore raw
Quoted 8 lines · Dmitry Rozhkov

I've just checked PEP-349. It recommends that

"- The more general python command should be installed whenever any version of Python 2 is installed and should invoke the same version of Python as the python2 command (however, note that some distributions have already chosen to have python implement the python3 command; see the Rationale and Migration Notes below)."

Quoted 4 lines

The proposed patch doesn't violate this recommendation, because "whenever" Python 2 is installed the python command still invokes python2.

Only out of the box: you can install py2 and py3 and set the alternatives so that python is python3.

If I understood correctly OE-core is not a distribution, but a tool to

Quoted 2 lines

create "a custom one". I would rather allow people to create distributions where python3 is the default like in Arch Linux.

Personally I feel the PEP is an attempt to limit the damage caused by Arch ignoring the Python maintainers, and I'd prefer not to spread the impression that 'python' could be Python 3. Unless you code incredibly carefully it's not simple to have a script that doesn't care if it runs under Py2 or Py3.

Ross