denisenkom / pytds

Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation
MIT License
190 stars 52 forks source link

DistributionNotFound error when using python-tds on AWS Lambda #146

Closed paldana closed 1 year ago

paldana commented 1 year ago

Hi, I'm trying to use pytds on a lambda function to connect to an MS SQL database. Everything works fine when I run the code locally on a MacOS computer. However, when I try to test it on the actual Lambda function on AWS Console, I'm getting the "DistributionNotFound" error. Below is the complete log

{ "errorMessage": "The 'python-tds' distribution was not found and is required by the application", "errorType": "DistributionNotFound", "requestId": "", "stackTrace": [ " File \"/var/lang/lib/python3.9/importlib/init.py\", line 127, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n", " File \"\", line 1030, in _gcd_import\n", " File \"\", line 1007, in _find_and_load\n", " File \"\", line 986, in _find_and_load_unlocked\n", " File \"\", line 680, in _load_unlocked\n", " File \"\", line 850, in exec_module\n", " File \"\", line 228, in _call_with_frames_removed\n", " File \"/var/task/lambda_function.py\", line 5, in \n from test_mssql import connect_mssql\n", " File \"/var/task/test_mssql.py\", line 4, in \n import pytds\n", " File \"/var/task/pytds/init.py\", line 46, in \n version = pkg_resources.get_distribution('python-tds').version\n", " File \"/var/lang/lib/python3.9/site-packages/pkg_resources/init.py\", line 466, in get_distribution\n dist = get_provider(dist)\n", " File \"/var/lang/lib/python3.9/site-packages/pkg_resources/init.py\", line 342, in get_provider\n return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]\n", " File \"/var/lang/lib/python3.9/site-packages/pkg_resources/init.py\", line 886, in require\n needed = self.resolve(parse_requirements(requirements))\n", " File \"/var/lang/lib/python3.9/site-packages/pkg_resources/init.py\", line 772, in resolve\n raise DistributionNotFound(req, requirers)\n" ] }

How should I resolve this issue?

Thanks,

Paul

takoau commented 1 year ago

Hi Paul,

Lambda does not support a proper module installation so package manager is empty, while python-tds needs it. Not much can be done unless hacking the source.

Run the following on your Mac:

import pkg_resources pkg_resources.get_distribution('python-tds').version '1.11.0'

Hardcode line 46 of init.py with this value.

Regards, Tako

paldana commented 1 year ago

Hi Tako,

Thanks for your response! Hardcoding the version number worked. Hopefully there will be a better way to do this in the future.

For future reference, I had to download the source code and include pytds directory with the lambda handler function to make it work.

Thanks again!

Best,

Paul