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

   Apr 09

Local Develop Env in Python

by zuqqhi2 at 2015年4月9日

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://github.com/yyuu/pyenv.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...
-> 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

# 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 setup.py 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 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

# Like gemrat
$ pip freeze > requirements_new.txt

# Exit from virtual environment
$ deactivate

Now you know pyenv and virtualenv provide us functions like bundler.

I tried to make Dockerfile for machine learning using pyenv and virtualenv.
(You know, Docker distinguish environment already. So, actually virtualenv is not needed.)

Reference(Only Japanese Sites)

Related Posts

  • <!--:ja-->[PCL][Python][CPP]Python PCL (Point Cloud Library)のインストールとサンプル実行<!--:--><!--:en-->[PCL][Python][CPP]Install Python PCL(Point Cloud Library) And Run Sample Program<!--:-->2014年8月3日 [PCL][Python][CPP]Install Python PCL(Point Cloud Library) And Run Sample Program 概要 PCL(点群処理ライブラリ)のが出たということで触って見た。 まだ python pcl でできる部分は少ないみたい。 Env Linux ubuntu 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 […]
  • 2013年5月26日 [Python]Install python urllib 2 本のコードを写経していて python urllib 2が必要なのだが、 なぜかpipでurllib2を検索しても出て来なくて、 urllib3しかないっぽいからこっちを入れてみる。 […]
  • [Machine Learning]Created docker image including python ML libraries2017年1月8日 [Machine Learning]Created docker image including python ML libraries [gitflow]How to install home directory git-flowのインストール 以下のようにやるとうまく動いた。 git-flowの参考URL http://qiita.com/items/b4d9331ec3952dbe5205 git-flowのインストール It seems to work […]
  • 2015年3月24日 Install ElasticSearch on ubuntu elasticsearch インストール […]
  • 2015年3月29日 Install Scala and Sbt Scala Sbt インストール Scalaはオブジェクト指向言語と関数型言語の両方の特徴を併せ持つハイブリッドな言語。 SbtはScalaのビルドツール。プロジェクト・パッケージ・ライブラリ管理もしてくれる便利なツール。 環境 OSLinux […]

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