【问题标题】:No module named MySQLdb even if MySQL-python installed?即使安装了 MySQL-python,也没有名为 MySQLdb 的模块?
【发布时间】:2014-07-08 23:12:04
【问题描述】:

我有一个 CentOS 盒子。

(venv)[root@localhost]# yum install MySQL-python
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.maine.edu
 * epel: ftp.osuosl.org
 * extras: mirror.us.leaseweb.net
 * remi: rpms.famillecollet.com
 * updates: mirrors.centarra.com
Setting up Install Process
Package MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 already installed and latest version
Nothing to do

...

(venv)[root@localhost]# python
Python 2.7.6 (default, May 20 2014, 20:23:08)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
>>>

我该怎么办?

当我尝试pip install mysql-python 时,我得到了这个:

Traceback (most recent call last):
  File "/home/user1/project/venv/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/home/user1/project/venv/lib/python2.7/site-packages/pip/__init__.py", line 185, in main
    return command.main(cmd_args)
  File "/home/user1/project/venv/lib/python2.7/site-packages/pip/basecommand.py", line 161, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal not in range(128)

【问题讨论】:

标签: python mysql unix centos mysql-python


【解决方案1】:

您需要强制pip 使用默认本地化设置。而不是:

pip install mysql-python

做:

LC_ALL=C pip install mysql-python

如果遇到error: Python.h: No such file or directory,则需要安装一个额外的库:

yum install python-devel

【讨论】:

  • 这成功了!我也必须yum install mysql-devel,以及上述步骤。
【解决方案2】:

有时我们需要在外部添加/安装 git repo, 这还没有发布,但你可以从下面的命令中添加

pip install git+https://github.com/PyMySQL/mysqlclient-python.git

还有

pip install mysql-python

yum install python-devel

【讨论】:

    【解决方案3】:

    你可以使用 MySQL-python

    对于centos

    yum 安装 MySQL-python

    Python 2.7.13 (default, Sep 26 2018, 18:42:22) 
    [GCC 6.3.0 20170516] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    
        >> import MySQLdb
        >> 
    

    【讨论】:

      猜你喜欢
      • 2015-08-19
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-29
      • 2019-02-02
      • 1970-01-01
      • 2018-08-18
      相关资源
      最近更新 更多