Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

Can't use pyodbc #1272

Closed svickers closed 9 months ago

svickers commented 1 year ago

Starting from a fresh function app with the v2 model it will stop working when you import pyodbc.

Investigative information

Repro steps

  1. func init python_test_func --python -m V2
  2. pip install pyodbc
  3. Create file function_app.py:
    
    import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="HttpTrigger1") @app.route(route="hello") def test_function(req: func.HttpRequest) -> func.HttpResponse: return func.HttpResponse("HttpTrigger1 function processed a request!")

4. run `func start` to verify it works.
5. add `import pyodbc` to top of function_app.py
6. run `func start`

### Expected behavior
Should continue to work and serve requests.

### Actual behavior

Result: Failure Exception: ModuleNotFoundError: No module named 'pyodbc'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 338, in _handlefunctions_metadata_request fx_metadata_results = self.index_functions(function_path) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 607, in index_functions indexed_functions = loader.index_function_app(function_path) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 48, in call raise extend_exception_message(e, message) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 44, in call return func(*args, **kwargs) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10\WINDOWS\X64\azure_functions_worker\loader.py", line 151, in index_function_app imported_module = importlib.import_module(module_name) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64qbz5n2kfra8p0\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "C:\Users\me\dev\python_test_func\function_app.py", line 2, in import pyodbc



### Known workarounds

None

### Contents of the requirements.txt file:
azure-functions
pyodbc
bhagyshricompany commented 1 year ago

Hi @svickers pls try again its working fine as I tested the step you provided.

image image image
svickers commented 1 year ago

@bhagyshricompany Did you add import pyodbc to the top of the function_app.py file?

bhagyshricompany commented 1 year ago

yes as you can see here.

image image
svickers commented 1 year ago

Interesting... I pushed a basic test repo up here: https://github.com/svickers/python_test_func

Here is my environment information:

func start
Found Python version 3.10.11 (python3).

Azure Functions Core Tools
Core Tools Version:       4.0.5198 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.21.1.20667
bhagyshricompany commented 1 year ago

pls check your file format have and directory structure

image
svickers commented 1 year ago

Everything looks the same, very strange. I've tried two different projects from scratch in fresh directories and environments but no luck...not sure what else could be going on. Also referencing other libraries works fine, just pyodbc that I've had an issue with.

Screenshot_549

svickers commented 1 year ago

Also one other thing to note is I can deploy to a function app just fine until I add the pyodbc reference and then it also fails to start and fails to index any functions.... which makes me think it is more than just my local environment.

Screenshot_550 Screenshot_551

If I remove the single import pyodbc line then everything works fine in azure:

Screenshot_552

bhagyshricompany commented 1 year ago

I deployed code with pyodbc its working fine.

image image image image
svickers commented 1 year ago

Can you post your repo somewhere so I can try it?

bhagyshricompany commented 1 year ago

you downloaded python from msstore or python.org install from python.org then try

svickers commented 1 year ago

Installed from python.org.

bhagyshricompany commented 1 year ago

still not working ?

svickers commented 1 year ago

Yes still not working.

bhagyshricompany commented 1 year ago

@vrdmr pls comment and validate

YunchuWang commented 1 year ago

Tried the test repo and working for me too. Here is my func info: Found Python version 3.10.11 (py).

Azure Functions Core Tools Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit) Function Runtime Version: 4.21.1.20667

image

Here is my local.settings.json file:

image

@svickers Wonder whats your local.settings.file look like and can you try the core tool version i have with a new virtual environment created?

microsoft-github-policy-service[bot] commented 1 year ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

svickers commented 1 year ago

Still seeing the same issue with a fresh venv, really unsure what is going on because the same code also doesn't work in azure, which shouldn't have anything to do with my local setup.

Screenshot_609

My local.settings.json file is:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true"
  }
}
yorek commented 1 year ago

Hi, I'm having the same issue. It seems it happens only when I'm trying to run func start and I have an active virtual enviroment, and the packages needed are only installed in the virtual enviroment.

KKiriri commented 1 year ago

Same issue here. Only difference is that I am using v1 python model, running with init.py. Both local environment and Azure function environment have trouble importing pyodbc.

hallvictoria commented 9 months ago

Hi, just to follow up on this.

I tested this with this setup:

image

I was able to replicate the error by doing this:

  1. python -m venv .venv
  2. .venv/Scripts/activate
  3. func start

image image

From this, it looks like pyodbc has not been installed correctly.

I then tried running pip install -r requirements.txt and it started correctly:

  1. python -m venv .venv
  2. .venv/Scripts/activate
  3. pip install -r requirements.txt
  4. func start

image

I also tested deploying the app from VSCode (with pyodc added in requirements.txt and imported) and that was successful.

image image image

Is this still not working correctly on your end?

GengisGutierrez commented 7 months ago

I have been looking for a solution to my deployment error, and I found the error .... So, the pyodbc library works with an ODBC SQL driver. So make sure you know which driver your Azure function environment works with. If you are using Python 10, it seems to me that by default it enables a driver of the type DRIVER={ODBC Driver 17 for SQL Server} . And searching through several forums, I stopped to check the driver for my environment... and it turned out that for the version of Python 11 as the environment language, the driver is DRIVER={ODBC Driver 18 for SQL Server}.

My advice will be. First make sure you know what ODBC drivers your app has with the code drivers = pyodbc.drivers()

For local tests it works the same, the driver I used was DRIVER={SQL Server} ... My connection code was conn = pyodbc.connect( "DRIVER={ODBC Driver 18 for SQL Server};" f"SERVER=tcp:{server};" f"DATABASE={database};UID={user};" f"PWD={password};" "Encrypt=yes;" "TrustServerCertificate=no;" "Connection Timeout=30;" )

Winkielek commented 6 months ago

In case anyone has still problem with it. For me it turned out that I forgot to install an ODBC driver manager on my Linux (dev container) prior to installing pyodbc.

According to the pydobc documentation:

You should make sure you have an ODBC driver manager installed before installing pyodbc. See the docs for more information about how to do this on different Unix flavors. (On Windows, the ODBC driver manager is built-in.)

jacksorjacksor commented 5 months ago

Hi - I'm having this same issue. Windows 11 Azure Functions CLI v4.0.5455 Python 3.11.9

Steps to reproduce: Create a new folder Using Powershell 7 func new --template "Http Trigger" --name MyHttpTrigger --worker-runtime Python python -m venv .venv .\.venv\scripts\Activate.ps1 pip install -r requirements.txt pip install pyodbc pip freeze > requirements.py

In function_app.py add line import pyodbc (we literally just need to import the library, we don't even do any code!)

func start

This leads to two different errors:

  1. Error in index_function_app. Sys Path: [....loads of locations....] ,python-packages Path exists: False

  2. Worker failed to index functions Result: Failure Exception: ModuleNotFoundError: No module named 'pyodbc'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound

Any help would be GREATLY appreciated.

I've made a YT video showing this: https://youtu.be/dR4i41d5E8Q

Thanks!

Rich

jacksorjacksor commented 5 months ago

FIXED: installed Python from Python.org. Version downloaded from Microsoft Store resulted in these issues.

https://github.com/Azure/azure-functions-core-tools/issues/3042#issuecomment-1113848949