|
| 1 | +.. _older-standard-library-interfaces: |
| 2 | + |
| 3 | +Older interfaces |
| 4 | +~~~~~~~~~~~~~~~~ |
| 5 | + |
| 6 | +In addition to the direct and ``install_aliases()`` interfaces (described in |
| 7 | +:ref:`standard-library-imports`), ``future`` supports four other interfaces to |
| 8 | +the reorganized standard library. This is largely for historical reasons (for |
| 9 | +versions prior to 0.14). |
| 10 | + |
| 11 | + |
| 12 | +Context-manager for import hooks |
| 13 | +________________________________ |
| 14 | + |
| 15 | +The context-manager interface is via a context-manager called ``hooks``:: |
| 16 | + |
| 17 | + from future.standard_library import hooks |
| 18 | + with hooks(): |
| 19 | + import socketserver |
| 20 | + import queue |
| 21 | + import configparser |
| 22 | + import test.support |
| 23 | + import html.parser |
| 24 | + from collections import UserList |
| 25 | + from itertools import filterfalse, zip_longest |
| 26 | + from http.client import HttpConnection |
| 27 | + import urllib.request |
| 28 | + # and other moved modules and definitions |
| 29 | + |
| 30 | +This interface is straightforward and effective, using PEP 302 import |
| 31 | +hooks. |
| 32 | + |
| 33 | + |
| 34 | +``future.moves`` interface |
| 35 | +__________________________ |
| 36 | + |
| 37 | +The ``future.moves`` interface avoids import hooks. It may therefore be more |
| 38 | +robust, at the cost of less idiomatic code. Use it as follows:: |
| 39 | + |
| 40 | + from future.moves import queue |
| 41 | + from future.moves import socketserver |
| 42 | + from future.moves.http.client import HTTPConnection |
| 43 | + # etc. |
| 44 | + |
| 45 | +If you wish to achieve the effect of a two-level import such as this:: |
| 46 | + |
| 47 | + import http.client |
| 48 | + |
| 49 | +portably on both Python 2 and Python 3, note that Python currently does not |
| 50 | +support syntax like this:: |
| 51 | + |
| 52 | + from future.moves import http.client |
| 53 | + |
| 54 | +One workaround is to replace the dot with an underscore:: |
| 55 | + |
| 56 | + import future.moves.http.client as http_client |
| 57 | + |
| 58 | + |
| 59 | +Comparing future.moves and six.moves |
| 60 | +++++++++++++++++++++++++++++++++++++ |
| 61 | + |
| 62 | +``future.moves`` and ``six.moves`` provide a similar Python 3-style |
| 63 | +interface to the native standard library module definitions. |
| 64 | + |
| 65 | +The major difference is that the ``future.moves`` package is a real Python package |
| 66 | +(``future/moves/__init__.py``) with real modules provided as ``.py`` files, whereas |
| 67 | +``six.moves`` constructs fake ``_LazyModule`` module objects within the Python |
| 68 | +code and injects them into the ``sys.modules`` cache. |
| 69 | + |
| 70 | +The advantage of ``six.moves`` is that the code fits in a single module that can be |
| 71 | +copied into a project that seeks to eliminate external dependencies. |
| 72 | + |
| 73 | +The advantage of ``future.moves`` is that it is likely to be more robust in the |
| 74 | +face of magic like Django's auto-reloader and tools like ``py2exe`` and |
| 75 | +``cx_freeze``. See issues #51, #53, #56, and #63 in the ``six`` project for |
| 76 | +more detail of bugs related to the ``six.moves`` approach. |
| 77 | + |
| 78 | + |
| 79 | +``import_`` and ``from_import`` functions |
| 80 | +_________________________________________ |
| 81 | + |
| 82 | +The functional interface is to use the ``import_`` and ``from_import`` |
| 83 | +functions from ``future.standard_library`` as follows:: |
| 84 | + |
| 85 | + from future.standard_library import import_, from_import |
| 86 | + |
| 87 | + http = import_('http.client') |
| 88 | + urllib = import_('urllib.request') |
| 89 | + |
| 90 | + urlopen, urlsplit = from_import('urllib.request', 'urlopen', 'urlsplit') |
| 91 | + |
| 92 | +This interface also works with two-level imports. |
| 93 | + |
| 94 | + |
| 95 | +install_hooks() call |
| 96 | +____________________ |
| 97 | + |
| 98 | +The last interface to the reorganized standard library is via a call to |
| 99 | +``install_hooks()``:: |
| 100 | + |
| 101 | + from future import standard_library |
| 102 | + standard_library.install_hooks() |
| 103 | + |
| 104 | + import urllib |
| 105 | + f = urllib.request.urlopen('http://www.python.org/') |
| 106 | + |
| 107 | + standard_library.remove_hooks() |
| 108 | + |
| 109 | +If you use this interface, it is recommended to disable the import hooks again |
| 110 | +after use by calling ``remove_hooks()``, in order to prevent the futurized |
| 111 | +modules from being invoked inadvertently by other modules. (Python does not |
| 112 | +automatically disable import hooks at the end of a module, but keeps them |
| 113 | +active for the life of a process unless removed.) |
| 114 | + |
| 115 | +.. The call to ``scrub_future_sys_modules()`` removes any modules from the |
| 116 | +.. ``sys.modules`` cache (on Py2 only) that have Py3-style names, like ``http.client``. |
| 117 | +.. This can prevent libraries that have their own Py2/3 compatibility code from |
| 118 | +.. importing the ``future.moves`` or ``future.backports`` modules unintentionally. |
| 119 | +.. Code such as this will then fall through to using the Py2 standard library |
| 120 | +.. modules on Py2:: |
| 121 | +.. |
| 122 | +.. try: |
| 123 | +.. from http.client import HTTPConnection |
| 124 | +.. except ImportError: |
| 125 | +.. from httplib import HTTPConnection |
| 126 | +.. |
| 127 | +.. **Requests**: The above snippet is from the `requests |
| 128 | +.. <http://docs.python-requests.org>`_ library. As of v0.12, the |
| 129 | +.. ``future.standard_library`` import hooks are compatible with Requests. |
| 130 | +
|
| 131 | +
|
| 132 | +.. If you wish to avoid changing every reference of ``http.client`` to |
| 133 | +.. ``http_client`` in your code, an alternative is this:: |
| 134 | +.. |
| 135 | +.. from future.standard_library import http |
| 136 | +.. from future.standard_library.http import client as _client |
| 137 | +.. http.client = client |
| 138 | +
|
| 139 | +.. but it has the advantage that it can be used by automatic translation scripts such as ``futurize`` and ``pasteurize``. |
| 140 | +
|
| 141 | +
|
0 commit comments