ImportError: No module named 'mysql'

I was successfully using your tutorial application then I decided to get bold and installed your virturalenv. All worked well after except for the SQLAlchemy/MySQL logic which then reported 'No module named mysql' upon accessing the contacts db. Can someone explain how fix this?

From python command line I entered:

from flask_app import Comment 

The results ended with

Traceback (most recent call last): File
line 988, in __call__
    return self.registry[key] KeyError: 140307501483840 During handling of the above exception, another exception occurred: Traceback
(most recent call last):   File "<stdin>", line 1, in <module>   File
"/home/bobgott/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/flask_sqlalchemy/", line 500, in __get__
    return type.query_class(mapper,   File "/home/bobgott/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/sqlalchemy/orm/",
line 78, in __call__
    return self.registry()   File "/home/bobgott/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/sqlalchemy/util/",
line  ......
    return strategy.create(*args, **kwargs)   File "/home/bobgott/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/sqlalchemy/engine/",
line 75, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)   File "/home/bobgott/.virtualenvs/my-virtualenv/lib/python3.4/site-packages/sqlalchemy/dialects/mysql/",
line 107, in dbapi
    from mysql import connector ImportError: No module named 'mysql'

[edit by admin: formatting]

That's dangerously brave ;-)

Once you've installed a virtualenv, you need to install all of the modules that your Python application depends on into it. So, to fix this specific problem, you'll need to install the MySQL library. To do that:

  • Start a bash console in the virtualenv using the link on the "Web" tab
  • Run pip install mysqlclient (assuming you're using Python 3.x)

I see that you've already installed SQLAlchemy; you may also need to install Flask if you get errors about that too, which is (of course) just a case of running pip install flask in the same virtualenv bash console.

I installed mysqlclient like you said .. same error. Then I removed/installed my virt environment again based on your web page Rebuilding a Virtualenv. Problem still exists. I'm sure I'm missing something.

I created a file with my installed packages (pip freeze) and full latest error message. error log

Hate to waste your time .. next step is to blow everything away and start from the very beginning .. but not wanting to go down that rabbit hole yet.

<i>(<b>side note:</b> I accidently deleted .pythonstartup .. but just put an empty file in its place. Is that a problem?)</i>

Ah, I think I see the problem. Your code appears to be set up to use SQAlchemy with MySQLConnector instead of the normal MySQL client library. Try installing mysql-connector-python instead of mysqlclient.

Wow, that that fixed the problem.

So .. (correct me if I'm wrong) ... my program worked when it was NOT in the virt environment as "mysql-connector-python" was installed in your base system. However, going to the virt-environment needed that installation. Is that right?

Being somewhat new to python .. it would have taken me a long time to narrow down on that.

Thank you!

That's exactly it. We have pretty much everything but the kitchen sink installed in our base system, so that it's easy to get started. Using a virtualenv is still a good idea, though -- it means that if we ever have to change the system-wide versions, you're insulated from that.

Just wanted to shout back a hearty thank you for helping me solve my problem so quickly.

Glad to help!

Thank you, this is another beginner who had the exact same problem, and installing "mysql-connector-python" fixed it.

I'd love to know, how did you know this was the fix?

From the module name "mysql", my first try was "pip install mysql", I didn't see the name "mysql-connector-python" in the error message and didn't know where to look next.

Can you point the spot/method to dig so I can improve my own troubleshooting skills for the future?

An excellent question! Unfortunately my answer's not so excellent, or at least not super-helpful -- it's simply because we see a lot of questions here on the forums, and over email, so wind up with a lot of random knowledge about which things people need to install for what.