Linux: MySQLdb on virtualenv with –no-site-packages

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:

this guide to install virtualenv

virtualenv myproject --no-site-packages

Activate virtualenv:

source myproject/bin/activate

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[0] cursor.close() conn.close()

Tested on CentOS 7, Python 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.

See also

The Hitchhiker’s Guide to Python

Simple MySQLdb connection tutorial

About the same topic

Python: MySQLdb on Windows virtualenv (w. figures)

https://web.archive.org/web/20170321000000*/http://docs.python-guide.org/en/latest/dev/virtualenvs/

https://web.archive.org/web/20170321000000*/https://en.wikipedia.org/wiki/SQLAlchemy

https://web.archive.org/web/20170321000000*/http://docs.python-guide.org/en/latest/dev/virtualenvs/

https://web.archive.org/web/20170321000000*/http://stackoverflow.com/a/372899/892951

https://web.archive.org/web/20170321000000*/https://chirale.org/2017/03/01/python-mysqldb-on-windows-virtualenv-w-figures/

Proxied content from gemini://chirale.org/2017-03-21_3579.gmi (external content)

Gemini request details:

Original URL
gemini://chirale.org/2017-03-21_3579.gmi
Status code
Success
Meta
text/gemini; charset=utf-8
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.