Linux下安装python及MySQLdb

安装所需软件包: Python-2.6.tar.bz2、MySQL-python-1.2.3c1.tar、setuptools-0.6c11.tar.gz。

安装前:

1. 定义python的安装目录:

PYTHON_DIR = /home/$USER/local/python

2. 定义安装目录:

INSTALL_DIR = /home/$USER/install


tar -C $INSTALL_DIR -xjvf Python-2.6.tar.bz2
cd $INSTALL_DIR/Python-2.6
./configure --prefix = $PYTHON_DIR
make && make install


3. 测试

  在命令行下输入python,出现python解释器即表示已经正确安装。

  在suse10或rhel5(es5)下系统默认已经装了python但版本是2.4.x;本次安装后在shell中输入

  #python

  会发现显示结果:

  # python

  Python 2.4.3 (#1, Dec 11 2006, 11:38:52)

  [GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2

  Type “help”, “copyright”, “credits” or “license” for more information.

  >>>

  版本还是2.4.x的

  解决办法:

  #cd /usr/bin

  #ll |grep python   //查看该目录下python

  #rm -rf python

  #ln -s PREFIX/Python-2.5.2/python ./python  //PREFIX为你解压python的目录

  #python

  # python

  Python 2.5.2 (#1, Dec 11 2006, 11:38:52)

  [GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2

  Type “help”, “copyright”, “credits” or “license” for more information.

  >>>

或是修改PATH变量 vim /etc/bashrc 增加 export PATH=$PYTHON_DIR/bin:${PATH}
      

OK!问题解决!

4. 安装MySQLdb:

tar -C $INSTALL_DIR -xzvf MySQL-python-1.2.3c1.tar
cd $INSTALL_DIR/MySQL-python-1.2.3c1

5. 修改配置项

mysql_config=your_mysql_dir/mysql-bin/bin/mysql_config
vi site.cfg
$PYTHON_DIR/bin/python setup.py build
$PYTHON_DIR/bin/python setup.py install --prefix = $PYTHON_DIR
6.安装
python setup.py build

sudo python setup.py install

这一步可能会提示ImportError: No module named setuptools,则需要安装setuptools模块。

7. 安装setuptool:

tar -C $INSTALL_DIR -xzvf setuptools-0.6c11.tar.gz
cd $INSTALL_DIR/setuptools-0.6c1
$PYTHON_DIR/bin/python setup.py build
$PYTHON_DIR/bin/python setup.py install

8. 回到MySQL-python-1.2.3c1目录,执行安装MySQLdb最后一步:

cd $INSTALL_DIR/MySQL-python-1.2.3c1
$PYTHON_DIR/bin/python setup.py install --prefix = $PYTHON_DIR

这一步可能会提示如下错误:

/usr/bin/ld: cannot find -lmysqlclient_r
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

9. 修改配置项,

embedded = False
threadsafe = False
static = False
vi site.cfg

再执行:

$PYTHON_DIR/bin/python setup.py install  --prefix = $PYTHON_DIR

安装完成测试:

python
import MySQLdb;

如果报错:

ImportError: libmysqlclient.so: cannot open shared object file: No such file or directory
cd ~ && vi .bash_profile

在最后增加

export LD_LIBRARY_PATH=/home/$USER/local/mysql/lib/mysql:$LD_LIBRARY_PATH

错误的原因是未能引入libmysqlclient.so.18库,
查找库文件存在,于是建立链接:
ln -s /usr/local/server/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

重启session,再测试正常安装完成!

你可能感兴趣的:(python,linux,path,library,gcc,测试)