1
votes

I'm having a challenging time getting the Python azure-cosmos library to correctly load for the purposes of locally testing a function in VS Code.

The specific error I'm getting (with the file path shortened) is: Exception: ImportError: cannot import name 'exceptions' from 'azure.cosmos' ([shortened]/.venv/lib/python3.8/site-packages/azure/cosmos/__init__.py)

Things I've checked/tried so far:

  • Check that requirements.txt specifies azure-cosmos
  • Manually go into python for each of the interpreters available within VS code and ensure I can manually import azure.cosmos
  • As instructed here, attempt to reinstall the azure-cosmos library using pip3 and ensuring the --pre flag is used.
  • [Updated] Verified I can successfully import azure.cosmos.cosmos_client as cosmos_client without any errors

Any ideas? Thanks! Below is the relevant section of my code.

import datetime
import logging
import tempfile
import requests
import os
import zipfile
import pandas as pd
import azure.functions as func
from azure.cosmos import exceptions, CosmosClient, PartitionKey

def main(mytimer: func.TimerRequest, calendars: func.Out[func.Document]) -> None:
    logging.info("Timer function has initiated.")
2
So you are not using the azure function cosmos binding?George Chen
@GeorgeChen Yes, actually, I intend to eventually. Once I implement that, does this library become unnecessary? (Sorry for the basic question; still new to Azure.) Even still, I'd expect the library to be able to load given that it loads everywhere else.George
On my side, it is no problem. It works well. Actually, Azure Function SDK contains some necessary packages. What the Python 3.8 you are using? 64bit or 32 bit? Can you show your code?Cindy Pau
@BowmanZhu I'm using Python 3.8.0 64-bit.George
Hi, I think the root reason is the version of the packages. But only run install pre will not solve. You need to delete all of the related packages first, otherwise it will not work.Cindy Pau

2 Answers

1
votes

This is what you face now:

enter image description here

This is the offcial doc:

https://github.com/Azure-Samples/azure-cosmos-db-python-getting-started

This doc tells you how to solve this problem.

So the solution is to install pre version.(George Chen's solution is right.)

Didn't install the pre version is the root reason, but please notice that, you need to first delete the package. Otherwise, the pre version will not be installed.(Only run install pre will not solve this problem, you need to delete all of the related packages first. And then install the pre package.)

enter image description here

1
votes

Whether azure.cosmos is needed depends on whether function binding meets your needs, if the binding could do what you want suppose you don't need to use azure.cosmos.

About this import error, I could reproduce this exception, and I check the github solution it have to add a --pre flag.

So my solution is go to task.json under .vscde, add the flag to the command like below.

enter image description here

If you want to get more details about cosmos binding you could refer to this doc:Azure Cosmos DB trigger and bindings