Efe Büyük
Efe Büyük

Reputation: 145

Getting Error while building mysql for python on Mac OS X El Capitan

I read some of the related topics and tried to implement those solutions to my situation but still I get the following error after this command :

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

The Error message is :

    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'

Edit: I reinstalled homebrew, then I run this following command. That is successfully installed.

brew install mysql

However, I cannot still import MySQLdb in python.

Upvotes: 1

Views: 497

Answers (3)

Efe B&#252;y&#252;k
Efe B&#252;y&#252;k

Reputation: 145

Finally I found a solution for myself and by myself. I gave up using directly MySQLdb library and instead of that, I installed PyMySQL. By the way I had already installed MySQL Workbench before, so mysql server was already running.

Here is what I do step by step. I hope someone can get benefit from it.

1) Create a virtual environment:

virtualenv virt1

2) Make your virtual environment activated:

source virt1/bin/activate

3) Now, you are in your virtual environment. Install PyMySQL:

pip install PyMySQL

4) Now, try whether your MySQL connection is OK or not with a simple py executable:

#!/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) You should see that your first row from the table appears on the screen.

Note: The part "import pymysql.cursors" could be tricky. First, I wrote as "import PyMySQL.cursors" and that didn't work!

Upvotes: 0

technico
technico

Reputation: 1172

As it is more complicated than the previous answer, i won't told you about the custom installation of python mysql.

Found this, the Jude's way :

Install mysql via homebrew, then you can install mysql python via pip.

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

Upvotes: 1

technico
technico

Reputation: 1172

Taken from the Python2 doc, still relevant in 3.5 :

"Alternate installation: the user scheme

This scheme is designed to be the most convenient solution for users that don’t have write permission to the global site-packages directory or don’t want to install into it. It is enabled with a simple option: "

python setup.py install --user

Could help in your case.

Notice there are different scheme to use : --home, or --prefix and --exec-prefix, or --install-base and --install-platbase

Upvotes: 0

Related Questions