Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError importing from azure.identity in Azure Function #19989

Closed
Valdeminas opened this issue Jul 29, 2021 · 16 comments
Closed

AssertionError importing from azure.identity in Azure Function #19989

Valdeminas opened this issue Jul 29, 2021 · 16 comments
Assignees
Labels
Azure.Identity bug This issue requires a change to an existing behavior in the product in order to be resolved. customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close.

Comments

@Valdeminas
Copy link

I'm trying to use azure keyvault in my python azure function.

When importing azure.keyvault.secrets, everything works fine.

However, when I add from azure.identity import DefaultAzureCredential, build fails with (slightly redacted for privacy):

[2021-07-29T12:41:02.772Z] Result: Failure
Exception: AssertionError:
Stack:   File "C:\Users\Username\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 301, in _handle__function_load_request
    func = loader.load_function(
  File "C:\Users\Username\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 40, in call
    return func(*args, **kwargs)
  File "C:\Users\Username\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\loader.py", line 83, in load_function
    mod = importlib.import_module(fullmodname)
  File "C:\Users\Username\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\Balance\__init__.py", line 5, in <module>
    from ..shared_code import utility
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\shared_code\utility.py", line 4, in <module>
    from azure.identity import DefaultAzureCredential
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\__init__.py", line 10, in <module>
    from ._credentials import (
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\_credentials\__init__.py", line 5, in <module>
    from .authorization_code import AuthorizationCodeCredential
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\_credentials\authorization_code.py", line 8, in <module>   
    from .._internal.aad_client import AadClient
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\_internal\__init__.py", line 52, in <module>
    from .interactive import InteractiveCredential
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\_internal\interactive.py", line 20, in <module>
    from .msal_credentials import MsalCredential
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\_internal\msal_credentials.py", line 11, in <module>       
    from .._persistent_cache import _load_persistent_cache, TokenCachePersistenceOptions
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\azure\identity\_persistent_cache.py", line 9, in <module>
    import msal_extensions
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\msal_extensions\__init__.py", line 12, in <module>
    from .cache_lock import CrossPlatLock
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\msal_extensions\cache_lock.py", line 5, in <module>
    import portalocker
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\portalocker\__init__.py", line 4, in <module>
    from . import portalocker
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\portalocker\portalocker.py", line 10, in <module>
    import pywintypes
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\win32\lib\pywintypes.py", line 105, in <module>
    __import_pywin32_system_module__("pywintypes", globals())
  File "D:\Users\Username\Source\Repos\ProjectName\ProjectName\.venv\lib\site-packages\win32\lib\pywintypes.py", line 99, in __import_pywin32_system_module__    
    assert sys.modules[modname] is not old_mod
.

This stack overflow question seems to deal with the same issue, but sadly does not have an answer :(

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 29, 2021
@chlowell chlowell added Azure.Identity and removed needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Jul 29, 2021
@chlowell chlowell self-assigned this Jul 29, 2021
@ghost ghost added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Jul 29, 2021
@chlowell
Copy link
Member

There's also an old issue on pywin32. I assume you're running this function locally on Windows with the Azure Functions CLI. This error doesn't repro in my environment with a function that imports DefaultAzureCredential and pywintypes. Can you share the output of pip freeze for your environment?

Here's mine for comparison:

azure-core==1.16.0
azure-functions==1.7.2
azure-identity==1.6.0
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.3
cryptography==3.4.7
idna==3.2
msal==1.13.0
msal-extensions==0.3.0
portalocker==1.7.1
pycparser==2.20
PyJWT==2.1.0
pywin32==301
requests==2.26.0
six==1.16.0
urllib3==1.26.6

@chlowell chlowell added needs-author-feedback Workflow: More information is needed from author to address the issue. and removed needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team labels Jul 29, 2021
@aaronsmith1234
Copy link

We are having this same issue, and here is the pip freeze output;

azure-functions==1.7.2
azure-identity==1.6.0
azure-storage-blob==12.8.1
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.3
cryptography==3.4.7
idna==3.2
isodate==0.6.0
msal==1.13.0
msal-extensions==0.3.0
msrest==0.6.21
numpy==1.21.1
oauthlib==3.1.1
portalocker==1.7.1
pycparser==2.20
PyJWT==2.1.0
pywin32==301
requests==2.26.0
requests-oauthlib==1.3.0
scipy==1.7.0
six==1.16.0
urllib3==1.26.6

@chlowell
Copy link
Member

chlowell commented Jul 29, 2021

Interesting, same version of the pertinent libraries. Which Python? I installed your libraries (plus azure-core 1.16.0) in my environment (Python 3.7.9) and still don't get the error.

I should also add I'm using Azure Functions CLI 3.0.3477.

@aaronsmith1234
Copy link

aaronsmith1234 commented Jul 29, 2021

3.9.2 64 bit Also, confirmed with co-worker that this is the line that is causing the error above.
from azure.identity import DefaultAzureCredential

@chlowell
Copy link
Member

Do you see the same error in a fresh venv? I don't see it on 3.9 either.

@aaronsmith1234
Copy link

It did; we deleted and re-did the virutal environment. We were using the Azure Functions add-in for VSCode to create the boilerplate code for the function; not sure if that's the same as the CLI.

Here is the code (can't post the rest of function, sorry!) which results in an error

import logging

import azure.functions as func
from azure.identity import DefaultAzureCredential

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    return 1

Azure Functions Core Tools
Core Tools Version: 3.0.3477 Commit hash: 5fbb9a76fc00e4168f2cc90d6ff0afe5373afc6d (64-bit)
Function Runtime Version: 3.0.15584.0

Python 3.9.2 64 bit, requirements.txt

azure-functions==1.7.2
azure-identity==1.6.0
azure-storage-blob==12.8.1
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.3
cryptography==3.4.7
idna==3.2
isodate==0.6.0
msal==1.13.0
msal-extensions==0.3.0
msrest==0.6.21
numpy==1.21.1
oauthlib==3.1.1
portalocker==1.7.1
pycparser==2.20
PyJWT==2.1.0
pywin32==301
requests==2.26.0
requests-oauthlib==1.3.0
scipy==1.7.0
six==1.16.0
urllib3==1.26.6

@chlowell
Copy link
Member

chlowell commented Aug 3, 2021

I don't see this error on my machine with everything installed as you describe. I think fixing it will require understanding why, in your particular environment, pywin32 loads an unexpected pywintypes (the code is here, if you're curious). Do you see a similar error running simply python -c "import azure.identity" and python -c "import pywintypes"?

@chlowell chlowell added bug This issue requires a change to an existing behavior in the product in order to be resolved. and removed needs-author-feedback Workflow: More information is needed from author to address the issue. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Aug 4, 2021
@ghost ghost added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Aug 4, 2021
@chlowell chlowell added this to the [2021] August milestone Aug 4, 2021
@chlowell chlowell removed the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Aug 4, 2021
@HoLengZai
Copy link

HoLengZai commented Aug 6, 2021

Hi @chlowell

I got a similar issue but I don't need to add any code. As soon I do this "cocktail" modules together, I will get a similar error message (not to say the same)

Here some info of my conf:

VSCode version: 1.58.2 (system setup)
Commit: c3f126316369cd610563c75b1b1725e0679adfb3
Date: 2021-07-14T22:10:15.214Z
Electron: 12.0.13
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Windows_NT x64 10.0.19042

I try to develop a Durable Function
I use Azurite extension and I start it before running the Durable Function

Here my requirements.txt

azure-functions
azure-functions-durable
azure-devops
azure-keyvault-secrets
azure-identity
azure-storage-blob

or "only"

azure-functions
azure-functions-durable
azure-devops
azure-identity

To reproduce my env:

  1. Press F1 > Azure Functions: Create Function
  2. Durable Functions orchestrator
  3. Name: DurableFunctionsOrchestrator1 (whatever, it's not important)
  4. Repeat the same to create the Http Starter and the Activity one
  5. if no virtual env created for your project: $>python -m venv .venv
  6. Press F5

All good it works

Then add this on your requirements.txt

azure-devops
azure-keyvault-secrets
azure-identity
azure-storage-blob

Run debug again, it will run but with a bunch of warnings:

> Executing task: .venv\Scripts\activate ; func host start <

Found Python version 3.9.5 (py).

Azure Functions Core Tools
Core Tools Version:       3.0.3477 Commit hash: 5fbb9a76fc00e4168f2cc90d6ff0afe5373afc6d  (64-bit)
Function Runtime Version: 3.0.15584.0


Functions:

        DurableFunctionsHttpStart1: [POST,GET] http://localhost:7071/api/orchestrators/{functionName}

        DurableFunctionsOrchestrator1: orchestrationTrigger

        Hello1: activityTrigger

For detailed output, run func with --verbose flag.
[2021-08-06T08:58:38.623Z] Worker process started and initialized.
[2021-08-06T08:58:38.701Z] Worker failed to function id 49e3735b-c31d-4712-943f-c2bc5d252778.
[2021-08-06T08:58:38.707Z] Result: Failure
Exception: AssertionError:
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 302, in _handle__function_load_request        
    func = loader.load_function(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 40, in call
    return func(*args, **kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\loader.py", line 83, in load_function
    mod = importlib.import_module(fullmodname)
  File "C:\Users\holengzai\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\holengzai\source\repos\test_durable_function\DurableFunctionsHttpStart1\__init__.py", line 10, in <module>
    import azure.functions as func
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\azure\__init__.py", line 5, in <module>
    import pkg_resources
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\__init__.py", line 74, in <module>
    from pkg_resources.extern import appdirs
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\extern\__init__.py", line 52, in create_module
    return self.load_module(spec.name)
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\extern\__init__.py", line 37, in load_module
    __import__(extant)
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\_vendor\appdirs.py", line 560, in <module>
    import win32com.shell
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32com\__init__.py", line 6, in <module>
    import pythoncom
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pythoncom.py", line 2, in <module>
    import pywintypes
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32\lib\pywintypes.py", line 105, in <module>
    __import_pywin32_system_module__("pywintypes", globals())
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32\lib\pywintypes.py", line 99, in __import_pywin32_system_module__
    assert sys.modules[modname] is not old_mod
.
[2021-08-06T08:58:38.745Z] Worker failed to function id 96615abc-719b-4eb8-9af1-27f5869407eb.
[2021-08-06T08:58:38.749Z] Result: Failure
Exception: AssertionError:
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 302, in _handle__function_load_request        
    func = loader.load_function(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 40, in call
    return func(*args, **kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\loader.py", line 83, in load_function
    mod = importlib.import_module(fullmodname)
  File "C:\Users\holengzai\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\holengzai\source\repos\test_durable_function\DurableFunctionsOrchestrator1\__init__.py", line 12, in <module>
    import azure.functions as func
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\azure\__init__.py", line 5, in <module>
    import pkg_resources
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\__init__.py", line 74, in <module>
    from pkg_resources.extern import appdirs
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\extern\__init__.py", line 52, in create_module
    return self.load_module(spec.name)
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\extern\__init__.py", line 37, in load_module
    __import__(extant)
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\_vendor\appdirs.py", line 560, in <module>
    import win32com.shell
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32com\__init__.py", line 6, in <module>
    import pythoncom
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pythoncom.py", line 2, in <module>
    import pywintypes
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32\lib\pywintypes.py", line 105, in <module>
    __import_pywin32_system_module__("pywintypes", globals())
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32\lib\pywintypes.py", line 99, in __import_pywin32_system_module__
    assert sys.modules[modname] is not old_mod
.
[2021-08-06T08:58:38.783Z] Worker failed to function id 8a5d6d9b-f87d-4cc6-b0b2-51b3f6779bac.
[2021-08-06T08:58:38.788Z] Result: Failure
Exception: AssertionError:
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 308, in _handle__function_load_request        
    self._functions.add_function(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\functions.py", line 88, in add_function
    elif bindings.has_implicit_output(desc.type):
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\bindings\meta.py", line 53, in has_implicit_output
    binding = get_binding(bind_name)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\bindings\meta.py", line 28, in get_binding
    registry = get_binding_registry()
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\bindings\meta.py", line 21, in get_binding_registry
    import azure.functions as func
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\azure\__init__.py", line 5, in <module>
    import pkg_resources
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\__init__.py", line 74, in <module>
    from pkg_resources.extern import appdirs
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\extern\__init__.py", line 52, in create_module
    return self.load_module(spec.name)
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\extern\__init__.py", line 37, in load_module
    __import__(extant)
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pkg_resources\_vendor\appdirs.py", line 560, in <module>
    import win32com.shell
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32com\__init__.py", line 6, in <module>
    import pythoncom
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\pythoncom.py", line 2, in <module>
    import pywintypes
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32\lib\pywintypes.py", line 105, in <module>
    __import_pywin32_system_module__("pywintypes", globals())
  File "C:\Users\holengzai\source\repos\test_durable_function\.venv\lib\site-packages\win32\lib\pywintypes.py", line 99, in __import_pywin32_system_module__
    assert sys.modules[modname] is not old_mod
.
[2021-08-06T08:58:40.103Z] Host lock lease acquired by instance ID '0000000000000000000000000166BC8F'.

If on my requirements.txt, I put:

azure-keyvault-secrets
azure-identity
azure-storage-blob

or

azure-devops
azure-keyvault-secrets
azure-storage-blob

After recreating my .venv, I don't have any issue

It is as soon as I put "azure-identity" and "azure-devops" together, it "corrupts" my virtual env

Here my pip freeze (from my virtual env):

aiohttp==3.7.4.post0
async-timeout==3.0.1
attrs==21.2.0
azure-common==1.1.27
azure-core==1.17.0
azure-devops==6.0.0b4
azure-functions==1.7.2
azure-functions-durable==1.0.3
azure-identity==1.6.0
azure-keyvault-secrets==4.3.0
azure-storage-blob==12.8.1
certifi==2021.5.30
cffi==1.14.6
chardet==4.0.0
charset-normalizer==2.0.4
cryptography==3.4.7
furl==2.1.2
idna==3.2
isodate==0.6.0
msal==1.13.0
msal-extensions==0.3.0
msrest==0.6.21
multidict==5.1.0
oauthlib==3.1.1
orderedmultidict==1.0.1
portalocker==1.7.1
pycparser==2.20
PyJWT==2.1.0
python-dateutil==2.8.2
pywin32==301
requests==2.26.0
requests-oauthlib==1.3.0
six==1.16.0
typing-extensions==3.10.0.0
urllib3==1.26.6
yarl==1.6.3

I tried a different version of "pywin32" (300, 228) I got the same issue.

I tried on Windows Subsystem Linux and I don't get any issue to debug the durable function.
(I got an error with "wheel" module on the first run but after that success to build azure-devops module and then no issue.)

Thanks

@Valdeminas
Copy link
Author

Sorry, for late reply. Was out on vacation.

Here's my pip freeze output:

azure-core==1.16.0
azure-identity==1.6.0        
azure-keyvault-secrets==4.3.0
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.3    
cryptography==3.4.7
idna==3.2
isodate==0.6.0
msal==1.13.0
msal-extensions==0.3.0
msrest==0.6.21
oauthlib==3.1.1
portalocker==1.7.1
pycparser==2.20
PyJWT==2.1.0
pywin32==301
requests==2.26.0
requests-oauthlib==1.3.0
six==1.16.0
urllib3==1.26.6
Python version: 3.9.6

Azure Functions Core Tools:

Core Tools Version:       3.0.3568 Commit hash: e30a0ede85fd498199c28ad699ab2548593f759b  (64-bit)
Function Runtime Version: 3.0.15828.0

@chlowell
Copy link
Member

I'm unable to reproduce this exception using any of the environments described above, so the root cause must be something more subtle. I think the best we can do in azure-identity is to enable a workaround. I've done that in azure-identity 1.7.0b3, which is now available on PyPI. With 1.7.0b3, you can avoid importing pywin32 by not using a persistent cache. I doubt you'll miss anything by doing this--persistent caching doesn't make sense for a Functions app and during local development you're probably better served by authenticating through the Azure CLI or VS Code. SharedTokenCacheCredential still won't work, because it requires a persistent cache, but all the other credentials should be useable in 1.7.0b3. If you're using DefaultAzureCredential, you'll need to add an optional argument, unless you're configuring authentication through environment variables: DefaultAzureCredential(exclude_shared_token_cache_credential=True). Please try 1.7.0b3 and let me know if this workaround works for you.

@chlowell chlowell removed this from the [2021] August milestone Aug 10, 2021
@chrisasa
Copy link

To add to this issue, I am facing the same issue from VSCode when I am loading the azure-identity module:
from azure.identity import DefaultAzureCredential

All these when I am using the debugger form vs code. Once I go to PowerShell 7 terminal and activate the same .venv there is no problem at all to start it (func host start)
In both cases I am following identical steps:
1. .venv\Scripts\python -m pip install -r requirements.txt
2. .venv\Scripts\activate ; func host start

The issue should be related to the Azure function plugin or something on VS Code, as I changed the vs code settings to use as default terminal the PS 7 one (C:\Program Files\PowerShell\7\pwsh.exe) instead of the cmd.exe. Again though, in vs code execution I am getting the same error, and if I run an instance of the terminal alone all good.

@Valdeminas
Copy link
Author

@chlowell using 1.7.0b3 solved the issue.

Thanks!

@aaronsmith1234
Copy link

I don't see this error on my machine with everything installed as you describe. I think fixing it will require understanding why, in your particular environment, pywin32 loads an unexpected pywintypes (the code is here, if you're curious). Do you see a similar error running simply python -c "import azure.identity" and python -c "import pywintypes"?

Just as a note, importing both of those using the commands above, in a VSCode Powershell terminal activated in my venv, worked fine with no errors, NOT using 1.7.0b3. However, if i then go and run my function in debug mode (F5), I still get the error

@chlowell
Copy link
Member

chlowell commented Aug 18, 2021

Thank you everyone for contributing so much detail here. I've released a new stable version, 1.6.1, with the fix from 1.7.0b3. To summarize the situation:

  • this issue affects Azure Functions development on Windows
  • pywin32 is a transitive dependency of azure-identity used to implement a persistent cache (on Windows)
  • pywin32 asserts that one of its modules is loaded exactly once (see its code here)
  • This assertion can--but doesn't always--fail while running an Azure Function. Perhaps a component of the Functions host triggers a reload of the module as in AssertionError when reloading pywintypes mhammond/pywin32#1209
  • The latest azure-identity releases (1.6.1 and 1.7.0b3) import pywin32 only when loading a persistent cache. This enables a workaround for pywin32's AssertionError: don't use SharedTokenCacheCredential or configure another credential to use a persistent cache. For DefaultAzureCredential, this requires a keyword argument:
# prevent DefaultAzureCredential trying to load a persistent cache
DefaultAzureCredential(exclude_shared_token_cache_credential=True)
  • persistent caching doesn't make sense for a deployed Functions app, so you won't miss anything in production by disabling it

I'm marking this issue resolved because I believe there's nothing more we can do in the Azure SDK to address it. Please feel free to unresolve this issue if you want to continue the discussion.

@chlowell chlowell added the issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. label Aug 18, 2021
@ghost
Copy link

ghost commented Aug 18, 2021

Hi @Valdeminas. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

@chlowell chlowell changed the title Azure function fails to import azure.identity AssertionError importing from azure.identity in Azure Function Aug 19, 2021
@ghost
Copy link

ghost commented Aug 26, 2021

Hi @Valdeminas, since you haven’t asked that we “/unresolve” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve” to reopen the issue.

@ghost ghost closed this as completed Aug 26, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Azure.Identity bug This issue requires a change to an existing behavior in the product in order to be resolved. customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close.
Projects
None yet
Development

No branches or pull requests

5 participants