From bd2a72d178dde36b76bf6d7cbc0d7879bd9dfe57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 14 Apr 2020 14:33:42 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20docs:=20Minor=20changes=20for=20?= =?UTF-8?q?version=20specific=20imports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also fixed a section that got misplaced by the PR. --- docs/plugins/python3_migration.rst | 31 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/docs/plugins/python3_migration.rst b/docs/plugins/python3_migration.rst index 37ce935af..a01de01b3 100644 --- a/docs/plugins/python3_migration.rst +++ b/docs/plugins/python3_migration.rst @@ -218,7 +218,7 @@ You can read more about this specific issue in the corresponding section of the `Python porting guide `__ and also in the `cheat sheet `__. -.. _sec-plugins-python3-pitfalls-imports: +.. _sec-plugins-python3-pitfalls-absolute-imports: Absolute imports ................ @@ -242,11 +242,18 @@ future import: from __future__ import absolute_imports -Explicit imports -................ +You can read more about this specific issue in the +`cheat sheet `__ and also in +`the book `__. -Sometimes it is necessary to use an import statement that is explicitly related to a specific python version. -You can do this in differnt ways: +.. _sec-plugins-python3-pitfalls-version-specific-imports: + +Version specific imports +........................ + +Sometimes it is necessary to use an import statement that is explicitly related to a specific Python version, e.g. due to +a package change between Python 2 and 3. You can do this by first trying the Python 3 import and if that doesn't work +out trying the Python 2 import instead: .. code-block:: python @@ -255,20 +262,16 @@ You can do this in differnt ways: except ImportError: import Queue as queue -or +This should be the preferred method of handling situations like this. If you actually do need to do explicit version +specific imports that cannot be handled this way, you can check for the Python version like this: .. code-block:: python import sys - is_py2 = sys.version[0] == '2' - if is_py2: - import Queue as queue + if sys.version[0] == '2': + # Python 2 specific imports else: - import queue as queue - -You can read more about this specific issue in the -`cheat sheet `__ and also in -`the book `__. + # Python 3 specific imports .. _sec-plugins-python3-pitfalls-intdiv: