Python 在 OSX 10.9 (Mavericks) 上安装 pyodbc 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20074620/
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-18 19:26:21  来源:igfitidea点击:

Installing pyodbc fails on OSX 10.9 (Mavericks)

pythonmacospyodbc

提问by Moshe

When running pip install pyodbc, I get

跑步时pip install pyodbc,我得到

In file included from .../build/pyodbc/src/buffer.cpp:12:
    .../build/pyodbc/src/pyodbc.h:52:10: fatal error: 'sql.h' file not found
    #include <sql.h>
             ^
    1 error generated.
    error: command 'cc' failed with exit status 1

It seems that Mavericks has no sql.h under /usr/include

小牛好像在/usr/include下没有sql.h

Did anyone manage to install pyodbc? Is there a known workaround?

有没有人设法安装pyodbc?有已知的解决方法吗?

采纳答案by m_vitaly

As you noticed OSX Mavericks dropped sql headers that are required for PyODBC compilation. Following these steps allowed me to install PyODBC:

正如您所注意到的,OSX Mavericks 删除了 PyODBC 编译所需的 sql 标头。按照这些步骤,我可以安装 PyODBC:

  1. Make sure you have iODBC library installed (http://www.iodbc.org/)
  2. Download and extract iODBC sources
  3. Run pip install --no-install pyodbc
  4. cd [VIRTUAL_ENV]/build/pyodbc
  5. Run python setup.py build_ext --include-dirs=[LIBIODBC_SOURCES]/include/
  6. Run pip install --no-download pyodbc:

    Installing collected packages: pyodbc
      Running setup.py install for pyodbc
    
        warning: no files found matching 'tests/*'
    Successfully installed pyodbc
    Cleaning up...
    
  1. 确保您安装了 iODBC 库 ( http://www.iodbc.org/)
  2. 下载并提取 iODBC 源
  3. pip install --no-install pyodbc
  4. cd [VIRTUAL_ENV]/build/pyodbc
  5. python setup.py build_ext --include-dirs=[LIBIODBC_SOURCES]/include/
  6. 运行pip install --no-download pyodbc

    Installing collected packages: pyodbc
      Running setup.py install for pyodbc
    
        warning: no files found matching 'tests/*'
    Successfully installed pyodbc
    Cleaning up...
    

I could as well copy the files under [libiodbc_sources]/include/to my /usr/includeand just run pip install pyodbc, but I didn't want to add files manually to system folders.

我也可以将文件复制[libiodbc_sources]/include/到 my 下/usr/include并运行pip install pyodbc,但我不想手动将文件添加到系统文件夹中。

回答by MFB

I had no joy with @Vitaly's answer because there appears to be an issue building packages on Mavericks to do with the lack of support for hard-linking. I couldn't get the package to build.

我对@Vitaly 的回答并不满意,因为在 Mavericks 上构建软件包似乎存在问题,这与缺乏对硬链接的支持有关。我无法构建包。

So I opted for @Vitaly's second suggestion which was to copy the necessary files from the [libiodbc_sources]/include/directory to /usr/includeand the install worked find. Here is a list of the files you will need to copy:

所以我选择了@Vitaly 的第二个建议,即将必要的文件从[libiodbc_sources]/include/目录复制到/usr/include安装工作找到。以下是您需要复制的文件列表:

  • sql.h
  • sqltypes.h
  • iodbcunix.h
  • sqlext.h
  • sqlucode.h
  • sql.h
  • sqltypes.h
  • iodbcunix文件
  • sqlext.h文件
  • 代码文件

回答by Ryan Balfanz

You can use Homebrewto install unixodbc, then pyodbc via pip in the usual fashion.

您可以使用Homebrew安装 unixodbc,然后以通常的方式通过 pip 安装 pyodbc。

brew install unixodbc && pip install pyodbc

This works for me on Mavericks.

这对我在小牛队有用。

回答by asbjornenge

If you see errors like

如果您看到类似的错误

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

The problem is that with Mavericks Apple has removed gccfrom the command line developer tools; it is now clangjust symlinkedto gcc. The --mno-fused-maddflag is not supported by clang (same goes for lots of other flags).

问题在于,在 Mavericks 中,Apple从命令行开发人员工具中删除了gcc;现在clang只是符号链接gcc--mno-fused-maddclang 不支持该标志(许多其他标志也是如此)。

One solution might be to install gcc using homebrew or another method and symlink /usr/bin/gcc to a proper gcc.

一种解决方案可能是使用自制软件或其他方法安装 gcc,并将 /usr/bin/gcc 符号链接到适当的 gcc。

A simpler workaround that worked for me is suppressing these error by turning them into warnings:

对我有用的更简单的解决方法是通过将它们转换为警告来抑制这些错误:

export CFLAGS=-Qunused-arguments

After settings that I was able to pip install pyodbcwithout errors.

设置后,我能够pip install pyodbc没有错误。

PS! In future versions of clang this might not be possible. At least it works on:

附注!在clang的未来版本中,这可能是不可能的。至少它适用于:

$> gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

Refs:
https://bitbucket.org/cffi/cffi/issue/46/unused-mno-fused-madd-clang-warnings-on-oshttps://coderwall.com/p/lqpp8wclang: error: unsupported option '-static-libgcc' on Mac OSX Mavericks

参考:
https : //bitbucket.org/cffi/cffi/issue/46/unused-mno-fused-madd-clang-warnings-on-os https://coderwall.com/p/lqpp8w clang:错误:不支持的选项Mac OSX Mavericks 上的“-static-libgcc”

回答by Bouke

See my installation instructionswhich I've written after some futile attempts of the other answers provided:

请参阅我在对提供的其他答案进行了一些徒劳尝试后编写的安装说明

First, install the following libraries:

首先,安装以下库:

$ brew install unixodbc
$ brew install freetds --with-unixodbc

FreeTDS should already work now, without configuration:

FreeTDS 现在应该已经可以工作了,无需配置:

$ tsql -S [IP or hostname] -U [username] -P [password]
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> ^D

Onto unixODBC, we need to link to the driver, edit /usr/local/etc/odbcinst.ini:

在 unixODBC 上,我们需要链接到驱动程序,编辑/usr/local/etc/odbcinst.ini

[FreeTDS]
Description = TD Driver (MSSQL)
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
FileUsage = 1

The test command we're using requires configuring a DSN, so edit /usr/local/etc/odbc.ini:

我们使用的测试命令需要配置一个 DSN,所以编辑/usr/local/etc/odbc.ini

[MYDSN]
Driver = FreeTDS
Server = [IP address]
Port = 1433

The configuration for your DNS might vary, you might need the TDS_Versionor Servernamedirectives. The above worked for me for SQL Server 2008 R2. Now, run the test command:

您的 DNS 配置可能会有所不同,您可能需要TDS_VersionServername指令。以上对我来说适用于 SQL Server 2008 R2。现在,运行测试命令:

$ isql MYDSN [username] [password] -v
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> ^D

If the test succeeded, you can continue onto installing the Python library pyodbc. The current version of writing (3.0.7) doesn't link with unixODBC on OS X, so a change has to be made to setup.py. Download the source package and extract it somewhere. Find the following lines (146-147):

如果测试成功,您可以继续安装 Python 库pyodbc。当前版本 (3.0.7) 未与 OS X 上的 unixODBC 链接,因此必须对setup.py. 下载源包并将其解压缩到某处。找到以下几行 (146-147):

    elif sys.platform == 'darwin':
        # OS/X now ships with iODBC.

And change this line:

并更改此行:

        settings['libraries'].append('iodbc')

into:

进入:

        settings['libraries'].append('odbc')

Then run the following command to install:

然后运行以下命令进行安装:

> python install .

Now pyodbc should work:

现在 pyodbc 应该可以工作了:

import pyodbc
pyodbc.connect('DSN=MYDSN;UID=[username];PWD=[password]')

You don't need to have your DSN configured in odbc.ini, so clear that file. You probably want to select a database on connect, so change your connect line to read:

您不需要在 中配置 DSN odbc.ini,因此请清除该文件。您可能希望在连接时选择一个数据库,因此将连接行更改为:

pyodbc.connect('DRIVER=FreeTDS;SERVER=[IP address];PORT=1433;DATABASE=[database];UID=[username];PWD=[password]')

Note that you could also link to the library file of FreeTDS instead of using odbcinst.ini, like this:

请注意,您也可以链接到 FreeTDS 的库文件而不是使用odbcinst.ini,如下所示:

pyodbc.connect('DRIVER=/usr/local/lib/libtdsodbc.so;SERVER=[IP address];PORT=1433;DATABASE=[database];UID=[username];PWD=[password]')

回答by dennisobrien

After many dead ends, this worked for me:

经过许多死胡同后,这对我有用:

$ brew unlink unixodbc
$ brew install unixodbc  --universal
$ sudo pip install --upgrade --global-option=build_ext --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib" --allow-external pyodbc --allow-unverified pyodbc pyodbc

回答by Hongli Deng

I just went through the whole process on Mac OS X; connecting to pyodbc to MS SQL Server 2014. The whole process is as follows:

我刚刚在 Mac OS X 上完成了整个过程;连接pyodbc到MS SQL Server 2014。整个过程如下:

Connection pipeline:

连接管道:

pyodbc ----> iodbc ----> freetds ----> MS SQL Server 2014
  1. Build freetds (the SQL Server driver/connector):

    ./configure --prefix=/usr/local --with-tdsver=8.0
    
    make
    
    sudo make install
    
    // you should see /usr/local/lib/libtdsodbc.so was generated
    
    //test method 1:
    
    TDSVER=8.0 tsql -H hostname -p 1433 -U username -P XXX -D databasename
    
    //test method 2:
    
    //config /usr/local/etc/freetds.conf
    [mssqlserver]
        host = XXX
        port = 1433
        tds version = 8.0
    
    //run
    
    tsql -S mssqlserver -U username -P XXX -D databasename  
    
    //if you can run sql, good to go!
    
  2. Build iodbc (ODBC manager):

    //download from github, go to the folder
    
    cd mac
    
    ./configure 
    
    ./make
    
    sudo ./make install
    
    //config /usr/local/etc/odbc.ini
    [mssqlserver]
    Driver=/usr/local/lib/libtdsodbc.so
    TDS_Version=8.0
    Server=xxxx
    Port = 1433
    Trace = Yes
    Description=XXX
    
    //test
    
    which iodbctest
    
    iodbctest 
    
    DSN=masqlserver;UID=xxx;PWD=xxx
    
    //if you can run sql, good to go!
    
  3. Connect pyodbc (Python ODBC wrapper) to iodbc:

    pip install pyodbc 
    
    //in python,
    
    conn = pyodbc.connect("DSN=mssqlserver;UID=xxx;PWD=xxxx") 
    
  1. 构建 freetds(SQL Server 驱动程序/连接器):

    ./configure --prefix=/usr/local --with-tdsver=8.0
    
    make
    
    sudo make install
    
    // you should see /usr/local/lib/libtdsodbc.so was generated
    
    //test method 1:
    
    TDSVER=8.0 tsql -H hostname -p 1433 -U username -P XXX -D databasename
    
    //test method 2:
    
    //config /usr/local/etc/freetds.conf
    [mssqlserver]
        host = XXX
        port = 1433
        tds version = 8.0
    
    //run
    
    tsql -S mssqlserver -U username -P XXX -D databasename  
    
    //if you can run sql, good to go!
    
  2. 构建 iodbc(ODBC 管理器):

    //download from github, go to the folder
    
    cd mac
    
    ./configure 
    
    ./make
    
    sudo ./make install
    
    //config /usr/local/etc/odbc.ini
    [mssqlserver]
    Driver=/usr/local/lib/libtdsodbc.so
    TDS_Version=8.0
    Server=xxxx
    Port = 1433
    Trace = Yes
    Description=XXX
    
    //test
    
    which iodbctest
    
    iodbctest 
    
    DSN=masqlserver;UID=xxx;PWD=xxx
    
    //if you can run sql, good to go!
    
  3. 将 pyodbc(Python ODBC 包装器)连接到 iodbc:

    pip install pyodbc 
    
    //in python,
    
    conn = pyodbc.connect("DSN=mssqlserver;UID=xxx;PWD=xxxx") 
    

回答by Brian

I was successful with sudo port install py-pyodbc

我成功使用 sudo port install py-pyodbc

回答by Lothilius

This worked for me after trying just about everything else suggested.

在尝试了几乎所有其他建议之后,这对我有用。

brew install unixodbc
sudo pip install --upgrade --global-option=build_ext --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib" --allow-external pyodbc --allow-unverified pyodbc pyodbc

Running Mac OS 10.11.1,Homebrew 0.9.5, and pip 7.1.2

运行 Mac OS 10.11.1、Homebrew 0.9.5 和 pip 7.1.2

回答by WanderInCode

I met the the same problem today on ubuntu 14.04. I found some guy in below link said should install unixodbc-dev.

我今天在 ubuntu 14.04 上遇到了同样的问题。我在下面的链接中发现有人说应该安装 unixodbc-dev。

https://code.google.com/p/pyodbc/issues/detail?id=55

https://code.google.com/p/pyodbc/issues/detail?id=55

I did it, and then the pip install success.

我做到了,然后pip安装成功。

Hope this helpful.

希望这有帮助。