LoginSignup
4
4

More than 5 years have passed since last update.

UbuntuにdirenvでPython3系の環境を作りました。

Last updated at Posted at 2016-01-25

direnvを使うとPythonの環境構築が楽という話を聞きまして、Ubuntuに3系の環境を作りました。VagrantにUbuntu14.04のboxを入れるところからです。

ubuntuのboxが入ったVagrantをupする

boxをとってくる

vagrant box add ubuntu-14.04 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
vagrant init ubuntu-14.04
vagrant up

vagrantにログイン

vagrant ssh

Ubuntuにパッケージをインストールする

パッケージをアップデート
sudo apt-get update

pipとpythonzを入れる

pipとpythonzをインストールします。

pipとpythonzをインストール
sudo apt-get install build-essential zlib1g-dev libbz2-dev libssl-dev libreadline-dev libncurses5-dev libsqlite3-dev libgdbm-dev libdb-dev libexpat-dev libpcap-dev liblzma-dev libpcre3-dev curl python-pip
curl -kL https://raw.github.com/saghul/pythonz/master/pythonz-install | bash
echo '[[ -s $HOME/.pythonz/etc/bashrc ]] && source $HOME/.pythonz/etc/bashrc' >> $HOME/.bashrc
exec $SHELL

virtualenvを入れる

sudo pip install virtualenv
Downloading/unpacking virtualenv
  Downloading virtualenv-13.1.2-py2.py3-none-any.whl (1.7MB): 1.7MB downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...

direnvを入れる

curl -L -o direnv https://github.com/zimbatm/direnv/releases/download/v2.5.0/direnv.linux-amd64
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   142    0   142    0     0     22      0 --:--:--  0:00:06 --:--:--    37
  0     0    0   602    0     0     93      0 --:--:--  0:00:06 --:--:--    93
100 3579k  100 3579k    0     0   236k      0  0:00:15  0:00:15 --:--:--  865k
sudo install direnv /usr/local/bin
echo 'type direnv > /dev/null 2>&1 && eval "$(direnv hook bash)" ' >> $HOME/.bashrc
exec $SHELL
pythonz list -a
略
     2.7.8
     2.7.9
     2.7.10
     3.0
     3.0.1
     3.1
略

デフォルトだと2.7.6系が入っているみたいです。

python -V
Python 2.7.6

python3系をインストール

pythonz install 3.4.3

Downloading Python-3.4.3.tgz as /home/vagrant/.pythonz/dists/Python-3.4.3.tgz
########################################################################## 100%
Extracting Python-3.4.3.tgz into /home/vagrant/.pythonz/build/CPython-3.4.3

This could take a while. You can run the following command on another shell to track the status:
  tail -f /home/vagrant/.pythonz/log/build.log

Installing CPython-3.4.3 into /home/vagrant/.pythonz/pythons/CPython-3.4.3

Installed CPython-3.4.3 successfully.
pythonz locate 3.4.3
/home/vagrant/.pythonz/pythons/CPython-3.4.3/bin/python3
pip -V
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

Python3を使うディレクトリを指定

pwd
/home/vagrant

/home/vagrant/sand_box というディレクトリでpython3.4.3を使えるようにします。

sand_boxディレクトリでpythonが使えるように設定
echo 'layout python $(pythonz locate 3.4.3)' > sand_box/.envrc
direnv allow sand_box

一旦Vagrantをぬける

exit

Vagrantに入り直す

vagrant ssh

このログアウト手順はいらないかも?

Python3系のパッケージがインストールされればOK

ここでsand_boxというディレクトリに移動するとインストールが自動的に始まります。

cd sand_box
direnv: loading .envrc
Running virtualenv with interpreter /home/vagrant/.pythonz/pythons/CPython-3.4.3/bin/python3
Using base prefix '/home/vagrant/.pythonz/pythons/CPython-3.4.3'
New python executable in /home/vagrant/sand_box/.direnv/python-3.4.3/bin/python3
Also creating executable in /home/vagrant/sand_box/.direnv/python-3.4.3/bin/python
Installing setuptools, pip, wheel...done.
direnv: export +VIRTUAL_ENV ~PATH

試しにPrettyTableをいれてみる

pip install PrettyTable
Collecting PrettyTable
  Downloading prettytable-0.7.2.tar.bz2
Building wheels for collected packages: PrettyTable
  Running setup.py bdist_wheel for PrettyTable
  Stored in directory: /home/vagrant/.cache/pip/wheels/41/46/ec/ec86e65e50e9f4be52547a3bedd46077e7414c53d2ed0418fd
Successfully built PrettyTable
Installing collected packages: PrettyTable
Successfully installed PrettyTable-0.7.2

普通に入りました。もし正しく3.4.3経由でpipが呼ばれていないとうまく入りません。

Python3系が使われているか確認

python -V
Python 3.4.3

以下おまけ、ほかにもディレクトリを作る

適当に/var/www/sand_boxというディレクトリを作ってみてそこに他のバージョンのPythonを入れてみました。

sudo mkdir /var/www
sudo chown vagrant:vagrant /var/www

ディレクトリに移動前はPython2系です。

python -V
Python 2.7.6

pythonz install 3.2.4
Downloading Python-3.2.4.tgz as /home/vagrant/.pythonz/dists/Python-3.2.4.tgz
########################################################################## 100%
Extracting Python-3.2.4.tgz into /home/vagrant/.pythonz/build/CPython-3.2.4

This could take a while. You can run the following command on another shell to track the status:
  tail -f /home/vagrant/.pythonz/log/build.log

Installing CPython-3.2.4 into /home/vagrant/.pythonz/pythons/CPython-3.2.4

Installed CPython-3.2.4 successfully.
mkdir sand_box

/var/www/sand_boxに移動すると3.2.4系のファイルがインストールされます。

cd sand_box/
direnv: loading .envrc
Running virtualenv with interpreter /home/vagrant/.pythonz/pythons/CPython-3.2.4/bin/python3
New python executable in /var/www/sand_box/.direnv/python-3.2.4/bin/python3
Also creating executable in /var/www/sand_box/.direnv/python-3.2.4/bin/python
Installing setuptools, pip, wheel...done.
direnv: export +VIRTUAL_ENV ~PATH
pip install PrettyTable
Collecting PrettyTable
Installing collected packages: PrettyTable
Successfully installed PrettyTable-0.7.2

参考

参考サイト色々あったのにメモってなかった\(^o^)/
http://qiita.com/jnotoya/items/ca9a0dfee6b9f084f4da

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4