15

好的,我最近从 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()
4

3 回答 3

39

使用虚拟环境

第一件事。确保你有 Mysql、Setuptools 和一些必需品:

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

现在安装 VirtualEnv:

sudo easy_install -U virtualenv 

创建虚拟环境。正如文档所说: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 和一些软件包的轻量级版本。) 在这里你会找到适用于 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.

然后你激活你的环境:

source activate mysql-env

最后安装mysql-python:

conda install MySQL-python

(我认为conda install mysql-python也有效)

就这样。

于 2012-10-21T00:21:59.253 回答
11

easy_install -U distribute解决了我的问题

于 2013-01-30T17:45:49.447 回答
4

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

sudo apt-get install python-dev libmysqlclient-dev

它也让我退缩了。其他响应者可能已经安装了它并且没有意识到它是必需的。

于 2014-02-02T02:36:28.127 回答