I had problems installing the 64-bit version of MySQLdb on Windows via Pip (problem compiling sources) [32bit version installed ok]. Managed to install the compiled MySQLdb from the .whl file available from http://www.lfd.uci.edu/~gohlke/pythonlibs/
Follow-up: if you have a 64bit version of Python installed, then you want to install the 64-bit AMD version of MySQLdb from the link above [i.e. even if you have a Intel processor]. If you instead try and install the 32-bit version, I think you get the unsupported wheel error in comments below.
import pymysql asMySQLdb# Open database connection
db =MySQLdb.connect("localhost","root","root","test")# prepare a cursor object using cursor() method
cursor = db.cursor()# execute SQL query using execute() method.
cursor.execute("show tables")# Fetch a single row using fetchone() method.
data = cursor.fetchall()print(data)# disconnect from server
db.close()
Go to pycharm then go to default setting –> pip (double click) — pymsqldb..– > install –after installing use in a program like this
import pymysql as MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","root","root","test" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("show tables")
# Fetch a single row using fetchone() method.
data = cursor.fetchall()
print (data)
# disconnect from server
db.close()
I had the same problem too.Follow these steps if you are on Windows.
Go to:
1.My Computer
2.System Properties
3.Advance System Settings
4. Under the “Advanced” tab click the button that says “Environment Variables”
5. Then under System Variables you have to add / change the following variables: PYTHONPATH and Path. Here is a paste of what my variables look like:
python path:
The above answer is great, but there may be some problems when we using pip to install MySQL-python in Windows
for example,It needs some files that are associated with Visual Stdio .One solution is installing VS2008 or 2010……Obviously,it cost too much.
Another way is the answer of @bob90937 . I am here to do something to add.
with http://www.lfd.uci.edu/~gohlke/pythonlibs, u can download many Windows binaries of many scientific open-source extension packages for the official CPython distribution of the Python programming language.
Back to topic,we can choose the MySQL-python(py2) or Mysqlclient(py3) and use pip install to install. it gives us Great convenience!
root@2fb0da64a933:/home/test_scrapy# apt-get install python-mysqldbReadingpackage lists...DoneBuilding dependency tree
Reading state information...DoneThe following additional packages will be installed:
libmariadbclient18 mysql-common
Suggested packages:default-mysql-server |virtual-mysql-server python-egenix-mxdatetime python-mysqldb-dbg
The following NEW packages will be installed:
libmariadbclient18 mysql-common python-mysqldb
0 upgraded,3 newly installed,0 to removeand29not upgraded.Need to get843 kB of archives.Afterthis operation,4611 kB of additional disk space will be used.Do you want to continue?[Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 mysql-common all 5.8+1.0.2 [5608 B]Get:2 http://deb.debian.org/debian stretch/main amd64 libmariadbclient18 amd64 10.1.38-0+deb9u1 [785 kB]Get:3 http://deb.debian.org/debian stretch/main amd64 python-mysqldb amd64 1.3.7-1.1 [52.1 kB] Fetched843 kB in23s(35.8 kB/s)
debconf: delaying package configuration, since apt-utils isnot installed
Selecting previously unselected package mysql-common.(Reading database ...13223 files and directories currently installed.)Preparing to unpack .../mysql-common_5.8+1.0.2_all.deb ...Unpacking mysql-common (5.8+1.0.2)...Selecting previously unselected package libmariadbclient18:amd64.Preparing to unpack .../libmariadbclient18_10.1.38-0+deb9u1_amd64.deb ...Unpacking libmariadbclient18:amd64 (10.1.38-0+deb9u1)...Selecting previously unselected package python-mysqldb.Preparing to unpack .../python-mysqldb_1.3.7-1.1_amd64.deb ...Unpacking python-mysqldb (1.3.7-1.1)...Setting up mysql-common (5.8+1.0.2)...
update-alternatives:using/etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf)inauto mode
Setting up libmariadbclient18:amd64 (10.1.38-0+deb9u1)...Processing triggers for libc-bin (2.24-11+deb9u3)...Setting up python-mysqldb (1.3.7-1.1)...
root@2fb0da64a933:/home/test_scrapy# python Python2.7.13(default,Nov242017,17:33:09)[GCC 6.3.020170516] on linux2
Type"help","copyright","credits"or"license"for more information.>>>importMySQLdb>>>
actually, follow @Nick T’s answer doesn’t work for me, i try apt-get install python-mysqldb work for me
root@2fb0da64a933:/home/test_scrapy# apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libmariadbclient18 mysql-common
Suggested packages:
default-mysql-server | virtual-mysql-server python-egenix-mxdatetime python-mysqldb-dbg
The following NEW packages will be installed:
libmariadbclient18 mysql-common python-mysqldb
0 upgraded, 3 newly installed, 0 to remove and 29 not upgraded.
Need to get 843 kB of archives.
After this operation, 4611 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 mysql-common all 5.8+1.0.2 [5608 B]
Get:2 http://deb.debian.org/debian stretch/main amd64 libmariadbclient18 amd64 10.1.38-0+deb9u1 [785 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 python-mysqldb amd64 1.3.7-1.1 [52.1 kB]
Fetched 843 kB in 23s (35.8 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package mysql-common.
(Reading database ... 13223 files and directories currently installed.)
Preparing to unpack .../mysql-common_5.8+1.0.2_all.deb ...
Unpacking mysql-common (5.8+1.0.2) ...
Selecting previously unselected package libmariadbclient18:amd64.
Preparing to unpack .../libmariadbclient18_10.1.38-0+deb9u1_amd64.deb ...
Unpacking libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Selecting previously unselected package python-mysqldb.
Preparing to unpack .../python-mysqldb_1.3.7-1.1_amd64.deb ...
Unpacking python-mysqldb (1.3.7-1.1) ...
Setting up mysql-common (5.8+1.0.2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up python-mysqldb (1.3.7-1.1) ...
root@2fb0da64a933:/home/test_scrapy# python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>