Forums

MySQL - connecting to database

Hello. It's me again!

I launched MySQL database on my website but now i have problem. I would like to connect to it via sql-alchemy on my computer (virtualenv) using my simple project. Some ideas?

Code:

from sqlalchemy import create_engine
import MySQLdb

engine = create_engine('mysql+mysqlconnector://myUsername:myPassword@librarysystem.mysql.pythonanywhere- services.com/databaseName')
connection = engine.connect()

Error:

File "/home/username/Desktop/myProject/flask/local/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqlconnector.py", line 107, in dbapi
    from mysql import connector
ImportError: No module named mysql

I was searching but nothing helped. Have a nice day!

Hi there,

That path looks strange. Why is there a site-packages directory inside your myproject/flask folder? That looks like some kind of non-standard virtualenv setup, or some sort of pythonpath hack?

We tend to recommend using virtualenvwrapper to get custom packages for your web app. More info here: https://help.pythonanywhere.com/pages/Virtualenvs

  1. I created Python environment by:
 virtualenv --system-site-packages flask

...in my project directory.

2.

 cd flask/bin
 . activate

I installed required flask components by

pip install flask
pip install flask-sqlalchemy etc

3. In my flask enviroment i have mentioned error.

Site-packages can be there because of --system-site-packages. I'll try install it without parameter

Hm, yes, my guess would be that one of the system packages is shadowing the "mysql" that sqlalchemy is trying to import...