GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing python-future.org — Quick-start guide — Python-Future documentation Quick-start guide¶You can use If you already have Python 3 code, you can instead use Installation¶To install the latest stable version, type: pip install future If you would prefer the latest development version, it is available here. If you are writing code from scratch¶The easiest way is to start each new module with these lines: from __future__ import (absolute_import, division, print_function, unicode_literals) from builtins import * Then write standard Python 3 code. The
To convert existing Python 3 code¶To offer backward compatibility with Python 2 from your Python 3 code,
you can use the from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from builtins import open from builtins import str # etc., as needed from future import standard_library standard_library.install_aliases() and converts several Python 3-only constructs (like keyword-only arguments) to a form compatible with both Py3 and Py2. Most remaining Python 3 code should simply work on Python 2. See pasteurize: Py3 to Py2/3 for more details. To convert existing Python 2 code¶The For example, running import ConfigParser # Py2 module name class Upper(object): def __init__(self, iterable): self._iter = iter(iterable) def next(self): # Py2-style iterator interface return next(self._iter).upper() def __iter__(self): return self itr = Upper('hello') print next(itr), for letter in itr: print letter, # Py2-style print statement into this code which runs on both Py2 and Py3: from __future__ import print_function from future import standard_library standard_library.install_aliases() from future.builtins import next from future.builtins import object import configparser # Py3-style import class Upper(object): def __init__(self, iterable): self._iter = iter(iterable) def __next__(self): # Py3-style iterator interface return next(self._iter).upper() def __iter__(self): return self itr = Upper('hello') print(next(itr), end=' ') # Py3-style print function for letter in itr: print(letter, end=' ') To write out all the changes to your Python files that For complex projects, it is probably best to divide the porting into two stages.
Stage 1 is for “safe” changes that modernize the code but do not break Python
2.7 compatibility or introduce a dependency on the See Stage 1: “safe” fixes and Stage 2: Py3-style code with wrappers for Py2 for more details. Standard library reorganization¶
from future import standard_library standard_library.install_aliases() # Then these Py3-style imports work on both Python 2 and Python 3: import socketserver import queue from collections import UserDict, UserList, UserString from collections import ChainMap # even on Py2.7 from itertools import filterfalse, zip_longest import html import html.entities import html.parser import http import http.client import http.server import http.cookies import http.cookiejar import urllib.request import urllib.parse import urllib.response import urllib.error import urllib.robotparser import xmlrpc.client import xmlrpc.server and others. For a complete list, see Direct imports. Python 2-only dependencies¶If you have dependencies that support only Python 2, you may be able to use the
$ pip3 install mypackagename --no-compile # to ignore SyntaxErrors (or use Then add the following code at the top of your (Py3 or Py2/3-compatible) code: from past.translation import autotranslate autotranslate(['mypackagename']) import mypackagename This feature is experimental, and we would appreciate your feedback on how well this works or doesn’t work for you. Please file an issue here. For more information on the automatic translation feature, see Using Python 2-only dependencies on Python 3. Next steps¶For more information about writing Py2/3-compatible code, see: |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |