• MySQLdb安装指南


    默认情况下,MySQLdb包是没有安装的,不信? 看到类似下面的代码你就信了。
    -bash-3.2# /usr/local/python2.7.3/bin/python get_cnblogs_news.py
    Traceback (most recent call last):
      File "get_cnblogs_news.py", line 9, in <module>
        import MySQLdb
    ImportError: No module named MySQLdb

    这时我们就不得不安装MySQLdb包了。安装其实也挺简单,具体步骤如下:
     1、下载 MySQL for Python
    地址:http://sourceforge.net/projects/mysql-python/files/mysql-python/
    我这里安装的是1.2.3版本
    wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

    2、解压
    tar zxvf MySQL-python-1.2.3.tar.gz

    3、安装
    $ cd MySQL-python-1.2.3
    $ python setup.py build

    不过在python setup.py build时报错:

    ImportError: No module named setuptools

    ubuntu下安装:

    sudo apt-get install python-setuptools

    python-setuptools : Python Distutils Enhancements (setuptools compatibility)

     

    然后再次python setup.py build,又报错:

    EnvironmentError: mysql_config not found

    因为mysql_config是属于MySQL开发用的文件,而使用apt-get安装的MySQL是没有这个文件的,于是在包安装器里面寻找

    sudo apt-get install  libmysqld-dev

    libmysqld-dev : MySQL embedded database development files

    再次运行python setup.py build,报错:

    building ‘_mysql’ extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,’final’,0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-i686-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
    In file included from _mysql.c:29:0:
    pymemcompat.h:10:20: fatal error: Python.h: No such file or directory

    解决方案,

    sudo apt-get install python-dev

    python-dev : header files and a static library for Python (default)

     

    然后就按照README里的:

      pythonsetup.pybuild
    sudo python setup.py install

    测试:

    >>>import MySQLdb

    没有报错即可。


    参考:

    http://blog.csdn.net/guzicheng/article/details/5884106
    http://be-evil.org/post-185.html
    http://hi.baidu.com/hevensun/blog/item/3b6e313fe8515df154e7238f.html
    http://stackoverflow.com/questions/1449130/how-to-install-mysqldb-package-importerror-no-module-named-setuptools
    http://www.cnblogs.com/objdump/archive/2011/11/03/mysql_python_install_error.html

  • 相关阅读:
    nginx+vue刷新404
    java-Object类的解析(持续更新)
    Python源码学习(六)-PyCodeObject初探
    经典算法之不定方程问题
    MySql中的视图的概念及应用
    数据结构之 折半插入排序
    mahout算法源码分析之Itembased Collaborative Filtering实战
    【Android】为Android虚拟机创建SDCard
    30个酷毙的交互式网站(HTML5+CSS3)
    项目总结——也谈svn版本库迁移
  • 原文地址:https://www.cnblogs.com/tcming/p/6963339.html
Copyright © 2020-2023  润新知