Python脚本作业未在Azure Batch上运行-ODBC错误

时间:2018-11-12 17:59:48

标签: python azure odbc azure-virtual-machine azure-batch

我正在尝试使用Azure Batch上的VM运行Python作业。这是一个向我的Azure SQL数据库添加行的简单脚本。我直接从Azure门户下载了ODBC连接字符串,但出现此错误。奇怪的是,我可以在自己的机器上完美运行脚本。我已将VM配置为安装所需的Python版本,然后执行我的脚本-我完全不知所措。有什么想法吗?

cnxn = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};Server=tcp:svr-something.database.windows.net,fakeport232;Database=db-something-prod;Uid=something@svr-something;Pwd{fake_passwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')

回溯(最近通话最近一次):

  

文件“ D:\ batch \ tasks \ apppackages \ batch_python_test1.02018-11-12-14-         30 \ batch_python_test \ python_test.py“,第12行           r'Driver = {用于SQL Server的ODBC驱动程序13}; Server = tcp:svr-         mydatabase.database.windows.net,”         pyodbc.InterfaceError :(“ IM002”,“ [IM002] [Microsoft] [ODBC驱动程序管理器]         数据         找不到源名称,也未指定默认驱动程序(0)(SQLDriverConnect)')

2 个答案:

答案 0 :(得分:0)

作为Azure Batch的新手,我没有意识到虚拟机未安装ODBC驱动程序。分配池后,我编写了一个.bat文件以在节点上安装驱动程序。问题解决了。

答案 1 :(得分:0)

您必须在池的每个计算节点中安装ODBC驱动程序。

将以下命令放在外壳程序脚本文件startup_tasks.sh中:

sudo apt-get -y update;
export DEBIAN_FRONTEND=noninteractive;
sudo apt-get install -y python3-pip;
apt-get install -y --no-install-recommends apt-utils apt-transport-https;
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - ;
curl https://packages.microsoft.com/config/debian/9/prod.list > 
/etc/apt/sources.list.d/mssql-release.list ;
sudo apt-get -y update ;
ACCEPT_EULA=Y apt-get -y install msodbcsql17 ;
ACCEPT_EULA=Y apt-get -y install mssql-tools ;
echo 'export PATH=\"$PATH:/opt/mssql-tools/bin\"' >> ~/.bash_profile ;
echo 'export PATH=\"$PATH:/opt/mssql-tools/bin\"' >> ~/.bashrc ;
source ~/.bashrc && sudo apt-get install -y unixodbc unixodbc-dev ;

将bin / bash -c“ startup_tasks.sh”作为启动任务在azure批处理池中。这将在每个节点中安装ODBC驱动程序17。

然后在您的连接字符串中,将ODBC驱动程序版本更改为17