接觸過virtualenv後,想在這個虛擬環境中安裝獨立的開發環境。在安裝MySQLdb時遇到錯誤python
pc 09:09:30
File "/home/pc/work/VENV/py3/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 50, in run_setup
lambda: execfile(
File "/home/pc/work/VENV/py3/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 100, in run
return func()
File "/home/pc/work/VENV/py3/local/lib/python2.7/site-packages/setuptools/sandbox.py", line 52, in <lambda>
{'__file__':setup_script, '__name__':'__main__'}
File "setup.py", line 17, in <module>
File "/tmp/easy_install-FtC77Z/MySQL-python-1.2.5/setup_posix.py", line 43, in get_config
File "/tmp/easy_install-FtC77Z/MySQL-python-1.2.5/setup_posix.py", line 25, in mysql_config
EnvironmentError: mysql_config not foundmysql
在國內的網上找了好長時間也沒有找到辦法,大多數是講如何安裝mysql-python的。先說一下解決過程吧linux
我是在ubuntu14上安裝有virtualenv,在全局環境中安裝了mysql-server,安裝命令sql
sudo apt-get install mysql-server
後續過程都是在虛擬環境中進行的。個人是py3ubuntu
開始找到要想使用mysql須要安裝libmysqlclient-dev
python2.7
安裝事後在安裝mysql-python仍是出錯ui
(py3)pc@pc-virtual-machine:~/work/VENV/py3/bin$ pip install mysql-python
Downloading/unpacking mysql-python
Downloading MySQL-python-1.2.5.zip (108kB): 108kB downloaded
Running setup.py (path:/home/pc/work/VENV/py3/build/mysql-python/setup.py) egg_info for package mysql-python
Installing collected packages: mysql-python
Running setup.py install for mysql-python
building '_mysql' extension
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -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 -DTAOCRYPT_DISABLE_X86ASM -g -DNDEBUG
_mysql.c:29:20: fatal error: Python.h: No such file or directory
#include "Python.h"
^
compilation terminated.
error: command 'i686-linux-gnu-gcc' failed with exit status 1
Complete output from command /home/pc/work/VENV/py3/bin/python -c "import setuptools, tokenize;__file__='/home/pc/work/VENV/py3/build/mysql-python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-V4wHKY-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/pc/work/VENV/py3/include/site/python2.7:
running installspa
running buildprototype
running build_pydebug
creating build
creating build/lib.linux-i686-2.7
copying _mysql_exceptions.py -> build/lib.linux-i686-2.7
creating build/lib.linux-i686-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-i686-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-i686-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-i686-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-i686-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-i686-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-i686-2.7/MySQLdb
creating build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-i686-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-i686-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-i686-2.7
i686-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -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 -DTAOCRYPT_DISABLE_X86ASM -g -DNDEBUG
_mysql.c:29:20: fatal error: Python.h: No such file or directory
#include "Python.h"
^
compilation terminated.
error: command 'i686-linux-gnu-gcc' failed with exit status 1
----------------------------------------
Cleaning up...
Command /home/pc/work/VENV/py3/bin/python -c "import setuptools, tokenize;__file__='/home/pc/work/VENV/py3/build/mysql-python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-V4wHKY-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/pc/work/VENV/py3/include/site/python2.7 failed with error code 1 in /home/pc/work/VENV/py3/build/mysql-python
Storing debug log for failure in /home/pc/.pip/pip.log
看到若是沒有安裝python-dev,須要安裝這個就開始安裝
sudo apt-get install python-dev
此時在安裝mysql-python就能夠了,因此安裝步驟是
1 安裝python-dev
sudo apt-get install python-dev
2 安裝 libmysqlclient-dev
sudo apt-get install libmysqlclient-dev
3 安裝mysql-python
pip install mysql-python
最後進行驗證
(py3)pc@pc-virtual-machine:~/work/VENV/py3/bin$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
若是導入沒有問題就說明導入成功了。