[Python] MySQLdb (i.e. Mysql-python package) Installation Guide in OS X

Source: Internet
Author: User
Tags mysql version

Installation environment: OS X operating system, Python 2.7.3.

MySQLdb is actually included in the Mysql-python package, so you should search for Mysql-python whether you download or search in PIP.

The following is a description of the MySQLdb two common installation methods, download installation or PIP installation Mysql-python. 


Download MySQLdb

In SourceForge you can download Mysql-python-1.2.4b4.tar, unzip it after downloading, and then execute the following command in Terminal Terminal:


NEW-HOST-3:MYSQL-PYTHON-1.2.4B4 ifantastic$ python setup.py install


Installing MYSQLDB with PIP

Execute in Terminal:

new-host-3:~ ifantastic$ pip Install Mysql-python


Whether you are installing online or downloading an installation, you may encounter the first error message:

Environmenterror:mysql_config not found


It is recommended to use PIP installation because the version I downloaded from SourceForge is not as new as the PIP. If you want to upgrade the Mysql-python version, you can use the following command:

$ easy_install Mysql-python--upgrade


[Note] If you do not follow the instructions below to resolve the installation errors, the upgrade will also fail for the same cause of error.


Resolve Mysql_config not found error


When you install the MAC version of MYSQL by default, the Mysql_config file is generated in the/usr/local/mysql/bin directory.

Therefore, the solution to download the installation is: Locate the Site.cfg file in the Mysql-python installation package, open it, and locate the following:

# The path to mysql_config.# only with this if mysql_config are not on your path, or you had some weird# setup that require s it.# mysql_config =/usr/local/bin/mysql_config


Remove the last sentence first and modify it as follows:

Mysql_config =/usr/local/mysql/bin/mysql_config


Then execute:

$ python setup.py Install


Generally speaking, the installation can be completed at this point, but there are still problems, as described below.

There is no way to modify the Site.cfg file when using pip installation , so you can resolve errors that cannot be found by modifying OS X's system environment variables.

  Modify OS X environment variable : Open terminal, use VIM in terminal to open "~/.bash_profile", if no vim is installed, then show hidden file Open with text editor, the operation here is no longer described. In. Bash_profile, add the following:

Path= "/usr/local/mysql/bin:${path}" Export Pathexport dyld_library_path=/usr/local/mysql/lib/export VERSIONER_ Python_prefer_64_bit=noexport Versioner_python_prefer_32_bit=yes


Among them versioner_python_prefer_64_bit and versioner_python_prefer_64_bit according to their own installation of MySQL to choose.

In addition, a PIP installation can not find the Mysql_config solution (I did not verify), enter the following command in the terminal:

$ sudo ln-s/usr/local/mysql/bin/*/usr/bin


Well, here, Mysql-python package should be basically smooth installation.


Resolve Reason:image not found error


After installing the Mysql-python package, let us import mysqldb, there is an error, the last line of error is written reason:image not found.

The workaround is to execute at the terminal:

$ sudo ln-s/usr/local/mysql/lib/libmysqlclient.18.dylib/usr/lib/libmysqlclient.18.dylib$ sudo ln-s/usr/local/mysql /lib/usr/local/mysql/lib/mysql


Then restart idle to successfully import MySQLdb.

PS: Using PIP to install Mysql-python under Ubuntu also appears mysql_config no found error, it is recommended to use sudo apt-get install python-mysqldb for installation, so that directly can be installed successfully.

If you use MacPorts to install Mysql,macports installed source packages are stored in the/OPT directory, after the installation of MySQL, use port select--list MySQL to view the currently enabled MySQL version (after the initial installation is not Any version of MySQL is enabled, then use the port select--set MySQL mysql_you_selected to set the MySQL version enabled, and then use PIP to install without worrying about the mysql-config problem.

[Python] MySQLdb (that is, Mysql-python package) Installation Guide in OS X

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.