「Python」の版間の差分
(→Matplotlib) |
|||
行407: | 行407: | ||
* [http://matplotlib.org/faq/installing_faq.html#install-osx-binaries こちらの] の手順に従って、pip3 をインストール | * [http://matplotlib.org/faq/installing_faq.html#install-osx-binaries こちらの] の手順に従って、pip3 をインストール | ||
* pip3 install matplotlib | * pip3 install matplotlib | ||
− | * | + | * 以下のコマンドでテスト |
− | + | $ python3 -c 'import matplotlib; print(matplotlib.__version__, matplotlib.__file__)' | |
− | + | 1.5.1 /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py | |
=====Matplotlib Basemap Toolkit===== | =====Matplotlib Basemap Toolkit===== |
2016年8月8日 (月) 11:12時点における版
Pythonとは?
スクリプト言語の1つ。(Mac OS X に標準搭載されている。)
- 豊富なライブラリモジュール(パッケージ)が魅力?
Mac OS X の Python
インストールされている Python を調べる。下記は標準搭載の Python を調べている。
% sw_vers ProductName: Mac OS X ProductVersion: 10.7.4 BuildVersion: 11E53 $ which python /usr/bin/python $ python --version Python 2.7.1 $ python -c 'import sys,math;print math.log(sys.maxint+1)/math.log(2)+1' 64.0 $ python -c 'import numpy;print numpy.__version__' 1.5.1 $ python -c "import distutils.sysconfig;print distutils.sysconfig.get_python_lib()" /Library/Python/2.7/site-packages $ defaults read com.apple.versioner.python { "Prefer-32-Bit" = 0; }
64 or 32 ビット動作についてはアクティビティモニタでPythonのプロセスの「種類」を見るとより確実でしょう。
インストール
モジュールのインストールについて
- python本体とモジュールの整合に注意が必要
- architecture:MacOSX 10.6 標準には,universal: 64 bit (x86_64)/ 32 bit (i386)/ ppc の Python 2.6.1 と NumPy 1.2.1 がインストールされる。
- インストール済みモジュールを調べる
- バージョン(例 numpy)
$ python -c "import numpy;print numpy.__version__" 1.2.1
- モジュールのarchitectureを調べる
- 例 numpy
$ file /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/numpy/lib/_compiled_base.so
- 例 math
$ python -c "import math;print dir(math);import commands;print commands.getoutput('file ' + math.__file__)"
- 調べ方が統一されているわけではない
site-packagesディレクトリ
モジュール情報が収められるディレクトリ
MacOSX 10.5標準pythonでの例
/Library/Python/2.5/site-packages
python-2.5.x.dmgでの例
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/
site-packagesディレクトリを調べて、その中の egg ファイルやeasy-installでインストールされたモジュールを調べる例
$ ls `python -c "import distutils.sysconfig;print distutils.sysconfig.get_python_lib()"` |grep \.egg lxml-2.3-py2.6-macosx-10.6-universal.egg setuptools-0.6c11-py2.6.egg matplotlib-1.1.0-py2.6-macosx-10.6-universal.egg
$ egrep 'egg$' `python -c "import distutils.sysconfig;print distutils.sysconfig.get_python_lib()"`/easy-install.pth ./lxml-2.3-py2.6-macosx-10.6-universal.egg ./setuptools-0.6c11-py2.6.egg ./matplotlib-1.1.0-py2.6-macosx-10.6-universal.egg
python本体のインストール
標準搭載版でないpythonをインストールするには、
Python Official Website 提供版
http://www.python.org/download
- python-3.3.x.dmg
- python-3.2.x.dmg
- python-3.1.x.dmg
- python-2.7.x.dmg
- python-2.6.x.dmg
- python-2.5.x.dmg
homebrew
https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
- インストールのガイド:"Installing scientific Python on Mac OS X"
- インストールのガイド(旧):"Installing Python, virtualenv, NumPy, SciPy, matplotlib and IPython on Lion"
関係コマンドを含めて通常通りに /usr/local/bin にインストールされる。(以前は、関係コマンドについては /usr/local/share/python/ に収められた。)
$ brew info python python: stable 2.7.4 http://www.python.org : : $ which python /usr/local/bin/python $ python --version Python 2.7.4 $ python -c "import distutils.sysconfig;print distutils.sysconfig.get_python_lib()" /usr/local/lib/python2.7/site-packages
Python環境を整える例(うまくインストールできないかもしれない)
$ ls -ld `which git` lrwxr-xr-x 1 me admin 28 6 18 10:02 /usr/local/bin/git -> ../Cellar/git/1.7.11/bin/git $ brew install gfortran $ sudo easy_install numpy $ sudo easy_install scipy $ sudo easy_install matplotlib $ sudo easy_install pandas
ディストリビューション
SciPy, matplotlib などを同梱したディストリビューション(参考:http://www.scipy.org/install.html )
- Enthought Canopy
- Enthought Python Distribution の後継
- https://www.enthought.com/products/canopy/
- Anaconda
- http://continuum.io/downloads
- .bash_profile をインストーラーが改変して、$PATH に $HOME/anaconda/bin などを追記する。
Python 3.xのインストール
http://www.r-styles.net/mt-archives/weblog/development/python30-on-leopard.html
Pythonを支援する環境・ツール
setup tools および easy_installコマンド
setup tools
一部のモジュールに対して管理機能(インストール等)を提供
- http://pypi.python.org/pypi/setuptools
- easy_installコマンドも提供
- インストール作業例1
$ sh setuptools-0.6c11-py2.5.egg
- インストール作業例2
setuptools-0.6c11 をインストールしてくれるスクリプトの利用
$ rm -v ez_setup.py $ curl -O http://peak.telecommunity.com/dist/ez_setup.py $ sudo python ez_setup.py
既存のsetuptoolsをアップデートには
$ sudo python ez_setup.py -U setuptools
easy_installコマンド
easy_installコマンドは下記のモジュール(eggファイル形式)をインストールする機能を提供。
- Python Package Index (PyPI) でリモートで提供されている モジュール、もしくは、
- ローカルの eggファイル
easy_installコマンドを使ってPyYAMLをインストールする例
$ sudo easy_install PyYAML $ sudo easy_install PyYAML==3.10 # バージョン指定の例 $ sudo easy_install -U PyYAML # アップデート(PyPI提供の最新バージョンへ)
アンインストールの例
#!/bin/sh SUDO='sudo' PYTHON='/usr/bin/python' SITE_PACKAGES=`${PYTHON} -c "import distutils.sysconfig;print distutils.sysconfig.get_python_lib()"` ${SUDO} rm -rv ${SITE_PACKAGES}/PyYAML-3.10-py2.7-macosx-10.7-intel.egg
インストールにclangを必要とするモジュールもある。milkのインストールは、
$ brew install eigen $ sudo CC=clang CXX=clang++ easy_install milk
easy_installおよびpipコマンドではインストールできないモジュールでもダウンロードし処理しようとするが、下記例のようにエラーとなる。したがってsetup.pyでインストールする必要がある(下記の方法)。
$ sudo easy_install wxpython Processing wxPython-src-2.9.4.0.tar.bz2 error: Couldn't find a setup script in /tmp/easy_install-8Olzeb/wxPython-src-2.9.4.0.tar.bz2
setup.py
なお、setuptools(およびegg形式、easy_install)を使わない形の、パッケージのインストールは、setup.py を実行する(デフォルトのdistutilsの機能が使われる):
$ tar -xzf cvxopt-1.1.5.tar.gz $ cd cvxopt-1.1.5/src $ sudo python setup.py install --record install_log.txt : : $ ls /Library/Python/2.7/site-packages |grep cvxopt cvxopt cvxopt-1.1.5-py2.7.egg-info $ pip freeze |grep cvxopt cvxopt==1.1.5 $ pip search cvxopt CVXOPT - Convex optimization package
なお、cvxopt-1.1.6 は pip でインストールできるようになった。
$ pip install cvxopt
PIP
PIPコマンドは、PyPI で提供されているモジュール(eggファイル形式)に対して管理機能(インストール等)を提供。ただしローカルの eggファイルには非対応。上記のeasy_installコマンドの機能を代替する。searchやuninstall機能を持っている。
PIP をインストールするためにeasy_install を使う例。
$ sudo easy_install pip
PIPコマンドを使ってnumpyモジュールを探す例。
$ pip search numpy --timeout=30
(ただし pip 1.5 以前では pip search は proxy越えは動かない。→修正のための情報:「pip searchがproxyを通さない」。)
PIPを使って、Python Imaging Library (PIL) をインストールする例
$ sudo ARCHFLAGS="-arch i386 -arch x86_64" pip install PIL
インストールされているモジュールの一覧を見る例
$ pip freeze lxml==2.3 setuptools==0.6c11 matplotlib==1.1.0
モジュールのインストールの例(下記はアップデートかつバージョン指定の例)
$ pip install -U lxml==3.2.5
ソースを取ってきてインストール(アップデート)する例:
$ curl -O https://pypi.python.org/packages/source/s/setuptools/setuptools-3.4.2.tar.gz もしくは $ pip install --no-use-wheel -d . setuptools==3.4.2 $ pip install -U setuptools-3.4.2.tar.gz
PyPIからではなくgit/hgレポジトリから開発版モジュール等をインストールする例:
$ pip install git+https://github.com/mooz/percol.git#egg=Package $ sudo -E pip install hg+https://bitbucket.org/lgautier/rpy2/ $ pip freeze Warning: cannot find svn location for rpy2==2.4.0devdev-20121004 ## FIXME: could not find svn URL in dependency_links for this package: rpy2==2.4.0devdev-20121004 : :
インストールされているモジュールの新しいバージョンを探す例(ただしこの中のGDALについては、PyPIの更新がされていないので、本当の最新バージョンは認識できていない):
$ pip list | cut -d \( -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:' Pillow - Python Imaging Library (Fork) INSTALLED: 2.2.2 LATEST: 2.3.0 -- GDAL - GDAL: Geospatial Data Abstraction Library INSTALLED: 1.10.1 LATEST: 1.10.0
モジュール
IPython
python用の対話型シェル環境
- Python標準シェルに比べて高機能
- IPython 0.12.x は、Python 2.6, 2.7 と 3.1 を公式にはサポート。 webベースのノートブック機能付き。
$ ipython notebook [NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/ [NotebookApp] Use Control-C to stop this server and shut down all kernels.
- IPython 0.10.0 は、Python 2.5 と 2.6 を公式にはサポート
- IPython 0.10.1 は、Python 2.7 もサポート
SciPy
科学技術計算関数を提供。
- macports からもインストール可能。
- SciPy 0.7.x は Python 2.6 までをサポート
- SciPy 0.8.0 は Python 2.4 - 2.6 までをサポート
- SciPy 0.9.0 は Python 2.4 - 2.7 及び 3.1 をサポート( NumPy 1.5 が必要)
SciPyについての説明等:
- http://ja.wikipedia.org/wiki/SciPy
- http://alias.dip.jp/blog/archives/category/research/scipy
- http://scipy.dip.jp/index.php?SciPy
インストール
- Mac OS X 10.5.7 Leopardへインストール
- Building 64bit NumPy/SciPy/PyLab on Snow Leopard
- Mac OS X の標準状態は伝統的に SciPy(及び matplotlib、basemapなど)のインストールが容易ではないらしい.標準のPython+NumPyの下で率直にインストールを行うと下記のようなエラーとなる.(scipy-0.7.2、MacOSX 10.6.3 で確認)
$ python -c "from distutils import util;print util.get_platform()" macosx-10.6-universal $ python setup.py install : : Can't install when cross-compiling
- homebrew では、pip コマンドではインストールが容易ではないもの向けに、インストーラー(formula)が用意されている。
$ brew tap homebrew/science samueljohn/python $ brew search scipy scipy - SciPy: Scientific Library for Python LATEST: 0.13.1 (uninstalled)
- homebrew の gfortran を使うと、pip install scipy (easy_install scipy)に失敗する報告例がある。
Scipy Superpack
MacOSX標準搭載のPythonで動くように調整を施した各種64bitバイナリパッケージ(numpy-1.8.xなど)をインストールしてくれる。ただし安定版とは限らないので注意が必要。"Recent builds of fundamental Python scientific computing packages for OS X" とのこと。
- サイト:Scipy Superpack、Scipy Superpack (github)
- OS X 10.8 用、および OS X 10.7.4 用が用意されている
- 10.7.4 用の中身は、
ipython-0.14.dev-py2.7.egg matplotlib-1.2.x-py2.7-macosx-10.7-intel.egg numpy-1.8.0.dev_436a28f_20120710-py2.7-macosx-10.7-x86_64.egg pandas-0.8.1.dev_8cc9826_20120717-py2.7-macosx-10.7-x86_64.egg pymc-2.2-py2.7-macosx-10.7-intel.egg scikit_learn-0.12_git-py2.7-macosx-10.7-intel.egg scipy-0.10.1-py2.7-macosx-10.7-x86_64.egg statsmodels-0.5.0-py2.7-macosx-10.7-intel.egg
- 上記の matplotlib-1.2.x は, matplotlib.pyplot.pcolormesh(numpy.outer(arange(8),arange(8))) を show() すると backend_macosx.py が TypeError を起こす.(matplotlib-1.2.0 では解決されている)
下記はoutdated。
- This shell script will install recent 64-bit builds of Numpy (1.7) and Scipy (0.11), Matplotlib (1.2), iPython (0.12), Pandas (0.6), Statsmodels (0.4.0) as well as PyMC (2.2 alpha) for OS X 10.7 (Lion) on Intel Macintosh. . Dependencies: OS X 10.7 (Lion), Python 2.7, Xcode 4.2
- 下記の各種バイナリ+ソースのインストーラー。Snow Leopard(GCC 4.2.1, Python 2.6.1)環境向けにビルドされている。superpack_10.6_2010.12.15.sh。
- numpy-2.0.0.dev_799179d_20101215, matplotlib-1.0.0, scipy-0.10.0.dev_20101215, pymc-2.1beta_20101215
- setuptools-0.6c11 (ez_setup.py), readline, ipython-0.11.dev, nose 0.11, DateUtils
- gfortran 4.2.4(42-5664) (optional) Xcode 3.2.3--3.2.5向けビルド
NumPy
数値計算向け環境 http://docs.scipy.org/doc/numpy/reference/routines.html
- 関数抜粋とMATLAB比較: http://mathesaurus.sourceforge.net/matlab-numpy.html
- NumPy 1.2.x は Python 2.6 までをサポート
- MacOSX 10.6 には Python 2.6.1 と NumPy 1.2.1 とが標準で入っている
- NumPy 1.3.x や 1.4.x は Python 2.6 までをサポート
- NumPy 1.5.x は Python 3.1 までをサポート
- MacOSX 10.7 には Python 2.7.1 と NumPy 1.5.1 とが標準で入っている
- NumPy 1.6.x は Python 3.2 までをサポート
- Mac標準搭載Pythonで利用する場合には → http://radpy.blogspot.com/2009/06/numpymac.html
- PythonのリストやNumPyの配列(ndarray)などの例
import numpy def myprint(x): print x, "<--", x.__class__.__name__ a = range(1, 3) b = numpy.array(a) # b = numpy.arange(1, 3) c = tuple(a) d = {'python': 7, 'ruby': 8} myprint(a) #=> [1, 2] <-- list myprint(b) #=> [1 2] <-- ndarray myprint(numpy.vectorize(lambda x: x*2)(b)) #=> [2 4] <-- ndarray myprint(c) #=> (1, 2) <-- tuple myprint(d) #=> {'python': 7, 'ruby': 8} <-- dict myprint(map(lambda x: isinstance(x, (list, numpy.ndarray, tuple, dict)), [a, b, c, d]))
Matplotlib
2Dプロット環境
画像描画バックエンド
- MacOSXでは、標準では "macosx" がMatplotlibの画像描画バックエンドに使われる。
- アニメーション等の描画では、下記例のようにソースの冒頭で、画像描画バックエンドの切り替えが必要になる場合がある。もしくは ~/.matplotlib/matplotlibrc の中に記述して下さい。
import matplotlib matplotlib.use('TkAgg')
$ cat ~/.matplotlib/matplotlibrc backend : TkAgg
動作例
$ ipython --pylab In [1]: x = randn(10000) In [2]: hist(x, 100) In [3]: quit
動作例2:
$ cat scatterplot.py # scatter plot の動作テスト import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm npnt = 1000 pnts = np.random.normal(size=(npnt,2)) col = [cm.rainbow((i%10)/10.) for i in range(npnt)] plt.scatter(*np.transpose(pnts),s=20,c=col,edgecolors='none') plt.grid() plt.show() # こちらは遅い(重い) for i in range(npnt): plt.plot(*pnts[i],marker="o",ms=4,c=col[i],mec='none') plt.grid() plt.show()
インストール手順(1)
- matplotlib-1.0.0-python.org-py2.6-macosx10.4.dmgほか
- Building matplotlib for Snow Leopard
- Notes for how to setup a 64-bit Mac with important Python development tools
- NASA Modeling Guru: Installing MatplotLib and BaseMaps on Mac OSX
インストール手順(2)
- Python.org Python3
- こちらの の手順に従って、pip3 をインストール
- pip3 install matplotlib
- 以下のコマンドでテスト
$ python3 -c 'import matplotlib; print(matplotlib.__version__, matplotlib.__file__)' 1.5.1 /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/__init__.py
Matplotlib Basemap Toolkit
地図とデータをプロット http://matplotlib.sourceforge.net/basemap/doc/html/ http://www.scipy.org/Cookbook/Matplotlib/Maps
coastline データは GSHHS のもの(http://www.soest.hawaii.edu/wessel/gshhs/gshhs.html) . state, country, river データは GMT のもの(http://gmt.soest.hawaii.edu).
- 関連→ リスト/地球科学系
lxml
xml処理.
- "Pythonic binding for the libxml2 and libxslt libraries" とのこと
- http://codespeak.net/lxml
- インストール例
libxml2とlibxsltをインストール後、easy_installでlxmlをインストール
$ curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.8.tar.gz $ curl -O ftp://xmlsoft.org/libxml2/libxslt-1.1.26.tar.gz $ cd libxml2-2.7.8 $ ./configure --prefix=/usr/local/libxml2-2.7.8 CC="gcc -arch i386 -arch x86_64" CXX="g++ -arch i386 -arch x86_64" CPP="gcc -E" CXXCPP="g++ -E" $ make $ sudo make install $ cd ../libxslt-1.1.26 $ ./configure --prefix=/usr/local/libxslt-1.1.26 --with-libxml-prefix=/usr/local/libxml2-2.7.8 $ make $ sudo make install $ sudo ln -s /usr/local/libxml2-2.7.8/lib/python2.6/site-packages/* /Library/Python/2.6/site-packages/ # for Mac OSX 10.6 default Python $ sudo ln -s /usr/local/libxslt-1.1.26/lib/python2.6/site-packages/* /Library/Python/2.6/site-packages/ # for Mac OSX 10.6 default Python $ STATIC_DEPS=true sudo easy_install 'lxml>=2.3'
テスト
$ curl -O http://codespeak.net/lxml/lxml-2.3.tgz $ cd lxml-2.3 $ python selftest.py 184 tests ok. $ python selftest2.py 106 tests ok.
obspy
地震計データの処理.http://svn.geophysik.uni-muenchen.de/trac/obspy/wiki/
- インストール例
$ svn checkout https://svn.geophysik.uni-muenchen.de/svn/obspy obspy $ cd obspy $ python >>> import commands >>> list=["",'arclink','core','fissures','gse2','imaging','mseed','sac','seisan','seishub','signal','wav','xseed'] >>> for x in list: >>> if x != "": x = '.' + x >>> commands.getoutput('(cd obspy' + x + '/trunk; python setup.py develop)')
PyOpenGL
OpenGL for Python http://sourceforge.net/projects/pyopengl/
- インストール例
$ unzip PyOpenGL-3.0.1.zip $ cd PyOpenGL-3.0.1 $ python setup.py install
- テスト例
globe.py (minimum spanning tree on a sphere)を試す. http://www.treedub.org/blog/wp-content/uploads/2007/09/globe.tar.gz
$ python globe.py 20
関連情報
Wiki内
- PythonTips
- リスト/開発ツール
- リスト/UNIXパッケージ/Python
- 共有コード
Wiki外
Pythonについての情報