.. _future_module: future モジュール ================================================================== .. sectionauthor:: Noboru Yamamoto .. codeauthor:: Noboru Yamamoto .. _Easy, clean, reliable Python 2/3 compatibility: https://python-future.org/compatible_idioms.html :py:mod:`future` モジュール\ :cite:p:`Charmers-Pty-Ltd:2020kk`\ は\ [#futureModule]_ \ , python2/python3の双方で動作するスクリプトの開発を支援するためのモジュールです。 このモジュールのホームページ\ `Easy, clean, reliable Python 2/3 compatibility`_ \ には次の様に記述されています。 .. rubric:: ホームページの記述 python-future is the missing compatibility layer between Python 2 and Python 3. It allows you to use a single, clean Python 3.x-compatible codebase to support both Python 2 and Python 3 with minimal overhead. --------- python-futureは Python2 と Python3 の間にかけている互換性レイヤを提供します。このモジュールを使うことによって、Python2とPython3の双方で最小のオーバーヘッドで実行可能なpythonプログラムをサポートする、単一の綺麗な, Python3互換のプログラムの基盤を手に入れます。 futureモジュールのホームページジ\ `Easy, clean, reliable Python 2/3 compatibility`_ \ はpython2/python3で共通に動作するプログラムを書くために役に立つpython2/pytho3の違いを\ `Cheat Sheet `_\ にまとめています。これらの違いのうち多くは2to3スクリプトで適切に処理されます。 インストール ---------------- futureモジュールはpipを使って簡単にインストールすることができます。 .. code-block:: shell python2 -m pip install future RH, SL, CentOSなどのOSではyumを使ってインストールすることも可能です。 .. code-block:: shell yum install python2-future futurize -------------------- futurizeは futureモジュールと一緒にインストールされるコマンドツールです。 future moduleを使ったシェルレベルのコマンドです。 python2向けに書かれたスクリプトを、python2/3の双方で利用可能に書き換えることを サポートしてくれます。 futurizeコマンドは  * python2のスクリプトを"modern"な スクリプト に変換する stage1(-1/--staget1) と * future モジュールなどを使って、python2/python3の双方に対応可能なスクリプトに変換するstage2(-2/--stage2) のふたつのステージがあります。デフォルトでは、このふたつのステージを連続して行います(-0/--both-stagge)。 :command:`futurize` コマンドは内部で2to3のライブラリを利用しています。 pasteurize ------------------ pasturize は, futurerizeとは逆に、python3のスクリプト を python3/2両用のスクリプト に変換します。 futurize と同様に future moduleを利用しています。 3to2というモジュールも存在しますが, 2015年4月16日のバージョンで更新が止まっている様子です。 futureモジュールの最新版は 0.18.3で Jan. 13, 2023 に更新されています(。 Python2の現在のVersionは 2.7.18(Rel. April 20,2020) ですから、ほぼ最新版のPython2に対応していると思って良い様です。 .. rubric:: 脚注 .. [#futureModule] : :py:mod:`future` モジュールと :py:mod:`__future__` モジュールは似た名前ですが、全く異なるモジュールです。 :py:mod:`__future__` はPythonの標準ライブラリの一つで、 将来のバージョンで導入される(された)機能をそれ以前のバージョンでも利用可能とするためのモジュールです。Python3で導入された機能をPython2で利用するためにも利用されます。 一方、 :py:mod:`future` モジュールは、 `python-future `_ プロジェクトによって保守されている、"Python2とPython3の互換性を保つ" のに役立つモジュールです。