Windows 上的 pyodbc 和 python 3.4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23299034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:41:59  来源:igfitidea点击:

pyodbc and python 3.4 on Windows

pythonpyodbc

提问by steegness

pyodbc is a very nice thing, but the Windows installers only work with their very specific python version. With the release of Python 3.4, the only available installers just stop once they don't see 3.3 in the registry (though 3.4 is certainly there).

pyodbc 是一个非常好的东西,但是 Windows 安装程序只能使用它们非常特定的 python 版本。随着 Python 3.4 的发布,唯一可用的安装程序在注册表中没有看到 3.3 时就会停止(尽管 3.4 肯定存在)。

Copying the .pyd and .egg-info files from a 3.3 installation into the 3.4 site-packages directory doesn't seem to do the trick. When importing pyodbc, an ImportError is thrown: ImportError: DLL load failed: %1 is not a valid Win32 application.

将 .pyd 和 .egg-info 文件从 3.3 安装复制到 3.4 site-packages 目录似乎不起作用。导入pyodbc时,抛出ImportError:ImportError: DLL load failed: %1 is not a valid Win32 application.

Is there a secret sauce that can be added to make the 3.3 file work correctly? Or do we just need to wait for a 3.4 installer version?

有没有什么秘诀可以添加,使3.3文件正常工作?还是我们只需要等待 3.4 安装程序版本?

采纳答案by John Y

The different versions of Python are (for the most part) not binary-compatible, and thus any compiled extensions (such as pyodbc) will only work for a specific version.

Python 的不同版本(在大多数情况下)不是二进制兼容的,因此任何已编译的扩展(例如 pyodbc)仅适用于特定版本。

Note that pure-Python packages (the ones that are completely written in Python, and have no non-Python dependencies) do not need to be compiled, and thus can be written to support multiple Python versions.

请注意,纯 Python 包(完全用 Python 编写且没有非 Python 依赖项的包)不需要编译,因此可以编写为支持多个 Python 版本。

Also note that it is technically possiblefor a compiled extension to be written such that it works for Python 3.2 as well as 3.3, 3.4, and the future 3.x's to come, but they have to limit themselves to the "stable ABI"as specified by PEP 384, and most extensions do not do this. As far as I know, pyodbc is not limited to the stable ABI and must be compiled separately for each Python version.

另请注意,在技术上可以编写已编译的扩展,使其适用于 Python 3.2 以及 3.3、3.4 和未来的 3.x,但它们必须将自己限制为“稳定的 ABI”,如由PEP 384指定,大多数扩展不这样做。据我所知,pyodbc 不限于稳定的 ABI,必须为每个 Python 版本单独编译。

That said, it is also possible to compile your own version of pyodbc from source, as long as you have the required tools and expertise. (But I'm guessing if you're asking this question, you don't. I don't either, otherwise I'd include some tips in this answer.)

也就是说,只要您拥有所需的工具和专业知识,也可以从源代码编译您自己的 pyodbc 版本。(但我猜如果你问这个问题,你不会。我也不,否则我会在这个答案中包含一些提示。)

As you have already commented, pypyodbcmay be your best bet, as it is a pure-Python package.

正如您已经评论过的,pypyodbc可能是您最好的选择,因为它是一个纯 Python 包。

Installing pypyodbc can be done via the commandline:

可以通过命令行安装 pypyodbc:

C:\Python34\Scripts>pip install pypyodbc

Using it as drop-in replacement of pyodbc can be done using:

可以使用以下方法将其用作 pyodbc 的替代品:

import pypyodbc as pyodbc

[The current version of pyodbcat the time of this edit is 3.0.10, and it does support Python 3.4. Of course, it's still useful to be aware of pypyodbcin case pyodbcfalls behind again when future versions of Python are released.]

[pyodbc编辑时的当前版本是3.0.10,它确实支持 Python 3.4。当然,当 Python 的未来版本发布时pypyodbc,如果pyodbc再次落后,请注意它仍然很有用。]

回答by xbb

Did you try to download from here? It has an unofficial build for 3.4. I did a quick test myself, looks like it's working fine for me.

你试过从这里下载吗?它有一个 3.4 的非官方版本。我自己做了一个快速测试,看起来它对我来说很好用。

回答by coder

I fixed this by installing pyodbc 3.0.10. The latest version of pyodbc didn't work on Windows with Python 3.4

我通过安装pyodbc 3.0.10. 最新版本的 pyodbc 无法在 Windows 上运行Python 3.4

However pyodbc 3.0.10 did work for me

但是 pyodbc 3.0.10 对我有用

Install command on command prompt : pip install pyodbc 3.0.10

在命令提示符下安装命令: pip install pyodbc 3.0.10