Categories: Uncategorized

Local Develop Env in Python

Note : Updated at 2016/12/29 This article’s topic is how to contribute local develop env in python with pyenv.
Python also has a function like bundler and Gemfile in ruby.

Environment

  • 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
    • 1.0.6-1-g0256ff0
  • virtualenv
    • 15.1.0

Install pyenv and setting

It’s easy to do.
Just run some commands like following.
$ 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.5.2
Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
Installing Python-3.5.2...
patching file Lib/venv/scripts/posix/activate.fish
Installed Python-3.5.2 to /home/ml/.pyenv/versions/3.5.2

# Refresh shim
$ pyenv rehash

# Set version to use
$ pyenv global 3.5.2

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

# Uninstall
$ pyenv uninstall 3.5.2

Library Management

We can install libraries with pip command.
And requirements.txt(actually you can choose file name) have kind of same as Gemfile.
When you install a library, executable script would be also installed mostly in python.
So bundle exec is doesn’t needed in python. And I use virtualenv in this section because pip install with pyenv affects environments which use same pyenv and same python version.
virtualenv can avoid that.
$ which pip
$HOME/.pyenv/shims/pip

# Sometimes default pip version is low.
$ pip install --upgrade pip

# Install virtualenv
pip install virtualenv

# Create virtual environment.
# "p" option is for setting python path.
# (Just python${VERSION} is enough when you use pyenv)
# no option argument is for script instal path for entering virtual environment
$ virtualenv -p python3.5 $HOME/.venv35_ml

# Enter virtual environment
# Prompt will be from "$" to "(.venv35_ml) $"
$ source $HOME/.venv35_ml/activate

# Basic way to install
$ 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

# Check installed library
$ pip freeze
Django==1.2.7
nose==1.1.2
numpy==1.9.2
urllib3==1.10.2

# Like gemrat
$ pip freeze > requirements_new.txt

# Exit from virtual environment
$ deactivate

Now you know pyenv and virtualenv provide us functions like bundler. pyenvとvirtualenvを使って機械学習用のDockerfileを作ってみた。
I tried to make Dockerfile for machine learning using pyenv and virtualenv.
(You know, Docker distinguish environment already. So, actually virtualenv is not needed.)
docker-ml-sandbox - Dockerfile for machine learning environment(scikit-learn, chainer, gensim, tensorflow, jupyter)
zuqqhi2/docker-ml-sandbox - GitHub
zuqqhi2