zuqqhi2のIT日記

プログラミング + アカデミック + 何か面白いこと

   Apr 09

python ローカル開発環境 構築

by zuqqhi2 at 2015年4月9日
Pocket

pyenvを使って python ローカル開発環境 を構築する方法を簡単にまとめた。
pythonでもRubyのbundlerやGemfileと似た機構がある。
pythonでもバージョンやライブラリの管理が簡単にできるため、
これを期にpythonを試してみるのもあり。

環境

  • OS
    • Linux version 3.2.0-64-generic (buildd@kissel) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #97-Ubuntu SMP Wed Jun 4 22:04:21 UTC 2014

pyenvのインストールと設定

これは非常に簡単にできる。

$ git clone git://github.com/yyuu/pyenv.git ~/.pyenv

$ vim ~/.bashrc
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"

$ source ~/.bashrc

$ pyenv install 3.3.3
Downloading Python-3.3.3.tgz...
-> https://yyuu.github.io/pythons/30b60839bfe0ae8a2dba11e909328459bb8ee4a258afe7494b06b2ceda080efc
Installing Python-3.3.3...
patching file ./Modules/readline.c
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Installed Python-3.3.3 to $HOME/.pyenv/versions/3.3.3

# shimのリフレッシュ
$ pyenv rehash

# 使用するバージョン指定
$ pyenv global 3.3.3

$ which python
$HOME/.pyenv/shims/python

# アンインストール方法
$ pyenv uninstall 3.3.3

ライブラリの管理

pipコマンドを使ってライブラリのインストールができる。
Gemfileに相当するのはrequirements.txtだ(実際には名前はなんでも良い)。
また、pythonのライブラリは基本的には実行できるスクリプトもインストールされることが
ほとんどのようで、bundle execのようなものは必要ないらしい。

$ which pip
$HOME/.pyenv/shims/pip

# 基本的なライブラリのインストール方法
$ pip install numpy
Collecting numpy
  Downloading numpy-1.9.2.tar.gz (4.0MB)
    100% |████████████████████████████████| 4.0MB 91kB/s
Installing collected packages: numpy
  Running setup.py install for numpy
Successfully installed numpy-1.9.2


# requirements.txtを利用した場合
$ vim requirements.txt
django<1.3
urllib3
nose==1.1.2

$ pip install -r requirements.txt
 Collecting django<1.3 (from -r requirements.txt (line 1))
   Downloading Django-1.2.7.tar.gz (6.4MB)
     100% |████████████████████████████████| 6.4MB 50kB/s
 Collecting urllib3 (from -r requirements.txt (line 2))
   Downloading urllib3-1.10.2.tar.gz (132kB)
     100% |████████████████████████████████| 135kB 989kB/s
 Collecting nose==1.1.2 (from -r requirements.txt (line 3))
   Downloading nose-1.1.2.tar.gz (729kB)
     100% |████████████████████████████████| 733kB 416kB/s
 Installing collected packages: django, urllib3, nose
   Running setup.py install for django
   Running setup.py install for urllib3
   Running setup.py install for nose
 Successfully installed django-1.2.7 nose-1.1.2 urllib3-1.10.2

# インストールされているライブラリの確認
$ pip freeze
Django==1.2.7
nose==1.1.2
numpy==1.9.2
urllib3==1.10.2

pythonでも、phpのcomposerやrubyのbundlerのようなことができることがわかったと思う。

参考

Related Posts

Pocket

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.