Python 2.7 - 在Windows 8上安装MySQL-Python时出错

1 投票
1 回答
1933 浏览
提问于 2025-04-18 00:49

我这两天一直在为这个问题苦恼,完全没有进展。

我想在我的Windows 8电脑上安装Python的MySQL连接器(我真不喜欢Windows 8)。

我使用的是Python 2.7。最开始我遇到了一个错误:

找不到vcvarsall.bat。

我查了一下,发现需要在我的电脑上安装VS 2008(VC++编译器),于是我安装了它的精简版。我还在注册表中添加了ProductDir的值,并指向vcvarsall.bat的路径,具体位置是:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC.

我还添加了VS90COMNTOOLS环境变量,并指向vcvarsall.bat的路径。不过,当我运行以下命令时,还是出现了错误:

 pip install MySQL-python

如果你能提供任何帮助,我会非常感激……谢谢……

错误信息在pip.log的以下片段中列出:

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "c:\users\t\appdata\local\temp\pip_build_T\MySQL-python\setup.py", line 21, in <module>

    setuptools.setup(**metadata)

  File "C:\Python27\lib\distutils\core.py", line 152, in setup

    dist.run_commands()

  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands

    self.run_command(cmd)

  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command

    cmd_obj.run()

  File "build\bdist.win-amd64\egg\setuptools\command\install.py", line 53, in run

  File "C:\Python27\lib\distutils\command\install.py", line 563, in run

    self.run_command('build')

  File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command

    self.distribution.run_command(command)

  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command

    cmd_obj.run()

  File "C:\Python27\lib\distutils\command\build.py", line 127, in run

    self.run_command(cmd_name)

  File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command

    self.distribution.run_command(command)

  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command

    cmd_obj.run()

  File "build\bdist.win-amd64\egg\setuptools\command\build_ext.py", line 52, in run

  File "C:\Python27\lib\distutils\command\build_ext.py", line 337, in run

    self.build_extensions()

  File "C:\Python27\lib\distutils\command\build_ext.py", line 446, in build_extensions

    self.build_extension(ext)

  File "build\bdist.win-amd64\egg\setuptools\command\build_ext.py", line 186, in build_extension

  File "C:\Python27\lib\distutils\command\build_ext.py", line 496, in build_extension

    depends=ext.depends)

  File "C:\Python27\lib\distutils\msvc9compiler.py", line 473, in compile

    self.initialize()

  File "C:\Python27\lib\distutils\msvc9compiler.py", line 383, in initialize

    vc_env = query_vcvarsall(VERSION, plat_spec)

  File "C:\Python27\lib\distutils\msvc9compiler.py", line 299, in query_vcvarsall

    raise ValueError(str(list(result.keys())))

ValueError: [u'path']

----------------------------------------
Cleaning up...
  Removing temporary dir c:\users\t\appdata\local\temp\pip_build_T...
Command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\t\\appdata\\local\\temp\\pip_build_T\\MySQL-python\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\t\appdata\local\temp\pip-d5hmns-record\install-record.txt --single-version-externally-managed --compile failed with error code 1 in c:\users\t\appdata\local\temp\pip_build_T\MySQL-python
Exception information:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pip-1.5.4-py2.7.egg\pip\basecommand.py", line 122, in main
    status = self.run(options, args)
  File "C:\Python27\lib\site-packages\pip-1.5.4-py2.7.egg\pip\commands\install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "C:\Python27\lib\site-packages\pip-1.5.4-py2.7.egg\pip\req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "C:\Python27\lib\site-packages\pip-1.5.4-py2.7.egg\pip\req.py", line 706, in install
    cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False)
  File "C:\Python27\lib\site-packages\pip-1.5.4-py2.7.egg\pip\util.py", line 697, in call_subprocess
    % (command_desc, proc.returncode, cwd))
InstallationError: Command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\t\\appdata\\local\\temp\\pip_build_T\\MySQL-python\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\t\appdata\local\temp\pip-d5hmns-record\install-record.txt --single-version-externally-managed --compile failed with error code 1 in c:\users\t\appdata\local\temp\pip_build_T\MySQL-python

1 个回答

1

出现 pip install 失败的情况,说明可能有一些依赖项需要编译的文件。你可以试试从 https://pypi.python.org/pypi/MySQL-python 下载 .exe 安装程序,但这个方法不适合在虚拟环境中使用,而且在64位的Python上会出问题(它会提示找不到注册表中的Python 2.7,尽管实际上已经安装了)。

幸运的是,Christoph Gohlke维护了一些非官方的Windows二进制文件,这些文件可以通过pip安装。你可以在 http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 找到这些文件。

现在,你可以在你的主Python环境或者虚拟环境中运行 pip install C:/PATH/TO/MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl 来安装。

理想情况下,这些轮子文件会在MySQL-python的PyPI页面上提供,到时候只需要简单地运行 pip install 就可以了。

(这个回答是基于 Carsten 的有用评论。)

撰写回答