Python 无法打开 lib 'ODBC Driver 13 for SQL Server'?符号链接问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44527452/
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
Can't open lib 'ODBC Driver 13 for SQL Server'? Sym linking issue?
提问by emehex
When I try to connect to a sql server database with pyodbc (on mac):
当我尝试使用 pyodbc(在 mac 上)连接到 sql server 数据库时:
import pyodbc
server = '####'
database = '####'
username = '####@####'
password = '#####'
driver='{ODBC Driver 13 for SQL Server}'
pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+password)
I get the following error:
我收到以下错误:
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not found (0) (SQLDriverConnect)")
错误:('01000',“[01000] [unixODBC][Driver Manager]无法打开 lib 'ODBC Driver 13 for SQL Server':找不到文件 (0) (SQLDriverConnect)”)
When I path in the actual driver location:
当我进入实际的驱动程序位置时:
driver='/usr/local/lib/libmsodbcsql.13.dylib'
It starts working!
它开始工作!
My odbcinst.ini
looks like this:
我的odbcinst.ini
看起来像这样:
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.13.dylib
UsageCount=1
How can I get my reference to driver='{ODBC Driver 13 for SQL Server}'
to start working again?
我怎样才能得到我的参考以driver='{ODBC Driver 13 for SQL Server}'
重新开始工作?
I initially used this guideto install the driver. And I'm using anaconda on Mac Sierra if that helps?
我最初使用本指南来安装驱动程序。如果有帮助,我在 Mac Sierra 上使用 anaconda 吗?
回答by emehex
Running:
跑步:
odbcinst -j
It yielded:
它产生了:
unixODBC 2.3.4
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/emehex/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Instead of copying the files to the /etc/
directory (not sure why unixODBC thought they were there) I created a symbolic link to each file:
我没有将文件复制到/etc/
目录(不知道为什么 unixODBC 认为它们在那里),而是为每个文件创建了一个符号链接:
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
This solved the problem.
这解决了问题。
回答by Uzzal Podder
In my case, I had to change the pyodbc
database driver string todriver='{ODBC Driver 17 for SQL Server}'
就我而言,我必须将pyodbc
数据库驱动程序字符串更改为driver='{ODBC Driver 17 for SQL Server}'
actually, in my python code pyodbcwas expecting ODBC Driver 13but, as the ODBC Driver version was updated (because of ubuntu update) to current version ODBC Driver 17, the problem had occurred.
实际上,在我的 python 代码中,pyodbc期待ODBC 驱动程序 13但是,随着 ODBC 驱动程序版本更新(由于 ubuntu 更新)到当前版本ODBC 驱动程序 17,问题发生了。
回答by Rene B.
In my case, I have a Mac OS and the following commands fixed the problem:
就我而言,我有一个 Mac 操作系统,以下命令解决了这个问题:
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install msodbcsql mssql-tools
回答by KevinQiang
In my case, I fixed the problem with three steps as follow:
就我而言,我通过以下三个步骤解决了该问题:
# Step1: install unixodbc
brew install unixodbc
# Step2: install Microsoft ODBC Driver for SQL Server on MacOS
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install msodbcsql mssql-tools
# Step3:verify odbcinst configuration path is correct
odbcinst -j
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
回答by Tomasz Bartkowiak
I've tried to establish a connection to a remote MS SQL Server deployed on a (Windows) machine from an external (Linux) machine. It took me some time to realise you need to first install the drivers on the machine that tries establish connection(i.e. Linux in my case)!
我试图从外部 (Linux) 机器建立到部署在 (Windows) 机器上的远程 MS SQL Server 的连接。我花了一些时间才意识到您需要先在尝试建立连接的机器上安装驱动程序(即我的情况下是 Linux)!
If you're using macOS/Linux what you need to do is to simply Install the Microsoft ODBC Driver for SQL Server on Linux and macOSand then follow instructions on Connecting to databasesfor your particular OS.
如果您使用的是 macOS/Linux,您只需在 Linux 和 macOS 上为 SQL Server 安装 Microsoft ODBC 驱动程序,然后按照连接到特定操作系统的数据库的说明进行操作。
回答by Evandro Pomatti
I was building a custom image on top of Python and this is the configuration that made it work:
我在 Python 之上构建了一个自定义图像,这是使其工作的配置:
FROM python:3.8 as pyodbc
COPY . /app
WORKDIR /app
# Required for msodbcsql17 and mssql-tools
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
# Key might change in the future, replace with new one on the logs
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA6932366A755776
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 mssql-tools
RUN apt-get install unixodbc-dev
RUN pip install pipenv
RUN pipenv install --system --deploy
CMD [ "python" , "__init__.py"]
回答by Konrad Sitarz
Installation that worked on the Ubuntu 18.04. I'm not sure if two of the ./bash_profile
and ./bashrc
exports are needed but I didn't have time to check.
在 Ubuntu 18.04 上运行的安装。我不确定是否需要两个./bash_profile
和./bashrc
出口,但我没有时间检查。
sudo apt-get update
ACCEPT_EULA=Y sudo apt-get -y install msodbcsql17 mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile \
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc \
sudo apt-get -y install unixodbc libc6 libstdc++6 libkrb5-3 libcurl3 openssl debconf unixodbc unixodbc-dev
Then as a driver in connection use ODBC Driver 17 for SQL Server
which is matching the current Azure version.
然后作为ODBC Driver 17 for SQL Server
与当前 Azure 版本匹配的连接使用驱动程序。
回答by Devesh Sharma
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list >
/etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install msodbcsql17
apt-get -y install unixodbc-dev
sudo apt-get install python3-pip -y
pip3 install --upgrade pyodbc
Use above steps to istall the odbc driver correctly and everything will fall in place.
使用上述步骤正确安装 odbc 驱动程序,一切都会就位。
回答by Patrick
I have to add that if you are using a different driver (FreeTDS) and in your connection string you omit to mention it, it will default to driver='{ODBC Driver 17 for SQL Server}
or something like like that.
我必须补充一点,如果您使用不同的驱动程序 (FreeTDS) 并且在您的连接字符串中省略提及它,它将默认为driver='{ODBC Driver 17 for SQL Server}
或类似的。
So the solution is not to forget driver, you DB settings will look like this:
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': '127.0.0.1',
'NAME': 'mydb',
'PORT': '1433',
'USER': 'sa',
'PASSWORD': '*****',
'OPTIONS':{
'driver': 'FreeTDS',
'host_is_server': True,
}
}
所以解决方案是不要忘记驱动程序,您的数据库设置将如下所示:
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': '127.0.0.1',
'NAME': 'mydb',
'PORT': '1433',
'USER': 'sa',
'PASSWORD': '*****',
'OPTIONS':{
'driver': 'FreeTDS',
'host_is_server': True,
}
}