Windows python mysqldb install windows

mysqlclient

This project is a fork of MySQLdb1. This project adds Python 3 support and fixed many bugs.

Support

Do Not use Github Issue Tracker to ask help. OSS Maintainer is not free tech support

When your question looks relating to Python rather than MySQL:

Or when you have question about MySQL:

Install

Windows

Building mysqlclient on Windows is very hard. But there are some binary wheels you can install easily.

If binary wheels do not exist for your version of Python, it may be possible to build from source, but if this does not work, do not come asking for support. To build from source, download the MariaDB C Connector and install it. It must be installed in the default location (usually «C:\Program Files\MariaDB\MariaDB Connector C» or «C:\Program Files (x86)\MariaDB\MariaDB Connector C» for 32-bit). If you build the connector yourself or install it in a different location, set the environment variable MYSQLCLIENT_CONNECTOR before installing. Once you have the connector installed and an appropriate version of Visual Studio for your version of Python:

macOS (Homebrew)

Install MySQL and mysqlclient:

# Assume you are activating Python 3 venv $ brew install mysql pkg-config $ pip install mysqlclient 

If you don’t want to install MySQL server, you can use mysql-client instead:

# Assume you are activating Python 3 venv $ brew install mysql-client pkg-config $ export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig" $ pip install mysqlclient 

Linux

Note that this is a basic step. I can not support complete step for build for all environment. If you can see some error, you should fix it by yourself, or ask for support in some user forum. Don’t file a issue on the issue tracker.

You may need to install the Python 3 and MySQL development headers and libraries like so:

  • $ sudo apt-get install python3-dev default-libmysqlclient-dev build-essential # Debian / Ubuntu
  • % sudo yum install python3-devel mysql-devel # Red Hat / CentOS
Читайте также:  Введение числа с клавиатуры java

Then you can install mysqlclient via pip now:

Customize build (POSIX)

mysqlclient uses pkg-config —clfags —ldflags mysqlclient by default for finding compiler/linker flags.

You can use MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS environment variables to customize compiler/linker options.

$ export MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags` $ export MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs` $ pip install mysqlclient 

Documentation

Documentation is hosted on Read The Docs

Источник

Installing MySQLdb for Python 3 in Windows

My favorite Python connector for MySQL or MariaDB is MySQLdb, the problem with this connector is that it is complicated to install on Windows!

I am creating this article for those who want to install MySQLdb for Python 3 for Windows. Especially me, since each time I am doing a Python project that needs to connect to MariaDB or MySQL I always look on how to install MySQLdb.

If you are interested why I prefer MySQLdb compared to other MySQL connectors you may want to read the comparison of MySQL-connector and MySQLdb from Charles Nagy.

Problems with installing MySQLdb on Windows

You can actually install MySQLdb using pip. See pypi documentation here.

Unfortunately, the pypi documentation is already out of date with the latest release was on Jan 3, 2014.

You can still run the command above but it will look for Microsoft Visual C++ Build Tools, which if you install it in your Windows machine for the purpose of only using MySQLdb will become a program that occupies space but you will never ever use again.

So how do we install MySQLdb for Python on Windows? You can follow the steps below.

Installing MySQLdb on Windows

Download the appropriate .whl for your Python version.

Читайте также:  Debian run python script

I am currently using Python 3.6.8, so I downloaded mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl .

On windows command prompt, install the .whl file using pip. – pip install [.whl filename]

In my case the command is

pip install mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl

Note: The file on the command will be different if you use a different Python version or if there is an update on the version of the mysqlclient (MySQLdb).

Once pip says that you have Successfully installed mysqlclient you are good to go on using MySQLdb as your MySQL connector for Python.

Testing MySQLdb Installation

Below is the code I use if my MySQLdb connector has been properly installed in Windows.

import MySQLdb print("Connecting to database using MySQLdb") db_connection = MySQLdb.connect(host='DB_HOST', db='DB_NAME', user='DB_USERNAME', passwd='DB_PASSWORD') print("Succesfully Connected to database using MySQLdb!") db_connection.close()

You can change the following parameters to check if it can connect to your MySQL database or MariaDB database.

If all is well, then when you run the code above it would print Succesfully Connected to database using MySQLdb!

If MySQLdb is not yet installed then it will raise a ModuleNotFoundError like the screenshot below.

I hope this helped you install MySQLdb for Python on your Windows computer easier.

If there are any questions, errors or suggestions comment them down below so that I could check them out too.

Источник

Оцените статью