IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

Python環境構築

  • pyenvのインストール
wk$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/Users/foo/.pyenv'...

fatal: Unable to look up github.com (port 9418) (nodename nor servname provided, or not known)
wk$ 
wk$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/Users/foo/.pyenv'...
remote: Counting objects: 10760, done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 10760 (delta 1), reused 0 (delta 0), pack-reused 10746
Receiving objects: 100% (10760/10760), 1.87 MiB | 360.00 KiB/s, done.
Resolving deltas: 100% (7648/7648), done.
Checking connectivity... done.
  • rehashのインストール
wk$ git clone https://github.com/yyuu/pyenv-pip-rehash.git ~/.pyenv/plugins/pyenv-pip-rehash
Cloning into '/Users/foo/.pyenv/plugins/pyenv-pip-rehash'...
remote: Counting objects: 127, done.
remote: Total 127 (delta 0), reused 0 (delta 0), pack-reused 127
Receiving objects: 100% (127/127), 16.67 KiB | 0 bytes/s, done.
Resolving deltas: 100% (57/57), done.
Checking connectivity... done.
  • virtualenvのインストール
wk$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
Cloning into '/Users/foo/.pyenv/plugins/pyenv-virtualenv'...
remote: Counting objects: 1136, done.
remote: Compressing objects: 100% (74/74), done.
remote: Total 1136 (delta 45), reused 0 (delta 0), pack-reused 1062
Receiving objects: 100% (1136/1136), 351.95 KiB | 199.00 KiB/s, done.
Resolving deltas: 100% (744/744), done.
Checking connectivity... done.
  • プロファイルの編集
wk$ ls ~/.bash_profile 
/Users/foo/.bash_profile
wk$ less ~/.bash_profile 
wk$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
wk$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
wk$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
wk$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
wk$ source .bash_profile
  • pyenvインストールの確認
MacBook-Air:work03 foo$ pyenv 
pyenv 20150719
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/yyuu/pyenv#readme
  • インストール可能な一覧表示
wk$ pyenv install -l
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5
  2.5.1
  2.5.2
  2.5.3
  2.5.4
  2.5.5
  2.5.6
  2.6.6
  2.6.7
  2.6.8
  2.6.9
  2.7-dev
  2.7
  2.7.1
  2.7.2
  2.7.3
  2.7.4
  2.7.5
  2.7.6
  2.7.7
  2.7.8
  2.7.9
  2.7.10
  3.0.1
  3.1
  3.1.1
  3.1.2
  3.1.3
  3.1.4
  3.1.5
  3.2-dev
  3.2
  3.2.1
  3.2.2
  3.2.3
  3.2.4
  3.2.5
  3.2.6
  3.3.0
  3.3-dev
  3.3.1
  3.3.2
  3.3.3
  3.3.4
  3.3.5
  3.3.6
  3.4.0
  3.4-dev
  3.4.1
  3.4.2
  3.4.3
  3.5.0b1
  3.5.0b2
  3.5.0b3
  3.5-dev
  3.6-dev
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1
  anaconda-1.6.0
  anaconda-1.6.1
  anaconda-1.7.0
  anaconda-1.8.0
  anaconda-1.9.0
  anaconda-1.9.1
  anaconda-1.9.2
  anaconda-2.0.0
  anaconda-2.0.1
  anaconda-2.1.0
  anaconda-2.2.0
  anaconda-2.3.0
  anaconda3-2.0.0
  anaconda3-2.0.1
  anaconda3-2.1.0
  anaconda3-2.2.0
  anaconda3-2.3.0
  ironpython-dev
  ironpython-2.7.4
  ironpython-2.7.5
  jython-dev
  jython-2.5.0
  jython-2.5-dev
  jython-2.5.1
  jython-2.5.2
  jython-2.5.3
  jython-2.5.4-rc1
  jython-2.7.0
  miniconda-2.2.2
  miniconda-3.0.0
  miniconda-3.0.4
  miniconda-3.0.5
  miniconda-3.3.0
  miniconda-3.4.2
  miniconda-3.7.0
  miniconda-3.8.3
  miniconda-3.9.1
  miniconda3-2.2.2
  miniconda3-3.0.0
  miniconda3-3.0.4
  miniconda3-3.0.5
  miniconda3-3.3.0
  miniconda3-3.4.2
  miniconda3-3.7.0
  miniconda3-3.8.3
  miniconda3-3.9.1
  pypy-c-jit-latest
  pypy-c-nojit-latest
  pypy-dev
  pypy-portable-2.3.1
  pypy-portable-2.4
  pypy-portable-2.5
  pypy-portable-2.5.1
  pypy-portable-2.6.0
  pypy-1.5-src
  pypy-1.5
  pypy-1.6
  pypy-1.7-dev
  pypy-1.7
  pypy-1.8-dev
  pypy-1.8
  pypy-1.9-dev
  pypy-1.9
  pypy-2.0-dev
  pypy-2.0-src
  pypy-2.0
  pypy-2.0.1-src
  pypy-2.0.1
  pypy-2.0.2-src
  pypy-2.0.2
  pypy-2.1-src
  pypy-2.1
  pypy-2.2-src
  pypy-2.2
  pypy-2.2.1-src
  pypy-2.2.1
  pypy-2.3-src
  pypy-2.3
  pypy-2.3.1-src
  pypy-2.3.1
  pypy-2.4.0-src
  pypy-2.4.0
  pypy-2.4-beta1-src
  pypy-2.4-beta1
  pypy-2.5.0-src
  pypy-2.5.0
  pypy-2.5.1-src
  pypy-2.5.1
  pypy-2.6.0-src
  pypy-2.6.0
  pypy3-dev
  pypy3-portable-2.3.1
  pypy3-portable-2.4
  pypy3-2.3.1-src
  pypy3-2.3.1
  pypy3-2.4.0-src
  pypy3-2.4.0
  stackless-dev
  stackless-2.7-dev
  stackless-2.7.2
  stackless-2.7.3
  stackless-2.7.4
  stackless-2.7.5
  stackless-2.7.6
  stackless-2.7.7
  stackless-2.7.8
  stackless-3.2-dev
  stackless-3.2.2
  stackless-3.2.5
  stackless-3.3-dev
  stackless-3.3.5
  stackless-3.4.1
  • Pythonの3.4.3のインストール
wk$ pyenv install 3.4.3
Downloading readline-6.3.tar.gz...
-> http://ftpmirror.gnu.org/readline/readline-6.3.tar.gz
Installing readline-6.3...
Installed readline-6.3 to /Users/foo/.pyenv/versions/3.4.3

Downloading Python-3.4.3.tgz...
-> https://yyuu.github.io/pythons/4281ff86778db65892c05151d5de738d
Installing Python-3.4.3...
Installed Python-3.4.3 to /Users/foo/.pyenv/versions/3.4.3
  • インストールされたバージョンの確認
wk$ pyenv versions
* system (set by /Users/foo/.pyenv/version)
  3.4.3
  • pipの確認 (python 2には入っていない模様)
wk$ pip
pyenv: pip: command not found

The `pip' command exists in these Python versions:
  3.4.3
  • Pythonのバージョン変更
wk$ pyenv global 3.4.3
wk$ pyenv versions
  system
* 3.4.3 (set by /Users/foo/.pyenv/version)
  • pipの確認
wk$ pip

Usage:   
  pip <command> [options]

Commands:
  install                     Install packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  zip                         DEPRECATED. Zip individual packages.
  unzip                       DEPRECATED. Unzip individual packages.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output.
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
  --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
  • numpyのインストール
wk$ pip install numpy
You are using pip version 6.0.8, however version 7.1.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting numpy
  Downloading numpy-1.9.2-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.7MB)
    100% |################################| 3.7MB 85kB/s 
Installing collected packages: numpy

Successfully installed numpy-1.9.2
  • pandasのインストール
wk$ pip install pandas
You are using pip version 6.0.8, however version 7.1.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pandas
  Downloading pandas-0.16.2-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (7.4MB)
    100% |################################| 7.4MB 47kB/s 
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in ./.pyenv/versions/3.4.3/lib/python3.4/site-packages (from pandas)
Collecting python-dateutil>=2 (from pandas)
  Downloading python_dateutil-2.4.2-py2.py3-none-any.whl (188kB)
    100% |################################| 192kB 647kB/s 
Collecting pytz>=2011k (from pandas)
  Downloading pytz-2015.4-py2.py3-none-any.whl (475kB)
    100% |################################| 475kB 416kB/s 
Collecting six>=1.5 (from python-dateutil>=2->pandas)
  Downloading six-1.9.0-py2.py3-none-any.whl
Installing collected packages: six, pytz, python-dateutil, pandas
Successfully installed pandas-0.16.2 python-dateutil-2.4.2 pytz-2015.4 six-1.9.0
  • 作業ディレクトリに移動
wk$ python -V
Python 3.4.3
wk$ cd python/test01/
MacBook-Air:test01 foo$ python
Python 3.4.3 (default, Jul 20 2015, 20:49:52) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
  • numpyの確認
>>> import numpy
>>> [1,2,3]
>>> numpy.mean([1,2,3])
2.0
  • pandasの確認

http://nbviewer.ipython.org/urls/bitbucket.org/hrojas/learn-pandas/raw/master/lessons/01%20-%20Lesson.ipynb

>>> import pandas
>>> names = ['Bob','Jessica','Mary','John','Mel']
>>> births = [968, 155, 77, 578, 973]
>>> BabyDataSet = list(zip(names,births))   # Python3よりzipだけではダメでlistを使う必要がある(下記補足参照)
>>> df = pandas.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
>>> df
     Names  Births
0      Bob     968
1  Jessica     155
2     Mary      77
3     John     578
4      Mel     973
>>> 
  • 補足

Python 3 - Zip is an iterator in a pandas dataframe - Stack Overflow