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:
virtualenv myproject --no-site-packages
This command will install a new virtualenv inside a new directory *myproject* created by t*he command itself*.
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
Simple MySQLdb connection tutorial (http://stackoverflow.com)
About the same topic
https://web.archive.org/web/20170321000000*/https://en.wikipedia.org/wiki/SQLAlchemy (https://web.archive.org)
https://web.archive.org/web/20170321000000*/http://docs.python-guide.org/en/latest/dev/virtualenvs/ (https://web.archive.org)
https://web.archive.org/web/20170321000000*/http://stackoverflow.com/a/372899/892951 (https://web.archive.org)
https://web.archive.org/web/20170321000000*/https://chirale.org/2017/03/01/python-mysqldb-on-windows-virtualenv-w-figures/ (https://web.archive.org)
Response: 20 (Success), text/gemini
| Original URL | gemini://chirale.org/2017-03-21_3579.gmi |
|---|---|
| Status Code | 20 (Success) |
| Content-Type | text/gemini; charset=utf-8 |