Skip to content

Commit e9dc5d4

Browse files
committed
Better support for building on Windows
mysqlclient can be built against either the MySQL Connector/C or the MariaDB Connector/C. Previously, which library was used was hardcoded in setup_windows.py while the path to the library was in site.cfg. Now both settings can be specified in site.cfg, as environment variables, or auto-detected. If a client isn't specified but a connector path is, that path will be searched for either mariadbclient.lib or mysqlclient.lib to set the client type. If a client is specified but a connector path isn't, the default path used by the pre-built .msi installers will be assumed. This will probably be "C:\Program Files\MariaDB\MariaDB Connector C" for mariadbclient and "C:\Program Files\MySQL\MySQL Connector C 6.1" for mysqlclient ("C:\Program Files (x86)" will be used on 32-bit builds). If neither client nor connector are specified, both client types will be checked to see if the appropriate .lib file exists in the default connector directory. These changes will allow users to install from source via pip (if binary wheels aren't available) without having to clone this repo to edit site.cfg. They can either install one of the connectors in the default location or install it somewhere else and set the path in the `MYSQLCLIENT_CONNECTOR` environment variable before installing. One other slight change was to add additional install and library directories for mariadbclient. This is because the pre-built connector binaries install the needed files in `include` and `lib` whereas building from source (like this repo's workflow does) leaves them in `include/mariadb` and `lib/mariadb`.
1 parent 24aaa72 commit e9dc5d4

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

Diff for: README.md

+16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ Or when you have question about MySQL:
2828
Building mysqlclient on Windows is very hard.
2929
But there are some binary wheels you can install easily.
3030

31+
If binary wheels do not exist for your version of Python, it may be possible to
32+
build from source, but if this does not work, **do not come asking for support.**
33+
To build from source, download the
34+
[MariaDB C Connector](https://mariadb.com/downloads/#connectors) and install
35+
it. It must be installed in the default location
36+
(usually "C:\Program Files\MariaDB\MariaDB Connector C" or
37+
"C:\Program Files (x86)\MariaDB\MariaDB Connector C" for 32-bit). If you
38+
build the connector yourself or install it in a different location, set the
39+
environment variable `MYSQLCLIENT_CONNECTOR` before installing. Once you have
40+
the connector installed and an appropriate version of Visual Studio for your
41+
version of Python:
42+
43+
```
44+
$ pip install mysqlclient
45+
```
46+
3147
### macOS (Homebrew)
3248

3349
Install MySQL and mysqlclient:

Diff for: setup_windows.py

+49-7
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,60 @@
33
from distutils.msvccompiler import get_build_version
44

55

6+
def get_default_connector(client):
7+
if client == "mariadbclient":
8+
return os.path.join(os.environ["ProgramFiles"], "MariaDB",
9+
"MariaDB Connector C")
10+
elif client == "mysqlclient":
11+
return os.path.join(os.environ["ProgramFiles"], "MySQL",
12+
"MySQL Connector C 6.1")
13+
else:
14+
raise ValueError("Unknown client library")
15+
16+
17+
def find_library(client, connector=None):
18+
if not connector:
19+
connector = get_default_connector(client)
20+
paths = []
21+
if client == "mariadbclient":
22+
paths.append(os.path.join(connector, "lib", "mariadb", client + ".lib"))
23+
paths.append(os.path.join(connector, "lib", client + ".lib"))
24+
elif client == "mysqlclient":
25+
vcversion = int(get_build_version())
26+
paths.append(os.path.join(connector, "lib", "vs%d" % vcversion))
27+
else:
28+
raise ValueError("Unknown client library")
29+
for path in paths:
30+
if os.path.exists(path):
31+
return path
32+
return None
33+
34+
635
def get_config():
736
from setup_common import get_metadata_and_options, create_release_file
837

938
metadata, options = get_metadata_and_options()
1039

11-
connector = options["connector"]
40+
client = os.environ.get("MYSQLCLIENT_CLIENT", options.get("client"))
41+
connector = os.environ.get("MYSQLCLIENT_CONNECTOR", options.get("connector"))
1242

13-
extra_objects = []
43+
if not client:
44+
for client in ("mariadbclient", "mysqlclient"):
45+
if find_library(client, connector):
46+
break
47+
else:
48+
raise RuntimeError("Couldn't find MySQL or MariaDB Connector")
1449

15-
# client = "mysqlclient"
16-
client = "mariadbclient"
50+
if not connector:
51+
connector = get_default_connector(client)
52+
53+
54+
extra_objects = []
1755

1856
vcversion = int(get_build_version())
1957
if client == "mariadbclient":
20-
library_dirs = [os.path.join(connector, "lib", "mariadb")]
58+
library_dirs = [os.path.join(connector, "lib", "mariadb"),
59+
os.path.join(connector, "lib")]
2160
libraries = [
2261
"kernel32",
2362
"advapi32",
@@ -29,14 +68,17 @@ def get_config():
2968
"bcrypt",
3069
client,
3170
]
32-
include_dirs = [os.path.join(connector, "include", "mariadb")]
33-
else:
71+
include_dirs = [os.path.join(connector, "include", "mariadb"),
72+
os.path.join(connector, "include")]
73+
elif client == "mysqlclient":
3474
library_dirs = [
3575
os.path.join(connector, r"lib\vs%d" % vcversion),
3676
os.path.join(connector, "lib"),
3777
]
3878
libraries = ["kernel32", "advapi32", "wsock32", client]
3979
include_dirs = [os.path.join(connector, r"include")]
80+
else:
81+
raise ValueError("Unknown client library")
4082

4183
extra_link_args = ["/MANIFEST"]
4284

Diff for: site.cfg

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ static = False
99

1010
# http://stackoverflow.com/questions/1972259/mysql-python-install-problem-using-virtualenv-windows-pip
1111
# Windows connector libs for MySQL. You need a 32-bit connector for your 32-bit Python build.
12-
connector = C:\Program Files (x86)\MySQL\MySQL Connector C 6.1
12+
client =
13+
connector =

0 commit comments

Comments
 (0)