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:
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 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.
See also
The Hitchhiker’s Guide to Python
Simple MySQLdb connection tutorial
About the same topic
Collecting MySQL-python
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
sh: mysql_config: command not found
Traceback (most recent call last):
File “”, line 1, in
File “/tmp/pip-install-K02gcJ/MySQL-python/setup.py”, line 17, in
metadata, options = get_config()
File “setup_posix.py”, line 43, in get_config
libs = mysql_config(“libs_r”)
File “setup_posix.py”, line 25, in mysql_config
raise EnvironmentError(“%s not found” % (mysql_config.path,))
EnvironmentError: mysql_config not found
—————————————-
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-install-K02gcJ/MySQL-python/