LoginSignup
7
9

More than 5 years have passed since last update.

Pythonをローカルインストール

Last updated at Posted at 2017-06-08

Pythonをローカルインストールする際にはまったのでメモ.
管理者権限がない場合(sudoが使用できない場合)を想定している.
Python3.6.1をインストールしたが,他のバージョンにも適用可能であると考えられる.

環境

  • OS: Debian 8.8 (Jessie)
  • tcl/tk 8.6.6
  • Python 3.6.1

\$HOME/srcにダウンロード・展開し,\$HOME/localにインストールするものとして話を進める.

tcl/tkのインストール

まず,matplotlibにtkinterが必要なので前もってtck/tkをインストールしておく.
もうすでにインストールされており,インストールされたlibディレクトリとincludeディレクトリがわかっている場合は読み飛ばしてよい.
自身の場合はインストールディレクトリがわからなかった(かつバージョンが古かった)ため,ローカルにインストールしている.

tcl/tkの最新バージョンは8.6.6なので,こちらをインストールする.
公式サイト内のリンクからダウンロードする.

まずtclをインストールする.

cd $HOME/src
wget https://sourceforge.net/projects/tcl/files/tcl8.6.6-src.tar.gz/download -O tcl8.6.6.tar.gz
tar xzvf tcl8.6.6.tar.gz
cd tcl8.6.6
mkdir build; cd build
../unix/configure --prefix=$HOME/local/
make
make install

同様にtkもインストールする.

cd $HOME/src
wget https://sourceforge.net/projects/tcl/files/tk8.6.6-src.tar.gz/download -O tk8.6.6.tar.gz
tar xzvf tk8.6.6.tar.gz
cd tk8.6.6
mkdir build; cd build
../unix/configure --prefix=$HOME/local/
make
make install

Pythonのインストール

次にPythonをインストールする.
何も考えずにインストールするとtkinterがうまくいかなくなるので注意.

Pythonの最新バージョンは3.6.1なのでこちらをインストールする.
公式サイト内のリンクからダウンロードする.

cd $HOME/src
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
tar xfv Python-3.6.1.tar.xz
cd Python-3.6.1
mkdir build; cd build
../configure --prefix=$HOME/local/ --with-tcltk-includes="-I/$HOME/local/include" \
   --with-tcltk-libs="-L/$HOME/local/lib -ltcl8.6 -ltk8.6"
make
make install

注意すべきはconfigureの実行の際にオプションでincludeディレクトリとlibディレクトリを指定することである.
これがないとPythonインストール後の実行時にエラーを吐く.
エラーメッセージとしては
bash
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'

みたいなのが得られる.

インストールの際のメッセージの最後の方に

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_gdbm                 _lzma
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

と出ているのでこれらのパッケージが必要な場合はまた別のオプションを指定する必要がある.
困った場合は展開したPythonディレクトリ内にあるsetup.pyのスクリプトを参照するとよいだろう.

最後にpipをインストールすれば一応インストールは完了である.

wget https://bootstrap.pypa.io/get-pip.py
$HOME/local/bin/python3 get-pip.py

動くかの確認としてplt.show()などしてみるとよいだろう.

7
9
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
7
9