无法安装 MySQL-python

Posted

技术标签:

【中文标题】无法安装 MySQL-python【英文标题】:Unable to install MySQL-python 【发布时间】:2012-10-11 05:11:59 【问题描述】:

好的,我最近从 Ubuntu 10.04 升级到 12.04

只会面临很多问题。我是这方面的新手,所以您可能需要更详细或逐步告诉我

我有一个运行良好的 django 项目,但现在它给了我这个错误:

  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory

或在 python 中:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-i686/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.linux-i686/egg/_mysql.py", line 7, in <module>
  File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory

无论如何,我认为我的 MySQL-python 有问题,所以我尝试重新安装它。

sudo pip install mysql-python

给:

Downloading/unpacking mysql-python
  Running setup.py egg_info for package mysql-python
    The required version of distribute (>=0.6.28) is not available,
    and can't be installed while this script is running. Please
    install a more recent version first, using
    'easy_install -U distribute'.

    (Currently using distribute 0.6.24dev-r0 (/usr/lib/python2.7/dist-packages))
    Complete output from command python setup.py egg_info:
    The required version of distribute (>=0.6.28) is not available,

and can't be installed while this script is running. Please

install a more recent version first, using

'easy_install -U distribute'.



(Currently using distribute 0.6.24dev-r0 (/usr/lib/python2.7/dist-packages))

----------------------------------------
Command python setup.py egg_info failed with error code 2 in /tmp/pip-build/mysql-python
Storing complete log in /home/nabeel/.pip/pip.log

如果我这样做:

sudo easy_install -U distribute

它给出了这个错误:

Searching for distribute
Reading http://pypi.python.org/simple/distribute/
Reading http://packages.python.org/distribute
Best match: distribute 0.6.29
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.29.tar.gz#md5=1aad767bfca8bebaccafee25f85696cf
Processing distribute-0.6.29.tar.gz
Running distribute-0.6.29/setup.py -q bdist_egg --dist-dir /tmp/easy_install-K8ZuMX/distribute-0.6.29/egg-dist-tmp-Fj0gj_
Before install bootstrap.
Scanning installed packages
Setuptools installation detected at /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Egg installation
Patching...
Renaming /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg to /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg.OLD.1350776909.4
Patching complete.
Relaunching...
Traceback (most recent call last):
  File "/usr/local/bin/easy_install", line 9, in <module>
    load_entry_point('distribute==0.6.8', 'console_scripts', 'easy_install')()
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 1708, in main
    with_ei_usage(lambda:
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 1696, in with_ei_usage
    return f()
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 1712, in <lambda>
    distclass=DistributionWithoutHelpCommands, **kw
  File "/usr/local/lib/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 236, in run
    self.easy_install(spec, not self.no_deps)
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 471, in easy_install
    return self.install_item(spec, dist.location, tmpdir, deps)
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 501, in install_item
    dists = self.install_eggs(spec, download, tmpdir)
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 680, in install_eggs
    return self.build_and_install(setup_script, setup_base)
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 957, in build_and_install
    self.run_setup(setup_script, setup_base, args)
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/command/easy_install.py", line 946, in run_setup
    run_setup(setup_script, args)
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/sandbox.py", line 29, in run_setup
    lambda: execfile(
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/sandbox.py", line 70, in run
    return func()
  File "/usr/local/lib/python2.7/site-packages/distribute-0.6.8-py2.7.egg/setuptools/sandbox.py", line 31, in <lambda>
    '__file__':setup_script, '__name__':'__main__'
  File "setup.py", line 131, in <module>
  File "/tmp/easy_install-K8ZuMX/distribute-0.6.29/distribute_setup.py", line 368, in _before_install
  File "/tmp/easy_install-K8ZuMX/distribute-0.6.29/distribute_setup.py", line 441, in _fake_setuptools
  File "/tmp/easy_install-K8ZuMX/distribute-0.6.29/distribute_setup.py", line 450, in _relaunch
NameError: global name '_cmd1' is not defined

现在我又去了这个链接: http://pypi.python.org/pypi/setuptools/0.6c11 下载并安装 setuptools.. 我还从这里安装了最新的分发版 0.6.8: http://pypi.python.org/pypi/distribute/0.6.8

我也尝试过卸载以前的版本并重新安装,但它似乎不起作用。 我已经安装了 Python 2.7.3

谁能给我解释一下这是怎么回事...我可以如何安装 Mysqldb...

我也在 shell 上运行了这些命令:

sudo apt-get install mysql-server mysql-client python-mysqldb libmysqlclient-dev python-dev

但这并没有帮助......

更新我刚刚做了这个并且得到了这个错误:

nabeel@nabeel-pc:~$ pip uninstall setuptools
Uninstalling distribute:
  /usr/bin/easy_install
  /usr/bin/easy_install-2.7
  /usr/lib/python2.7/dist-packages/distribute-0.6.24dev_r0.egg-info
  /usr/lib/python2.7/dist-packages/easy_install.pyc
  /usr/lib/python2.7/dist-packages/pkg_resources.pyc
  /usr/lib/python2.7/dist-packages/setuptools
  /usr/lib/python2.7/dist-packages/site.pyc
  /usr/share/pyshared/easy_install.py
  /usr/share/pyshared/pkg_resources.py
  /usr/share/pyshared/site.py
Proceed (y/n)? y
  Successfully uninstalled distribute
nabeel@nabeel-pc:~$ sudo easy_install -U distribute
Searching for distribute
Reading http://pypi.python.org/simple/distribute/
Reading http://packages.python.org/distribute
Best match: distribute 0.6.29
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.29.tar.gz#md5=1aad767bfca8bebaccafee25f85696cf
Processing distribute-0.6.29.tar.gz
Running distribute-0.6.29/setup.py -q bdist_egg --dist-dir /tmp/easy_install-Xcer8d/distribute-0.6.29/egg-dist-tmp-K0E7gP
Before install bootstrap.
Scanning installed packages
Setuptools installation detected at /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Egg installation
Already patched.
After install bootstrap.
Creating /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info
Creating /usr/local/lib/python2.7/site-packages/setuptools.pth
Removing distribute 0.6.8 from easy-install.pth file
Adding distribute 0.6.29 to easy-install.pth file
Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin

Installed /usr/local/lib/python2.7/site-packages/distribute-0.6.29-py2.7.egg
Processing dependencies for distribute
Finished processing dependencies for distribute
nabeel@nabeel-pc:~$ sudo pip install mysql-python
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

更新 2: 我设法安装了 virtualenv 现在在环境中我也设法安装了 MySQLdb。 这是我在运行服务器时遇到的错误:

sudo python manage.py syncdb

我收到此错误:

  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory


(myenv)nabeel@nabeel-pc:~/workspace/gteam/metrics$ python
    Python 2.7.3 (default, Oct  6 2012, 21:57:36) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import MySQLdb
    >>> exit()

【问题讨论】:

你能把它安装在virtualenv中吗?这实际上更好,但也许你有一些特定的要求。 @robert-smith 我还没有尝试过 virtualenv.. 我从来没有用过所以我不确定.. 如果你想我可以试试然后让你知道响应 @RobertSmith 我已经为我的问题添加了一个新的更新...请您阅读一下 没问题,写答案:-) 【参考方案1】:

使用虚拟环境

首先要做的事情。确保你有 Mysql、Setuptools 和一些必需品:

sudo apt-get install mysql-server
sudo apt-get install python-setuptools python-dev build-essential

现在安装 VirtualEnv:

sudo easy_install -U virtualenv 

创建一个虚拟环境。正如documentation 所说:virtualenv is a tool to create isolated Python environments. 这意味着发生冲突的可能性非常小。这里的“myenv”是一个文件夹的名称,您可以在其中访问您的环境,所以在您想要的地方创建它。

virtualenv --distribute myenv

然后转到“myenv”目录并运行它来激活它:

source myenv/bin/activate

你需要安装 libmysqlclient-dev 才能让 MySQL-python 工作:

sudo apt-get install libmysqlclient-dev

现在您已准备好安装 MySQL-python:

pip install MySQL-python

一切都应该正常工作。

当你不想再在那个环境中工作时,在终端里写:

deactivate

更新

由于这个答案不断获得支持,让我向您展示如何使用 conda 做同样的事情,这已成为我现在最喜欢的环境。首先你必须安装 anaconda(我相信你也可以安装 miniconda,这是一个包含 conda 和一些软件包的轻量级版本。)Here 你会找到适用于 Linux、OS X 和 Windows 的安装程序。它是一个大文件,但包含许多准备安装的预编译包。假设你已经安装了 anaconda,你可以简单地运行:

conda create -n mysql-env python

mysql-env 是环境的名称(您可以随意调用它),python 是要安装的 python 的默认版本(当前为 2.7.10)。或者,使用conda create -n my-new-env python=3.4 安装其他版本的python。

然后你激活你的环境:

source activate mysql-env

最后安装mysql-python:

conda install MySQL-python

(我认为conda install mysql-python 也可以)

仅此而已。

【讨论】:

效果很好。我怎样才能让它在我的原始环境而不是虚拟环境中运行? 嗯,这就是使用虚拟环境的全部意义所在。你不想在你的主环境中运行所有这些,因为它会产生你知道的冲突,但是如果你真的想尝试修复你的主环境,试试这个来解决你的最后一个错误: curl python-distribute.org/distribute_setup.py | Python。但是,我建议您始终使用 virtualenv。另一个建议:从全新安装而不是升级安装较新版本的 Ubuntu。它会为您省去一些麻烦。 @RobertSmith... 很好。我已经设法安装 MySQLdb,但之后什么都没有……我在更新 2 中更新了我的问题……虚拟环境也无法运行我的项目 您是否使用新环境安装了 Django:pip install Django? 顺便说一句,我刚刚在 virtualenv 中测试了 Django,它完美地工作。我希望您在全新安装后不会遇到更多问题。你也可以从这个特定的问题开始一个新的问题,虽然有点难。【参考方案2】:

easy_install -U distribute 解决了我的问题

【讨论】:

是的,pip 也建议你这样做,我一开始也忽略了。 如果您使用的是virtualenv,请确保您的正确环境处于活动状态。【参考方案3】:

在某些 Ubuntu 安装中,您还需要考虑 libmysqlclient-dev:

sudo apt-get install python-dev libmysqlclient-dev

它也阻止了我。其他响应者可能已经安装了它,但没有意识到它是必需的。

【讨论】:

以上是关于无法安装 MySQL-python的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu 12.04下无法安装MySQL-python

Python3.4无法安装mysql-python

无法在 MacOS 10.12.4 上使用 pip 安装 mysql-python

安装mysql-python时“无法打开包含文件:'config-win.h':没有这样的文件或目录”

MySQL-python 无法连接到服务器

关于在Python3.6下安装MySQL-python模块的问题