如何在virtualenv虚拟环境中安装mysql-python
接触过virtualenv后,想在这个虚拟环境中安装独立的开发环境。在安装MySQLdb时遇到错误
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 found
在国内的网上找了好长时间也没有找到办法,大多数是讲如何安装mysql-python的。先说一下解决过程吧
我是在ubuntu14上安装有virtualenv,在全局环境中安装了mysql-server,安装命令
sudo apt-get install mysql-server
后续过程都是在虚拟环境中进行的。我的是py3
开始找到要想使用mysql需要安装libmysqlclient-dev
安装过后在安装mysql-python还是出错
(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 install
running build
running build_py
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
>>>
如果导入没有问题就说明导入成功了。
如果觉得有用,想赞助一下请移步赞助页面:赞助一下