Problems encountered during pyton Installation
I 'd like to write it myself, but I found that this buddy wrote it in quite detail. Just move it over.
Installation environment: OS X, Python 2.7.3.
MySQLdb is actually included in the MySQL-python package. Therefore, whether downloaded or searched in pip, you should search for MySQL-python.
The following describes two common installation methods for MySQLdb: download and install MySQL-python or pip.
Download MySQLdb
Download mysql-python-1.2.4b4.tar from sourceforge, decompress it, and then execute the following command in Terminal:
$ cd /Users/iFantastic/Downloads/MySQL-python-1.2.4b4
$ python setup.py install
Install MySQLdb using pip
Run the following command on the terminal:
$ pip install MySQL-python
Whether online installation or download installation, you may encounter the first error prompt:
EnvironmentError: mysql_config not found
We recommend that you use pip for installation, because the version I downloaded from sourceforge is not as good as that in pip. To upgrade mysql-python, run the following command:
$ easy_install MySQL-python --upgrade
[Note] if you do not follow the prompts below to solve the installation errors, the upgrade will also fail due to the same error cause.
Solve the mysql_config not found Error
When MySql for Mac is installed by default, the mysql_config file is generated in the/usr/local/mysql/bin directory.
Therefore, the solution is to find the site. cfg file in the MySQL-python installation package, open it, and find the following content:
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
# mysql_config = /usr/local/bin/mysql_config
Remove the first well number of the last sentence and modify it:
mysql_config = /usr/local/mysql/bin/mysql_config
Then execute:
$ python setup.py install
In general, the installation can be completed at this time, but there are still problems, which will be further elaborated below.
During pip installation, there is no way to modify the site. cfg file. Therefore, you can modify the OS X system environment variable to solve the problem of not finding mysql_config.
Modify OS X environment variables: Open the terminal and use vim to open "~ /. Bash_profile ". If vim is not installed, the hidden file is displayed and opened in a text editor. This will not be repeated here. Add the following content to. bash_profile:
PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes
VERSIONER_PYTHON_PREFER_64_BIT and VERSIONER_PYTHON_PREFER_64_BIT are selected based on their installed MySQL.
In addition, when installing pip, you can find a solution for mysql_config (I have not verified it). Enter the following command in the terminal:
$ sudo ln -s /usr/local/mysql/bin/* /usr/bin
Now, the MySQL-python package should be installed smoothly.
Solve the Reason: image not found Error
After installing the MySQL-python package, let's import MySQLdb. An error occurs. The last line of the error is Reason: image not found.
The solution is to run the following command on 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
Restart idle and import MySQLdb.
PS: If you use pip to install MySQL-python in Ubuntu, the following error occurs: mysql_config no found. We recommend that you use sudo apt-get install python-mysqldb for installation, in this way, the installation is successful.
If you use MacPorts to install MySQL, the source code packages installed by MacPorts are stored in the/opt directory. After installing MySQL, use port select -- list mysql to check the currently enabled MySQL version (MySQL of any version should not be enabled after the first installation ), use port select -- set mysql mysql_you_selected to set the enabled MySQL version, and then use pip to install mysql-config.