In the past it was difficult to get MySQL working on virtualenv without using system packages. Now you can have a real separated environment with simple steps:
- Follow this guide to install virtualenv using this command:
virtualenv myproject --no-site-packages
This command will install a new virtualenv inside a new directory myproject created by the command itself.
- Activate virtualenv:
- Upgrade setuptools
pip install pip --upgrade
- You can now install MySQLdb, inside the package MySQL-python:
pip install MySQL-python
- Now do a simple test trying to connect to an existing database:
python import MySQLdb db = MySQLdb.connect(host="localhost", # your host, usually localhost user="chirale", # your username passwd="ITSASECRET", # your password db="chiraledb") # name of the database cursor = conn.cursor() cursor.execute("SELECT VERSION()") row = cursor.fetchone() print "server version:", row cursor.close() conn.close()
Tested on CentOS 7, Python 2.7
Tip: If you are starting to create a database doing all the dirty work alone you’ve to give SQLAlchemy a try. You can use like an ORM or a lower level as you wish.
About the same topic