【さくら共有サーバー】pythonでmysqlを使えるようにする

この記事について

環境さくらスタンダードプラン、自前python2.7、MySQL for Python
目的pythonからmysqlを使えるようにする
結果setuptoolsを入れ、PYTHONPATH/LD_LIBRARY_PATHを.cshrcに設定してMySQL for Pythonをインストールできた

つまずきポイント要約

  • setup.py実行にはsetuptoolsが必要
  • PYTHONPATH未設定だと「does NOT support .pth files」エラー
  • 「This directory does not currently exist」エラーはpythonディレクトリを作れば解決

pythonでmysqlを使えるようにした際の覚書。

概要をつかむための参考ページ

【環境と前提】
・さくらスタンダードプラン
・「/home/username/local」に自前のpython2.7がインストール済み

1.pythonでmysqlを利用するためドライバをインストール

ドライバは何種類かある中で「MySQL for Python」を選択。デファクトスタンダードらしい。

こちらからダウンロード(この時のファイル名、MySQL-python-1.2.3.tar.gz)

ダウンロード先は「/home/username/local」で、次に解凍。
解凍したフォルダに移動してセットアップを実行。

参考:こちら

セットアップコマンド

% python setup.py install --home=~/local

Traceback (most recent call last):
  File "setup.py", line 5, in <module>
    from setuptools import setup, Extension
ImportError: No module named setuptools

エラーが出る。

setuptoolsがないとのことで、インストールする。

参考:Pythonで一番最初に入れるべきパッケージ
  こちらも参考

setuptoolsは、こちらからダウンロード

ダウンロード後、解凍し、フォルダの中へ移動。

コマンドを実行。

python ez_setup.py

インストールできたことを確認するため、コマンドを打って確認

easy_install

エラーは出るけど、コマンド自体が存在していることを確認。

setuptoolsがインストールできたので、「MySQL-python」に移動してsetupをリトライ。

コマンド

python setup.py install --home=~/local

今度は別のエラーが出る。

エラー内容

% python setup.py install --home=~/local

running install
Checking .pth file support in /home/username/local/lib/python/
/home/username/local/bin/python -E -c pass
TEST FAILED: /home/username/local/lib/python/ does NOT support .pth files
error: bad install directory or PYTHONPATH

You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /home/username/local/lib/python/

and your PYTHONPATH environment variable currently contains:

    ''

Here are some of your options for correcting the problem:

* You can choose a different installation directory, i.e., one that is
  on PYTHONPATH or supports .pth files

* You can add the installation directory to the PYTHONPATH environment
  variable.  (It must then also be on PYTHONPATH whenever you run
  Python and want to use the package(s) you are installing.)

* You can set up the installation directory to support ".pth" files by
  using one of the approaches described here:

  https://pythonhosted.org/setuptools/easy_install.html#custom-installation-locations

Please make the appropriate changes for your system and try again.

どうもPYTHONPATHがエラーの原因のようなので、PYTHONPATHの設定を変更する。

ホームディレクトリにある「.cshrc」に以下を追加。
参考:こちら

setenv PYTHONPATH $HOME/local/lib/python:$HOME/local/lib/python/site-packages

setenv LD_LIBRARY_PATH $HOME/local/lib

※実際には「$HOME/local/lib/python」というフォルダは存在しない。あるのは「$HOME/local/lib/python2.7」。だけど構わない。

パスの確認は、コンソール画面で

python
 >>>import sys
 >>>sys.path

サーバーに再接続して、ふたたびsetupを試みる。

コマンド

python setup.py install --home=~/local

うまくいった。

おまけ。
別の機会に「MySQL for Python」をインストールしたときは、こんなエラーが出た。

エラー内容

% python setup.py install --home=~/local
running install
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 2] No such file or directory: '/home/workroom/local/lib/python/test-easy-install-35897.write-test'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /home/workroom/local/lib/python/

This directory does not currently exist.  Please create it and try again, or
choose a different installation directory (using the -d or --install-dir
option).

「python」という名前のディレクトリを作ってあげたら解決した。

関連記事

タイトルとURLをコピーしました