在Mac OS X El Capitan上为python构建mysql时出错

时间:2016-08-06 06:26:12

标签: mysql mysql-python osx-elcapitan python-3.5

我阅读了一些相关主题并尝试根据我的情况实现这些解决方案,但在此命令后仍然出现以下错误:

  

(project2_env)Efe-MacBook-Air:MySQL-python-1.2.4b4 efe $ python setup.py build

错误讯息是:

    Extracting in /var/folders/rv/vbf7xqh1601_xjkrn85w7hp00000gn/T/tmpptpsggg7
Traceback (most recent call last):
  File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 143, in use_setuptools
    raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    use_setuptools()
  File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 145, in use_setuptools
    return _do_download(version, download_base, to_dir, download_delay)
  File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 125, in _do_download
    _build_egg(egg, tarball, to_dir)
  File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 99, in _build_egg
    _extractall(tar)
  File "/Users/efe/virtualenvs/downloads/MySQL-python-1.2.4b4/distribute_setup.py", line 486, in _extractall
    self.chown(tarinfo, dirpath)
TypeError: chown() missing 1 required positional argument: 'numeric_owner'

编辑:我重新安装了homebrew,然后运行以下命令。这已成功安装。

brew install mysql

但是,我仍然无法在python中导入MySQLdb。

3 个答案:

答案 0 :(得分:1)

由于它比上一个答案更复杂,我不会告诉你关于python mysql的自定义安装。

发现这个,裘德的方式:

通过homebrew安装mysql,然后你可以通过pip安装mysql python。

xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
//don't know how to install mysql via homebrew, but it should be done here
pip install MySQL-python

https://stackoverflow.com/a/25356073/6660122

答案 1 :(得分:0)

取自Python2 doc,仍与3.5相关:

&#34;备用安装:用户方案

对于没有对全局site-packages目录具有写入权限或不想安装到其中的用户,此方案旨在成为最方便的解决方案。它通过一个简单的选项启用:&#34;

python setup.py install --user

可以帮助你的情况。

请注意,有不同的方案可供使用: - home, - --prefix和--exec-prefix,或--install-base和--install-platbase

答案 2 :(得分:0)

最后,我为自己和自己找到了解决方案。我放弃了直接使用MySQLdb库而不是那个,我安装了PyMySQL。顺便说一下我之前已经安装过MySQL Workbench,所以mysql服务器已经运行了。

以下是我一步一步做的事情。我希望有人能从中受益。

1)创建一个虚拟环境:

virtualenv virt1

2)激活您的虚拟环境:

source virt1/bin/activate

3)现在,您处于虚拟环境中。安装PyMySQL:

pip install PyMySQL

4)现在,使用简单的py可执行文件尝试MySQL连接是否正常:

#!/usr/bin/python

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='YourDBuserName',
                             password='YourDBpassword',
                             db='YourDBname',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `name` FROM `YourTableName` WHERE `id`=%s"
        cursor.execute(sql, (1,))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()

5)您应该会看到表格中的第一行出现在屏幕上。

注意:部分&#34;导入pymysql.cursors&#34;可能很棘手。首先,我写了#34;导入PyMySQL.cursors&#34;那不起作用!