Tech Tips

  1. 未分類
  2. 113 view

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.


  • 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:// ~/.pyenv

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

$ source ~/.bashrc

$ pyenv install 3.5.2
Downloading Python-3.5.2.tar.xz...
Installing Python-3.5.2...
patching file Lib/venv/scripts/posix/
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

# 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

# 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 install for numpy
Successfully installed numpy-1.9.2

# requirements.txt
$ vim requirements.txt

$ 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 install for django
   Running install for urllib3
   Running install for nose
 Successfully installed django-1.2.7 nose-1.1.2 urllib3-1.10.2

# Check installed library
$ pip freeze

# 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.)

未分類 recent post

  1. Install sbt 1.0.0 and run sample template

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [Node.js]How to write batch script with Node.…

  5. Web Server Contribution by Vagrant and Ansibl…