「Python」の版間の差分
imported>Lahwaacz.bot (update Pkg/AUR templates (testing https://github.com/lahwaacz/wiki-scripts/blob/master/update-package-templates.py)) |
細 (→ウィジェットバインディング: メンテナンスされないパッケージ python-gobject2 削除) |
||
(6人の利用者による、間の16版が非表示) | |||
2行目: | 2行目: | ||
[[de:Python]] |
[[de:Python]] |
||
[[en:Python]] |
[[en:Python]] |
||
− | [[ |
+ | [[es:Python]] |
+ | [[ko:Python]] |
||
+ | [[ru:Python]] |
||
+ | [[zh-hans:Python]] |
||
{{Related articles start}} |
{{Related articles start}} |
||
− | {{ |
+ | {{Related|Python パッケージガイドライン}} |
− | {{Related|Python |
+ | {{Related|Python/仮想環境}} |
{{Related|mod_wsgi}} |
{{Related|mod_wsgi}} |
||
+ | {{Related|Django}} |
||
{{Related articles end}} |
{{Related articles end}} |
||
[[Wikipedia:ja:Python|Wikipedia]] より: |
[[Wikipedia:ja:Python|Wikipedia]] より: |
||
− | : |
+ | :Python は、広く使用されている汎用のスクリプト言語である。コードのリーダビリティが高くなるように言語が設計されているとされ、その構文のおかげで、C++ や Java などの言語に比べて、より少ないコード行数でプログラムを表現することができるとされている。小規模なプログラムから大規模なプログラムまで、さまざまなプログラムをクリアに書けるように、多くのコードが提供されている。 |
− | : |
+ | :Python は複数のプログラミングパラダイムをサポートしており、オブジェクト指向、命令型、関数型、手続き型などのスタイルでプログラムを書くことができる。動的型付けであり、自動メモリ管理が可能で、さまざまな領域をカバーする大規模な標準ライブラリを提供している。 |
== インストール == |
== インストール == |
||
− | |||
− | [[公式リポジトリ]]にある {{Pkg|python}} や {{Pkg|python2}} パッケージを[[インストール]]してください。 |
||
=== Python 3 === |
=== Python 3 === |
||
− | Python 3 は Python 言語の最新バージョンであり、Python 2 と互換性がありません。大まかには同じですが、細かいところ、特に辞書や文字列などのオブジェクトの扱い方が大幅に変更されており、非推奨になっていた機能が多数削除されています。また、標準ライブラリが目立たないところで再編成されています。大まかな差異については、[ |
+ | Python 3 は Python 言語の最新バージョンであり、Python 2 と互換性がありません。大まかには同じですが、細かいところ、特に辞書や文字列などのオブジェクトの扱い方が大幅に変更されており、非推奨になっていた機能が多数削除されています。また、標準ライブラリが目立たないところで再編成されています。大まかな差異については、[https://wiki.python.org/moin/Python2orPython3 Python2orPython3] や Dive into Python 3 の [http://getpython3.com/diveintopython3/porting-code-to-python-3-with-2to3.html 章] を見て下さい。 |
− | |||
− | 最新版の Python 3 をインストールするには、[[公式リポジトリ]]から {{Pkg|python}} パッケージを[[インストール]]してください。 |
||
− | |||
− | 最新の RC/ベータ版をソースからビルドしたいときは、[http://www.python.org/download/ Python Downloads] を訪れて下さい。[[Arch User Repository|Arch User Repository]] に [[PKGBUILD|PKGBUILD]] も含まれています。RC 版をビルドする場合、バイナリは (デフォルトで) {{ic|/usr/local/bin/python3.x}} にインストールされるので注意してください。 |
||
+ | 最新版の Python 3 をインストールするには、{{Pkg|python}} パッケージを[[インストール]]してください。 |
||
− | virtualenv で新しいプロジェクトを開始するには以下を実行します: |
||
+ | 最新の RC/ベータ版をソースからビルドしたいときは、[https://www.python.org/downloads/ Python Downloads] を訪れて下さい。[[Arch User Repository]] に [[PKGBUILD]] も含まれています。RC 版をビルドする場合、バイナリは (デフォルトで) {{ic|/usr/local/bin/python3.x}} にインストールされるので注意してください。 |
||
− | $ python -m venv newproj |
||
− | $ source newproj/bin/activate |
||
− | (newproj)$ pip install <dependency> |
||
=== Python 2 === |
=== Python 2 === |
||
+ | {{Warning|Python 2 に依存したパッケージも残っていますが、Python 2 は2020年以降メンテナンスされません。https://www.python.org/doc/sunset-python-2/}} |
||
− | 最新版の Python 2 をインストールするには、[[公式リポジトリ]]から {{Pkg|python2}} パッケージを[[インストール]]してください。 |
||
+ | |||
+ | 最新版の Python 2 をインストールするには、{{Pkg|python2}} パッケージを[[インストール]]してください。 |
||
Python 2 は Python 3 と上手く共存することができます。このバージョンを実行するときは {{ic|python2}} を指定する必要があります。 |
Python 2 は Python 3 と上手く共存することができます。このバージョンを実行するときは {{ic|python2}} を指定する必要があります。 |
||
49行目: | 47行目: | ||
どちらにしても、{{ic|python}} を {{ic|python2}} に変更することでプログラムは Python 3 ではなく Python 2 を使用するようになります。 |
どちらにしても、{{ic|python}} を {{ic|python2}} に変更することでプログラムは Python 3 ではなく Python 2 を使用するようになります。 |
||
− | スクリプトを変更せずに python2 を強制的に使用させる方法として python2 で明示的にプログラムを呼び出すという方法もあります、例: |
+ | スクリプトを変更せずに python2 を強制的に使用させる方法として {{ic|python2}} で明示的にプログラムを呼び出すという方法もあります、例: |
− | + | $ python2 ''myScript.py'' |
|
+ | 最後に、スクリプトの呼び出しを制御できない場合、環境を変更する方法があります。この方法はスクリプトが {{ic|#!/usr/bin/env python}} を使っている場合のみ動作し、{{ic|#!/usr/bin/python}} を使っている場合は動作しません。このトリックは {{ic|env}} が {{ic|PATH}} 変数内の最初に一致したエントリを探すことに依存しています。 |
||
− | Finally, you may not be able to control the script calls, but there is a way to trick the environment. It only works if the scripts use {{ic|#!/usr/bin/env python}}, it won't work with {{ic|#!/usr/bin/python}}. This trick relies on {{ic|env}} searching for the first corresponding entry in the PATH variable. |
||
+ | |||
− | First create a dummy folder. |
||
+ | まずダミーディレクトリを作成: |
||
$ mkdir ~/bin |
$ mkdir ~/bin |
||
+ | そして {{ic|python}} から ''python2'' のシンボリックリンクと、設定スクリプトをディレクトリに追加: |
||
− | Then add a symlink 'python' to python2 and the config scripts in it. |
||
$ ln -s /usr/bin/python2 ~/bin/python |
$ ln -s /usr/bin/python2 ~/bin/python |
||
$ ln -s /usr/bin/python2-config ~/bin/python-config |
$ ln -s /usr/bin/python2-config ~/bin/python-config |
||
+ | 最後にあなたの {{ic|PATH}} 変数の''最初に''新しいディレクトリを追加してください: |
||
− | Finally put the new folder ''at the beginning'' of your PATH variable. |
||
$ export PATH=~/bin:$PATH |
$ export PATH=~/bin:$PATH |
||
+ | {{Note|上記のコマンドによる[[環境変数]]の変更は永続的ではありません。現在のターミナルセッションでのみ有効です。}} |
||
− | Note that this change is not permanent and is only active in the current terminal session. |
||
+ | {{ic|env}} によってどの python インタプリタが使われているかを確認するには次のコマンドを使ってください: |
||
− | To check which python interpreter is being used by {{ic|env}}, use the following command: |
||
$ which python |
$ which python |
||
+ | スクリプトによって呼び出される {{ic|#!/usr/bin/env python}} を使用して環境を変更する別の方法として [[#仮想環境]] を使うものがあります。 |
||
− | A similar approach in tricking the environment, which also relies on {{ic|#!/usr/bin/env python}} to be called by the script in question, is to use a [[Virtualenv]]. When a Virtualenv is activated, the Python executable pointed to by {{ic|$PATH}} will be the one the Virtualenv was installed with. Therefore, if the Virtualenv is installed with Python 2, {{ic|python}} will refer to Python 2. To start, [[pacman|install]] {{Pkg|python2-virtualenv}}. |
||
+ | == 昔のバージョン == |
||
− | Then create the Virtualenv. |
||
− | $ virtualenv2 venv # Creates a directory, venv/, containing the Virtualenv |
||
− | Activate the Virtualenv, which will update {{ic|$PATH}} to point at Python 2. Note that this activation is only active for the current terminal session. |
||
− | $ source venv/bin/activate |
||
− | The desired script should then run using Python 2. |
||
+ | {{warning|2014年から Python のバージョン 2.7 と 3.4 以前はアップデートがもはやされていません。セキュリティパッチすらありません。インターネットを使用するアプリケーションや信頼できないコードで古いバージョンを使用するのは危険なので推奨されません。}} |
||
− | == ビルドスクリプトのバージョン問題の対処 == |
||
+ | 現在のバージョンの Python で動作しないアプリケーションのような、過去の遺物を使用したり、古いバージョンの Python が入っているディストリビューションでの Python プログラムの動作をテストするために、[[AUR]] から昔のバージョンの Python をインストールすることができます: |
||
− | Many projects' build scripts assume {{ic|python}} to be Python 2, and that would eventually result in an error - typically complaining that {{ic|print 'foo'}} is invalid syntax. Luckily, many of them call {{ic|python}} in the {{ic|$PATH}} instead of hardcoding {{ic|#!/usr/bin/python}} in the shebang line, and the Python scripts are all contained within the project tree. So, instead of modifying the build scripts manually, there is an easy workaround. Just create {{ic|/usr/local/bin/python}} with content like this: |
||
+ | * Python 3.5: {{AUR|python35}} |
||
− | {{hc|/usr/local/bin/python|<nowiki> |
||
+ | * Python 3.4: {{AUR|python34}} |
||
− | #!/bin/bash |
||
+ | * Python 2.6: {{AUR|python26}} |
||
− | script=$(readlink -f -- "$1") |
||
+ | * Python 2.5: {{AUR|python25}} |
||
− | case "$script" in (/path/to/project1/*|/path/to/project2/*|/path/to/project3*) |
||
+ | * Python 1.5: {{AUR|python15}} |
||
− | exec python2 "$@" |
||
− | ;; |
||
− | esac |
||
+ | 古いバージョンの Python のモジュールやライブラリは {{ic|python<''version without period''>}} で AUR を検索することで見つけられます。例: 2.6 のモジュールを検索する場合 "python26"。 |
||
− | exec python3 "$@" |
||
− | </nowiki>}} |
||
+ | == パッケージの管理 == |
||
− | Where {{ic|<nowiki>/path/to/project1/*|/path/to/project2/*|/path/to/project3*</nowiki>}} is a list of patterns separated by {{ic|<nowiki>|</nowiki>}} matching all project trees. |
||
+ | [[公式リポジトリ]]や [[AUR]] には膨大な Python パッケージが収められていますが、Python エコシステムには [https://pypi.python.org/ PyPI] (Python Package Index) を使用する独自のパッケージマネージャが存在します。 |
||
− | Don't forget to make it executable: |
||
+ | * {{App|pip|Python パッケージをインストールするための PyPA ツール。|https://pip.pypa.io/|{{Pkg|python-pip}}, {{Pkg|python2-pip}}}} |
||
− | # chmod +x /usr/local/bin/python |
||
+ | * {{App|setuptools|Python パッケージを簡単にダウンロード・ビルド・インストール・アップグレード・アンインストール。|https://setuptools.readthedocs.io/|{{Pkg|python-setuptools}}, {{Pkg|python2-setuptools}}}} |
||
+ | 上記のツールの簡単な歴史や比較は [https://packaging.python.org/pip_easy_install/#pip-vs-easy-install pip vs easy_install] を見てください。 |
||
− | Afterwards scripts within the specified project trees will be run with Python 2. |
||
+ | Python のパッケージ管理におけるベストプラクティスは権威筋の [https://packaging.python.org/ こちら] の資料に詳しく載っています。 |
||
− | == Python シェルで補完を表示 == |
||
+ | |||
+ | {{Note|pip を pacman に統合して pip パッケージの PKGBUILD を自動的に生成するツールも存在します: {{AUR|pipman-git}}, {{AUR|pip2arch-git}}。}} |
||
+ | |||
+ | == ウィジェットバインディング == |
||
+ | |||
+ | 以下の[[Wikipedia:ja:ウィジェット・ツールキット|ウィジェットツールキット]]のバインディングが存在します: |
||
+ | * {{App|TkInter|Tk バインディング|https://wiki.python.org/moin/TkInter|標準モジュール}} |
||
+ | * {{App|pyQt|[[Qt]] バインディング|https://riverbankcomputing.com/software/pyqt/intro|{{AUR|python2-pyqt4}} {{Pkg|python2-pyqt5}} {{AUR|python-pyqt4}} {{Pkg|python-pyqt5}}}} |
||
+ | * {{App|pySide|[[Qt]] バインディング|https://wiki.qt.io/PySide|{{AUR|python2-pyside}} {{AUR|python-pyside}}}} |
||
+ | * {{App|pyGTK|[[GTK+|GTK+ 2]] バインディング|http://www.pygtk.org/|{{AUR|pygtk}}}} |
||
+ | * {{App|PyGObject|GObject Introspection による [[GTK+|GTK+ 2/3]] バインディング|https://wiki.gnome.org/PyGObject/|{{Pkg|python2-gobject2}} {{Pkg|python2-gobject}} {{Pkg|python-gobject}}}} |
||
+ | * {{App|wxPython|wxWidgets バインディング|https://wxpython.org/|{{Pkg|python2-wxpython3}} {{Pkg|python-wxpython}}}} |
||
+ | 以上のバインディングを Python で使うには、適当なウィジェットキットをインストールする必要があります。 |
||
+ | |||
+ | == ヒントとテクニック == |
||
+ | |||
+ | === IPython === |
||
+ | [http://ipython.org/ IPython] は Python コマンドラインの強化版です。{{Pkg|ipython}} と {{Pkg|ipython2}} パッケージで利用できます。IPython notebook を利用したい場合、IPython3 notebook には {{Pkg|jupyter-notebook}} を、IPython2 notebook には {{Pkg|ipython2-notebook}} をインストールしてください。 |
||
+ | |||
+ | $ jupyter notebook |
||
+ | |||
+ | を実行することでブラウザを開き IPython カーネルを実行することができます。ブラウザ上で notebook を作成するときに python のバージョンを指定することができます。 |
||
+ | |||
+ | [https://bpython-interpreter.org/ bpython] は Python インタプリタの ncurses インターフェイスです。公式リポジトリでは {{Pkg|bpython}} と {{Pkg|bpython2}} で利用できます。 |
||
+ | |||
+ | === 仮想環境 === |
||
+ | |||
+ | Python は他の仮想環境や、システムの Python パッケージに干渉されることなくパッケージをインストールすることのできる隔離された環境を作成するツールを提供しています。特定のアプリケーションで利用される Python インタプリタを変更することもできます。 |
||
+ | |||
+ | 詳細は [[Python/仮想環境]] を見てください。 |
||
+ | |||
+ | == Python2 シェルで補完を表示 == |
||
+ | |||
+ | {{Note|この項目は Python2 にのみ関連します。Python 3.4 から [https://docs.python.org/3/tutorial/interactive.html タブ補完] はデフォルトで有効になっています。}} |
||
Python のインタラクティブシェルに以下をコピーしてください: |
Python のインタラクティブシェルに以下をコピーしてください: |
||
− | {{hc|/usr/bin/ |
+ | {{hc|/usr/bin/python2|2= |
import rlcompleter |
import rlcompleter |
||
import readline |
import readline |
||
readline.parse_and_bind("tab: complete") |
readline.parse_and_bind("tab: complete") |
||
}} |
}} |
||
− | [http://algorithmicallyrandom.blogspot.com.es/2009/09/tab-completion-in-python-shell-how-to.html |
+ | ソース: [http://algorithmicallyrandom.blogspot.com.es/2009/09/tab-completion-in-python-shell-how-to.html] |
− | == |
+ | == トラブルシューティング == |
+ | === ビルドスクリプトのバージョン問題の対処 === |
||
+ | 多くのプロジェクトのビルドスクリプトは {{ic|python}} は Python 2 だと仮定していて、このことはエラーを起こします。典型的には {{ic|print 'foo'}} は無効な文法であるというエラーが出ます。幸いにも多くの場合、シバン行にハードコードされた {{ic|#!/usr/bin/python}} ではなく {{ic|PATH}} から {{ic|python}} を呼び出していて、Python スクリプトはすべてプロジェクトツリーに含まれています。したがって、ビルドスクリプトを手動で変更する代わりに簡単なワークアラウンドがあります。以下のような {{ic|/usr/local/bin/python}} ファイルを作成してください: |
||
− | 以下の[[Wikipedia:ja:ウィジェット・ツールキット|ウィジェットツールキット]]のバインディングが存在します: |
||
− | * {{App|TkInter|Tk バインディング|http://wiki.python.org/moin/TkInter|標準モジュール}} |
||
− | * {{App|pyQt|[[Qt|Qt]] バインディング|http://www.riverbankcomputing.co.uk/software/pyqt/intro|{{Pkg|python2-pyqt4}} {{Pkg|python2-pyqt5}} {{Pkg|python-pyqt4}} {{Pkg|python-pyqt5}}}} |
||
− | * {{App|pySide|[[Qt|Qt]] バインディング|http://www.pyside.org/|{{Pkg|python2-pyside}} {{Pkg|python-pyside}}}} |
||
− | * {{App|pyGTK|[[GTK+|GTK+ 2]] バインディング|http://www.pygtk.org/|{{Pkg|pygtk}}}} |
||
− | * {{App|PyGObject|GObject Introspection による [[GTK+|GTK+ 2/3]] バインディング|https://wiki.gnome.org/PyGObject/|{{Pkg|python2-gobject2}} {{Pkg|python2-gobject}} {{Pkg|python-gobject2}} {{Pkg|python-gobject}}}} |
||
− | * {{App|wxPython|wxWidgets バインディング|http://wxpython.org/|{{Pkg|wxpython}}}} |
||
− | 以上のバインディングを Python で使うには、適当なウィジェットキットをインストールする必要があります。 |
||
+ | {{hc|/usr/local/bin/python|<nowiki> |
||
− | == 昔のバージョン == |
||
+ | #!/bin/bash |
||
+ | script=$(readlink -f -- "$1") |
||
+ | case "$script" in (/path/to/project1/*|/path/to/project2/*|/path/to/project3*) |
||
+ | exec python2 "$@" |
||
+ | ;; |
||
+ | esac |
||
+ | exec python3 "$@" |
||
− | 現在のバージョンの Python で動作しないアプリケーションのような、過去の遺物を使用したり、古いバージョンの Python が入っているディストリビューション (例: RHEL 5.x は Python 2.4 が、Ubuntu 12.04 は Python 3.1 が入っています) での Python プログラムの動作をテストするために、[[AUR|AUR]] から昔のバージョンの Python をインストールすることができます: |
||
+ | </nowiki>}} |
||
− | * {{AUR|python15}}: Python 1.5.2 |
||
− | * {{AUR|python16}}: Python 1.6.1 |
||
− | * {{AUR|python24}}: Python 2.4.6 |
||
− | * {{AUR|python25}}: Python 2.5.6 |
||
− | * {{AUR|python26}}: Python 2.6.9 |
||
− | * {{AUR|python30}}: Python 3.0.1 |
||
− | * {{AUR|python31}}: Python 3.1.5 |
||
− | * {{AUR|python32}}: Python 3.2.5 |
||
− | * {{AUR|python33}}: Python 3.3.5 |
||
+ | ここで {{ic|<nowiki>/path/to/project1/*|/path/to/project2/*|/path/to/project3*</nowiki>}} は {{ic|<nowiki>|</nowiki>}} によって区切られた Python 2 で動作させる必要のあるすべてのプロジェクトツリーです。 |
||
− | 2014年7月現在、上流の Python は Python 2.7, 3.2, 3.3, 3.4 だけのセキュリティフィックスをサポートしています。インターネットを使用するアプリケーションや信頼できないコードで古いバージョンを使用するのは危険なので推奨されません。 |
||
+ | ファイルを実行可能にすることを忘れないください: |
||
− | Extra modules/libraries for old versions of Python may be found on the AUR by searching for python(version without decimal), eg searching for "python26" for 2.6 modules. |
||
+ | # chmod +x /usr/local/bin/python |
||
− | == Tips and tricks == |
||
+ | その後指定したプロジェクトツリー内のスクリプトは Python 2 で動作します。 |
||
− | [http://ipython.org/ IPython] is a enhanced Python command line available in the official repositories as {{Pkg|ipython}} and {{Pkg|ipython2}}. |
||
== 参照 == |
== 参照 == |
||
− | * [http://shop.oreilly.com/product/ |
+ | * [http://shop.oreilly.com/product/0636920028154.do O'Reilly's Learning Python, 5th edition] ([https://www.oreilly.co.jp/books/9784873113937/ 初めての Python 第3版]) |
− | * [http://www.diveintopython.net/ Dive Into Python] |
+ | * [http://www.diveintopython.net/ Dive Into Python], [http://getpython3.com/diveintopython3/ Dive Into Python3] ([http://diveintopython3-ja.rdy.jp/ 日本語訳]) |
− | * [ |
+ | * [https://python.swaroopch.com/ A Byte of Python] |
− | * [ |
+ | * [https://learnpythonthehardway.org/ Learn Python the Hard Way] |
− | * [ |
+ | * [https://learnpython.org/ Learn Python] |
− | * [ |
+ | * [https://stephensugden.com/crash_into_python/ Crash into Python] (他のプログラミング言語に慣れていることを前提としてます) |
− | * [ |
+ | * [https://www.apress.com/book/9781590598726 Beginning Game Development with Python and Pygame] |
+ | * [http://www.greenteapress.com/thinkpython/ Think Python] ([http://www.cauldron.sakura.ne.jp/thinkpython/thinkpython/ThinkPython.pdf 日本語訳]) |
||
+ | * [https://pythonspot.com Pythonspot] |
||
+ | * [http://www.techbeamers.com/python-tutorial-step-by-step/ Learn Python Step by Step] |
||
+ | * [https://github.com/vinta/awesome-python awesome-python] - Python フレームワーク・ライブラリ・ソフトウェア・資料の厳選リスト。 |
||
+ | * [https://github.com/mahmoud/boltons boltons] - 標準ライブラリで使える構造・レシピ・スニペット。 |
||
+ | * [https://www.alphacodingskills.com/python/ Learn Python Basics to Advanced] |
2020年5月26日 (火) 02:07時点における版
Wikipedia より:
- Python は、広く使用されている汎用のスクリプト言語である。コードのリーダビリティが高くなるように言語が設計されているとされ、その構文のおかげで、C++ や Java などの言語に比べて、より少ないコード行数でプログラムを表現することができるとされている。小規模なプログラムから大規模なプログラムまで、さまざまなプログラムをクリアに書けるように、多くのコードが提供されている。
- Python は複数のプログラミングパラダイムをサポートしており、オブジェクト指向、命令型、関数型、手続き型などのスタイルでプログラムを書くことができる。動的型付けであり、自動メモリ管理が可能で、さまざまな領域をカバーする大規模な標準ライブラリを提供している。
目次
インストール
Python 3
Python 3 は Python 言語の最新バージョンであり、Python 2 と互換性がありません。大まかには同じですが、細かいところ、特に辞書や文字列などのオブジェクトの扱い方が大幅に変更されており、非推奨になっていた機能が多数削除されています。また、標準ライブラリが目立たないところで再編成されています。大まかな差異については、Python2orPython3 や Dive into Python 3 の 章 を見て下さい。
最新版の Python 3 をインストールするには、python パッケージをインストールしてください。
最新の RC/ベータ版をソースからビルドしたいときは、Python Downloads を訪れて下さい。Arch User Repository に PKGBUILD も含まれています。RC 版をビルドする場合、バイナリは (デフォルトで) /usr/local/bin/python3.x
にインストールされるので注意してください。
Python 2
最新版の Python 2 をインストールするには、python2 パッケージをインストールしてください。
Python 2 は Python 3 と上手く共存することができます。このバージョンを実行するときは python2
を指定する必要があります。
Python 2 を必要とするプログラムでは Python 3 の /usr/bin/python
ではなく /usr/bin/python2
を指定する必要があります。
それには、プログラムやスクリプトをテキストエディタで開いて一番最初の行を変更してください。
一番最初の行は以下のどちらかになっています:
#!/usr/bin/env python
または:
#!/usr/bin/python
どちらにしても、python
を python2
に変更することでプログラムは Python 3 ではなく Python 2 を使用するようになります。
スクリプトを変更せずに python2 を強制的に使用させる方法として python2
で明示的にプログラムを呼び出すという方法もあります、例:
$ python2 myScript.py
最後に、スクリプトの呼び出しを制御できない場合、環境を変更する方法があります。この方法はスクリプトが #!/usr/bin/env python
を使っている場合のみ動作し、#!/usr/bin/python
を使っている場合は動作しません。このトリックは env
が PATH
変数内の最初に一致したエントリを探すことに依存しています。
まずダミーディレクトリを作成:
$ mkdir ~/bin
そして python
から python2 のシンボリックリンクと、設定スクリプトをディレクトリに追加:
$ ln -s /usr/bin/python2 ~/bin/python $ ln -s /usr/bin/python2-config ~/bin/python-config
最後にあなたの PATH
変数の最初に新しいディレクトリを追加してください:
$ export PATH=~/bin:$PATH
env
によってどの python インタプリタが使われているかを確認するには次のコマンドを使ってください:
$ which python
スクリプトによって呼び出される #!/usr/bin/env python
を使用して環境を変更する別の方法として #仮想環境 を使うものがあります。
昔のバージョン
現在のバージョンの Python で動作しないアプリケーションのような、過去の遺物を使用したり、古いバージョンの Python が入っているディストリビューションでの Python プログラムの動作をテストするために、AUR から昔のバージョンの Python をインストールすることができます:
- Python 3.5: python35AUR
- Python 3.4: python34AUR
- Python 2.6: python26AUR
- Python 2.5: python25AUR
- Python 1.5: python15AUR
古いバージョンの Python のモジュールやライブラリは python<version without period>
で AUR を検索することで見つけられます。例: 2.6 のモジュールを検索する場合 "python26"。
パッケージの管理
公式リポジトリや AUR には膨大な Python パッケージが収められていますが、Python エコシステムには PyPI (Python Package Index) を使用する独自のパッケージマネージャが存在します。
- pip — Python パッケージをインストールするための PyPA ツール。
- setuptools — Python パッケージを簡単にダウンロード・ビルド・インストール・アップグレード・アンインストール。
上記のツールの簡単な歴史や比較は pip vs easy_install を見てください。
Python のパッケージ管理におけるベストプラクティスは権威筋の こちら の資料に詳しく載っています。
ウィジェットバインディング
以下のウィジェットツールキットのバインディングが存在します:
- TkInter — Tk バインディング
- https://wiki.python.org/moin/TkInter || 標準モジュール
- pyQt — Qt バインディング
- https://riverbankcomputing.com/software/pyqt/intro || python2-pyqt4AUR python2-pyqt5 python-pyqt4AUR python-pyqt5
- pySide — Qt バインディング
- pyGTK — GTK+ 2 バインディング
- http://www.pygtk.org/ || pygtkAUR
- PyGObject — GObject Introspection による GTK+ 2/3 バインディング
- wxPython — wxWidgets バインディング
以上のバインディングを Python で使うには、適当なウィジェットキットをインストールする必要があります。
ヒントとテクニック
IPython
IPython は Python コマンドラインの強化版です。ipython と ipython2 パッケージで利用できます。IPython notebook を利用したい場合、IPython3 notebook には jupyter-notebook を、IPython2 notebook には ipython2-notebook をインストールしてください。
$ jupyter notebook
を実行することでブラウザを開き IPython カーネルを実行することができます。ブラウザ上で notebook を作成するときに python のバージョンを指定することができます。
bpython は Python インタプリタの ncurses インターフェイスです。公式リポジトリでは bpython と bpython2 で利用できます。
仮想環境
Python は他の仮想環境や、システムの Python パッケージに干渉されることなくパッケージをインストールすることのできる隔離された環境を作成するツールを提供しています。特定のアプリケーションで利用される Python インタプリタを変更することもできます。
詳細は Python/仮想環境 を見てください。
Python2 シェルで補完を表示
Python のインタラクティブシェルに以下をコピーしてください:
/usr/bin/python2
import rlcompleter import readline readline.parse_and_bind("tab: complete")
ソース: [1]
トラブルシューティング
ビルドスクリプトのバージョン問題の対処
多くのプロジェクトのビルドスクリプトは python
は Python 2 だと仮定していて、このことはエラーを起こします。典型的には print 'foo'
は無効な文法であるというエラーが出ます。幸いにも多くの場合、シバン行にハードコードされた #!/usr/bin/python
ではなく PATH
から python
を呼び出していて、Python スクリプトはすべてプロジェクトツリーに含まれています。したがって、ビルドスクリプトを手動で変更する代わりに簡単なワークアラウンドがあります。以下のような /usr/local/bin/python
ファイルを作成してください:
/usr/local/bin/python
#!/bin/bash script=$(readlink -f -- "$1") case "$script" in (/path/to/project1/*|/path/to/project2/*|/path/to/project3*) exec python2 "$@" ;; esac exec python3 "$@"
ここで /path/to/project1/*|/path/to/project2/*|/path/to/project3*
は |
によって区切られた Python 2 で動作させる必要のあるすべてのプロジェクトツリーです。
ファイルを実行可能にすることを忘れないください:
# chmod +x /usr/local/bin/python
その後指定したプロジェクトツリー内のスクリプトは Python 2 で動作します。
参照
- O'Reilly's Learning Python, 5th edition (初めての Python 第3版)
- Dive Into Python, Dive Into Python3 (日本語訳)
- A Byte of Python
- Learn Python the Hard Way
- Learn Python
- Crash into Python (他のプログラミング言語に慣れていることを前提としてます)
- Beginning Game Development with Python and Pygame
- Think Python (日本語訳)
- Pythonspot
- Learn Python Step by Step
- awesome-python - Python フレームワーク・ライブラリ・ソフトウェア・資料の厳選リスト。
- boltons - 標準ライブラリで使える構造・レシピ・スニペット。
- Learn Python Basics to Advanced