Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to install Pyodbc for Sqlserver.
# Start a container that mimic the lambda environment
docker run -it --rm --entrypoint bash -e ODBCINI=/var/task -e ODBCSYSINI=/var/task -v "$PWD":/var/task lambci/lambda:build-python2.7
# Then, download ODBC source code, compile and take the output
curl ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.5.tar.gz -O
tar xvzf unixODBC-2.3.5.tar.gz
cd unixODBC-2.3.5
./configure --sysconfdir=/var/task --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --prefix=/home
make install
cd ..
mv /home/* .
mv unixODBC-2.3.5 unixODBC-2.3.5.tar.gz /tmp/
# Install MSsql odbc driver
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
ACCEPT_EULA=Y yum -y install msodbcsql
export CFLAGS="-I/var/task/include"
export LDFLAGS="-L/var/task/lib"
# Then you can install pyodbc (or pip install -t . -r requirements.txt)
pip install pyodbc -t .
cp -r /opt/microsoft/msodbcsql .
cat <<EOF > odbcinst.ini
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/var/task/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2
UsageCount=1
EOF
cat <<EOF > odbc.ini
[ODBC Driver 13 for SQL Server]
Driver = ODBC Driver 13 for SQL Server
Description = My ODBC Driver 13 for SQL Server
Trace = No
EOF
# Test if it works
python -c "import pyodbc; print(pyodbc.drivers());"
python -c 'import pyodbc;conn = pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server}; SERVER=YOUr_SERVER:ADD;PORT=1443;DATABASE=TestDB;UID=SA;PWD=<YourStrong!Passw0rd>");crsr = conn.cursor();rows = crsr.execute("select @@VERSION").fetchall();print(rows);crsr.close();conn.close()'
@matw11180

This comment has been minimized.

Copy link

matw11180 commented May 26, 2019

@carlochess

Having an issue with the install of ACCEPT_EULA=Y yum -y install msodbcsql

Coming up with a dependency issue of unixODBC-2.3.1
Can't see what I am doing wrong, following the instructions to the letter

@matw11180

This comment has been minimized.

Copy link

matw11180 commented May 26, 2019

Is it possible that the Amazon AMI update to 2018.03 (23rd May I believe) that could have caused this?

@gayathriramanujam

This comment has been minimized.

Copy link

gayathriramanujam commented May 28, 2019

running into same issue as well, its the updated packages causing issues. Will post here if I find anything useful

@carlochess

This comment has been minimized.

Copy link
Owner Author

carlochess commented May 28, 2019

I will check an alternative to this because Its seems that the msodbcsql package is using an unixODBC dependency that is no longer present in the resent AMIs.

bash-4.2# ACCEPT_EULA=Y yum -y install msodbcsql
Loaded plugins: ovl, priorities
4 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package msodbcsql.x86_64 0:13.1.9.2-1 will be installed
--> Processing Dependency: unixODBC >= 2.3.1 for package: msodbcsql-13.1.9.2-1.x86_64
--> Processing Dependency: e2fsprogs for package: msodbcsql-13.1.9.2-1.x86_64
--> Processing Dependency: libodbcinst.so.2()(64bit) for package: msodbcsql-13.1.9.2-1.x86_64
--> Running transaction check
---> Package e2fsprogs.x86_64 0:1.43.5-2.43.amzn1 will be installed
--> Processing Dependency: libss = 1.43.5-2.43.amzn1 for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: e2fsprogs-libs(x86-64) = 1.43.5-2.43.amzn1 for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libfuse.so.2(FUSE_2.8)(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libfuse.so.2(FUSE_2.6)(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libfuse.so.2(FUSE_2.5)(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libss.so.2()(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libfuse.so.2()(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libext2fs.so.2()(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
--> Processing Dependency: libe2p.so.2()(64bit) for package: e2fsprogs-1.43.5-2.43.amzn1.x86_64
---> Package msodbcsql.x86_64 0:13.1.9.2-1 will be installed
--> Processing Dependency: unixODBC >= 2.3.1 for package: msodbcsql-13.1.9.2-1.x86_64
---> Package unixODBC.x86_64 0:2.2.14-14.7.amzn1 will be installed
--> Running transaction check
---> Package e2fsprogs-libs.x86_64 0:1.43.5-2.43.amzn1 will be installed
---> Package fuse-libs.x86_64 0:2.9.4-1.18.amzn1 will be installed
---> Package libss.x86_64 0:1.43.5-2.43.amzn1 will be installed
---> Package msodbcsql.x86_64 0:13.1.9.2-1 will be installed
--> Processing Dependency: unixODBC >= 2.3.1 for package: msodbcsql-13.1.9.2-1.x86_64
--> Finished Dependency Resolution
Error: Package: msodbcsql-13.1.9.2-1.x86_64 (packages-microsoft-com-prod)
           Requires: unixODBC >= 2.3.1
           Available: unixODBC-2.2.14-14.7.amzn1.i686 (amzn-main)
               unixODBC = 2.2.14-14.7.amzn1
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
@matw11180

This comment has been minimized.

Copy link

matw11180 commented May 28, 2019

Thanks Carlo. That is the exact screenshot that I am getting when trying to complete the package build

@gayathriramanujam

This comment has been minimized.

Copy link

gayathriramanujam commented May 29, 2019

Confirming that the same steps work in Redhat AMI.

@Sahilshetye

This comment has been minimized.

Copy link

Sahilshetye commented Jun 5, 2019

run with

 yum install unixODBC --disablerepo=amzn*

you need to bypass the amzn repolist for installing correct unixODBC

@ca0abinary

This comment has been minimized.

Copy link

ca0abinary commented Jul 30, 2019

I created a Dockerfile based on this and the fixes mentioned since. It creates a .zip file usable as a lambda layer.
https://gist.github.com/ca0abinary/e4825841d47d987ffc78ed62e5619055

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.