diff --git a/docs/plugins/python3_migration.rst b/docs/plugins/python3_migration.rst index 90f08149e..b5ad65780 100644 --- a/docs/plugins/python3_migration.rst +++ b/docs/plugins/python3_migration.rst @@ -70,7 +70,7 @@ Then create an editable install of your plugin, start the server and start testi .. note:: - If you want to migrate your existing OctoPrint install *on OctoPi* to Python 3, I suggest to first make a + If you want to migrate your existing OctoPrint install *on OctoPi 0.17.0* to Python 3, I suggest to first make a :ref:`backup `, then move the existing venv ``/home/pi/oprint`` out of the way and create a new one based on Python 3 (which should already be present on current OctoPi images): @@ -79,7 +79,7 @@ Then create an editable install of your plugin, start the server and start testi mv ~/oprint ~/oprint.py2 virtualenv --python=/usr/bin/python3 oprint source ~/oprint/bin/activate - pip install "OctoPrint>=1.4.0rc1" + pip install "OctoPrint>=1.4.0" sudo service octoprint restart .. _sec-plugins-python3-markup: @@ -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 ................ @@ -246,6 +246,33 @@ You can read more about this specific issue in the `cheat sheet `__ and also in `the book `__. +.. _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 + + try: + import queue + except ImportError: + import Queue as queue + +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 + if sys.version[0] == '2': + # Python 2 specific imports + else: + # Python 3 specific imports + .. _sec-plugins-python3-pitfalls-intdiv: Integer division