0
votes

Arrghh... I am trying to use mySQL with Python. I have installed all the libraries for using mySQL, but keep getting the: "ImportError: No module named mysql.connector" for "import mysql.connector", "mysql", etc..

Here is my config:

I have a RHEL server:

Red Hat Enterprise Linux Server release 6.7 (Santiago)

with Python 2.7.9

Python 2.7.9 (default, Dec 16 2014, 10:42:10) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2

with mySQL 5.1

mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1


I have all the appropriate libraries/modules installed, I think!

yum install MySQL-python

Package MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 already installed and latest version

yum install mysql-connector-python.noarch

Installed: mysql-connector-python.noarch 0:1.1.6-1.el6
yum install MySQL-python.x86_64 Package MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 already installed and latest version

yum install mysql-connector-python.noarch

Package mysql-connector-python-1.1.6-1.el6.noarch already installed and latest version


What am I doing wrong? HELP!?

2
Are you developing in a Virtual Enviroment docs.python-guide.org/en/latest/dev/virtualenvs If not you are running the risk of having an unorganized system, your environment may not be looking in the right place for your python modulesDanHabib
what you see when you do import MySQLdbVor
@Vor: I am not using MySQLdb. I am/want to use the mysql.connector from Oracle/mySQL. dev.mysql.com/downloads/file.php?id=457183jewettg
@DanHabib: Honestly .. I have never heard of a "Virtual Environment" in regards to Python. I just have a basic Linux environment, I have used YUM for all installs, and this has worked in the past.. "driving" my driver batty! :(jewettg
@Vor: >>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named MySQLdbjewettg

2 Answers

2
votes

You should use virtualenv in order to isolate the environment. That way your project libs won't clash with other projects libs. Also, you probably should install the Mysql driver/connector from pip.

Virtualenv is a CLI tool for managing your environment. It is really easy to use and helps a lot. What it does is to create all the folders Python needs on a custom location (usually your specific project folder) and it also sets all the shell variables so that Python can find the folders. Your system (/usr and so on) folders are not removed from the shell; rather, they just get a low priority. That is done by correctly setting your PATH variable, and virtualenv does that when you load a determined environment.

It is common practice to use an environment for each project you work on. That way, Python and pip won't install libs on the global folders. Instead, pip installs the libs on the current environment you are using. That avoids version conflicts and even Python version conflicts.

-1
votes

Nevermind!!!

Apparently I am installing Python and libraries in the right directories and such (I have always used YUM), but apparently there are other versions of Python installed.. need to clean that up.

Running: /usr/bin/python

All the modules worked!

Running: python (Linux finding python in the path somewhere)

Modules are not working!

Grrr...

Maybe I will checking out the Python "Virtual Environments"! Thanks for the suggestions!