Windows 下配置 Python 和 MariaDb

在 Windows 下,做程序开发的事情真可谓噩梦。下载安装完程序语言的编译器或解释器后大量的配置过程听起来就让人不寒而栗。这不,今天我就碰到了在 Windows 下如何使用 Python(ActivePython 3.4 版本) 连接和操作 MariaDb 的问题。

安装配置

大家都知道,MariaDb 来源于并兼容于 MySQL,所以,连接 MariaDb 只需要安装 MySQL 连接器就行了。可是,Python 编译器是没有自带 MySQL 连接器的,怎么办?我第一个想到的办法就是使用 pip 安装 Python 缺失的插件:

pip install mysql      # 可能不应该选择安装 MySQL,不过这样反而发现了问题

等待安装过程中程序居然报告出错,提示找不到 vcvarsall.bat

Python error: Unable to find vcvarsall.bat

这是什么鬼?!上网一搜,原来是没有安装编译器的缘故。得嘞,装呗!吭哧吭哧把 VisualStudio 2013 给装上了。根据网友的说法,要配置环境变量 VS90COMNTOOLS,方法有二:

  1. 在系统环境变量中配置 VS90COMNTOOLS 指向 VS120COMNTOOLS(至于是 VS120 还是 VS110 取决于安装的 VisualStudio 版本,2013 版的是 VS120),重启电脑。
  2. 打开 <python安装目录>\Lib\distutils\msvc9compiler.py,找到 toolskey = “VS%0.f0COMNTOOLS” % version,直接修改为 toolskey = “VS120COMNTOOLS”(实测有效)

再试:

pip install mysql
Error: “config-win.h”: No such file or directory

好吧……我放弃了。但是在网上搜寻的结果却很让人兴奋,因为已经有人做了 mysql-connector-python 的包,就在 MySQL 官网上!

下载地址:http://dev.mysql.com/downloads/connector/python/

注意下载相应的版本,Windows 64-bit, Python 3.4 的版本。且注意配置好 PATH 环境变量 Python 的路径,mysql-connector 官方提示的:

Note

Connector/Python requires python to be in the system’s PATH and installation fails if python cannot be located. On Unix and Unix-like systems, python is normally located in a directory included in the default PATH setting. On Windows, if you install Python, either enable Add python.exe to Path during the installation process, or manually add the directory containing python.exe yourself.

For more information about installation and configuration of Python on Windows, see Using Python on Windows in the Python documentation.

安装。

检查安装

安装完后,检查是否安装妥当:<python 安装目录>\Lib\site-packages\mysql,如果能找到这个目录,那么恭喜你!Python 可以正常连接 MySQL 啦!以下是官方的测试方法:

On Windows, the default Connector/Python installation location is C:\PythonX.Y\Lib\site-packages, where X.Y is the Python version you used to install the connector.
On Unix-like systems, the default Connector/Python installation location is /prefix/pythonX.Y/site-packages/, where prefix is the location where Python is installed and X.Y is the Python version. See How installation works in the Python manual.
The C Extension is installed as _mysql_connector.so in the site-packages directory, not in the mysql/connector directory.
Depending on your platform, the installation path might differ from the default. If you are not sure where Connector/Python is installed, do the following to determine its location. The output here shows installation locations as might be seen on OS X:

shell> python
>>> from distutils.sysconfig import get_python_lib

>>> print get_python_lib()            # Python v2.x
/Library/Python/2.7/site-packages

>>> print(get_python_lib())           # Python v3.x
/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages

To test that your Connector/Python installation is working and able to connect to MySQL Server, you can run a very simple program where you supply the login credentials and host information required for the connection. For an example, see Section 5.1, “Connecting to MySQL Using Connector/Python”.

用起来

使用方法我就不写了,参考这篇文章,很详细了: Python 程序如何连接 MariaDB 数据库

参考文章

  1. Python error: Unable to find vcvarsall.bat
  2. win7 64 安装 mysql-python ‘config-win.h’ 错误
  3. Connector/Python Installation
  4. Verifying Your Connector/Python Installation
  5. Python 程序如何连接 MariaDB 数据库
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值