Question

I'm trying to set up MySQL to work with python 2.7.8 and Sublime Text 3 on OS X 10.9, so I installed the MySQL python connector. Whenever I try to run "import mysql.connector", I get back "ImportError: No module named connector".

How should this be configured so that I can use the connector to access MySQL?

Details:

-Sublime is installed under Applications

-Sublime Text 3 uses python 3 as the default, so I had to set up a python 2.7 build system. Here is my code for this system:

  "cmd": ["/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/bin/python2.7", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python2"

-Python is installed at

 /usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/bin/python2.7

-MySQL executable is installed at

/usr/local/mysql-5.6.19-osx10.7-x86_64/bin

-The connector has a symlink at

/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector

and is installed fully at

/usr/local/lib/python2.7/site-packages/mysql/connector

Additionally, I added the following to .bash_profile to get the necessary directories added to the path.

PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# trying to get mysql access
PATH = "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:${PATH}"
export PATH

I'm sure I'm missing something simple, but this is my first time setting this much up, and I've spent way too many hours googling and trying to find out what I'm doing wrong. Anyone have an idea what I need to change to access the connector module? Thanks in advance.

Was it helpful?

Solution

Python doesn't use the shell $PATH variable to locate importable modules, it uses a list called sys.path in Python. If you run your interpreter and print out sys.path from it you should see that the site-packages directory is already being searched. You can request further directories be added to sys.path before program execution by adding them to the $PYTHONPATH shell environment variable.

It appears from the structure that mysql is a package with connector being a sub-package, and I am guessing that you need to make the symbolic link one directory higher (i.e. to the mysql directory) to pick up the necessary __init__.py that will cause it to be recognized as a package.

In general it's a good idea to learn to use pip to install modules and packages in so-called virtual environments. it allows you (for example) to have different programs use different versions of the same module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top