0

I'm trying to deploy a flask app in a centOS 7.

I installed the connector with: sudo yum install mysql-connector-python

But when I run: python init.py, I got this error:

from mysql import connector
ImportError: No module named mysql

Note: my project is inside a virtualenv.

Inside my virtualenv:

pip install MySQL-python

Error:Requirement already satisfied (use --upgrade to upgrade): MySQL-python in /root/ams/env/lib/python2.7/site-packages

yum install MySQL-python

Error:Package MySQL-python-1.2.3-11.el7.x86_64 already installed and latest version

urb
  • 924
  • 1
  • 13
  • 28
  • it looks like something is not right when you install the `mysql-connector-python` package. Try follow my edited answer below to find out whether the `mysql` python package is in the `sys.path`. – charlee Oct 13 '15 at 16:21
  • when I type sudo python app.py, the adapter is imported, but without sudo python can't find the package. Its virtualenv problem? – urb Oct 13 '15 at 16:45

2 Answers2

-1

Not sure about the reason but probably the mysql connector you installed is not imported into your virtualenv.

Do the following to see the lib paths:

(virtualenv) $ python
>>> import sys
>>> sys.path

And search for each path for mysql directory.

UPDATE

If you decide to use MySQL-python instead of mysql-connector-python you can follow its document here.

My recommendation is to use some db layer like SQLAlchemy but if you want to connect to mysql manually here's a good tutorial.

How do I connect to a MySQL Database in Python?

Community
  • 1
  • 1
charlee
  • 1,309
  • 1
  • 11
  • 22
  • Doesn't exist any mysql directory. But when I run pip install MySQL-python, appears: MySQL-python in /root/ams/env/lib/python2.7/site-packages. So its installed, how I use it? – urb Oct 13 '15 at 16:23
  • Sorry that I didn't explain clearly -- the `sys.path` will output a list of paths and you have to check each path manually (in the terminal use `cd` and `ls`) for the `mysql` dir. The most likely path is 1) your virtualenv/lib/python27/site-packages 2) /usr/lib/python27/site-packages. First look for `mysql` in these two places – charlee Oct 13 '15 at 16:26
  • I just found mysql in /usr/lib/python27/site-packages. In virtualenv I found a different mysql distribution - MySQLdb – urb Oct 13 '15 at 16:30
-1

you can run this :

sudo yum install mysql-connector-python-rf
Naveen Agarwal
  • 930
  • 7
  • 7