サードパーティ製のモジュール

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

サードパーティ製のモジュール

web2pyはPythonで書かれているので、サードーパーティ製のものも含め任意のPythonモジュールをインポートして使うことができます。それらを見つけられるようにするだけです。任意のPythonアプリケーションと同様に、モジュールは公式のPython"site-packages"ディレクトリにてインストールすることができ、コードのどの場所からでもインポートすることができます。

web2py is written in Python, so it can import and use any Python module, including third party modules. It just needs to be able to find them. As with any Python application, modules can be installed in the official Python "site-packages" directory, and they can then be imported from anywhere inside your code.

"site-packages"にあるモジュールは、その名が示すように、サイトレベルのパッケージです。site-packagesを必要とするアプリケーションは、それらのモジュールが個別にインストールできないのであれば、ポータブルではありません。"site-pacakages"にモジュールを持つ利点は、複数のアプリケーションがそれを共有できることです。たとえば、"matplotlib"というグラフ描画用のパッケージを考えましょう。これはPEAKのeasy_installコマンドを使用してシェルからインストールすることができます。:

Modules in "site-packages" directory are, as the name suggests, site-level packages. Applications requiring site-packages are not portable unless these modules are installed separately. The advantage of having modules in "site-packages" is that multiple applications can share them. Let's consider, for example, the plotting package called "matplotlib". You can install it from the shell using the PEAK easy_install command:

1.

easy_install py-matplotlib

そうすると、すべてのモデル/コントローラ/ビューにおいてそれをインポートすることができます:

and then you can import it into any model/controller/view with:

1.

import matplotlib

web2pyのソース配布とWindows版のバイナリ配布は1つのsite-packagesをそのトップレベルのフォルダに持っています。Mac版のバイナリ配布は、そのフォルダ内にsite-packagesがあります:

The web2py source distribution, and the Windows binary distribution has a site-packages in the top-level folder. The Mac binary distribution has a site-packages folder in the folder:

1.

web2py.app/Contents/Resources/site-packages

site-packagesを使用する際の問題は、1つのモジュールの複数のバージョンを同時に使用することが難しいことです。つまり、2つのアプリケーションが1つのファイルの異なるバージョンを使用する場合があることです。この場合、sys.pathはどちらのアプリケーションにも影響するため変更することができません。

The problem with using site-packages is that it becomes difficult to use different versions of a single module at the same time, for example there could be two applications but each one uses a different version of the same file. In this example, sys.path cannot be altered because it would affect both applications.

このような状況のために、web2pyはモジュールをインポートするためのもう1つの方法を用意しています。それは、グローバルなsys.pathを変更せずに、アプリケーションの"modules"フォルダにそれらを配置する方法です。この1 つの利点は、モジュールはアプリケーションと共に自動的にコピーされ配布されることです。しかし、それを適用するにはそれなりの制限があります。そのた め、web2pyは"modules"フォルダからインポートするとき必ず使うべきlocal_import関数を用意しています。これはその使用例です:

For this kind of situation, web2py provides another way to import modules in such a way that the global sys.path is not altered: by placing them in the "modules" folder of an application. One side benefit is that the module will be automatically copied and distributed with the application; however, there are certain restrictions that apply. web2py provides alocal_import function that must be used to import modules from the "modules" folder. Here is an example of usage:

1.

mymodule = local_import('mymodule')

この関数はアプリのローカルモジュールフォルダにおいてmymoduleを探し、イコールの左側にある名前とともにインポートします。

The function looks for mymodule in the app local modules folder and imports it with the name on the left-hand side of equal.

この関数は、name、reload、appという3つの引数を取ります。

This function takes three arguments: name, reload and app.

  • reload=Trueと指定するときは、各リクエストにおいてモジュールが再インポートされます。そうしないと、pythonのプロセスはモジュールを一回しかインポートしません。デフォルトはreload=Falseです。開発段階では、reload=Trueに設定すると便利です。なぜなら、モジュールへの修正が、再起動なしに直ちに有効になるからです。しかし、リクエストごとに再インポートすることはパフォーマンスを低下させます。したがって、本番環境では、reload=Falseにすることをお勧めします。

  • When you specify reload=True, it will re-import the module upon each request; otherwise your python process will only import the module once. The default isreload=False. In development, setting reload=True can be convenient because changes to your module will work immediately without requiring a restart. However, re-importing upon each request will slow down performance, so the default reload=Falseis recommended in production.

  • appはアプリケーションの名前で、そこからモジュールがインポートされます。デフォルトはrequest.applicationです。

  • app is the name of the application from which to import the module, it defaults to request.application.