Working with Azure CosmosDB from Python (quick start digging)

This time, we will see how to operate Azure Cosmos DB from Python with reference to the quick start.

Preparation

First of all Latest Python installation-> https://www.python.org/ Perform a Python installation in Visual Studio Code. Installing Python in VS Code is optional, but it's very useful because you can mess with Python directly in VS Code and there are various shortcuts. https://marketplace.visualstudio.com/items?itemName=ms-python.python#overview image.png

Python app Clone

Clone the code from GitHub published by Microsoft. git clone https://github.com/Azure-Samples/azure-cosmos-db-python-getting-started.git

Open the Visual Studio Terminal and navigate to the cloned local directory.

Deploy Cosmos DB

Deploy Azure Cosmos DB. This time we'll create a simple Cosmos DB, so we'll deploy it from the Azure CLI.

az cosmosdb create --name <account-name> --resource-group <resource-group-name>

Once created, run the following command from the Azure CLI to get the Endpoint and Key.

az cosmosdb keys list --name <account-name> --resource-group <resource-group-name>
az cosmosdb show --name <account-name> --resource-group <resource-group-name>

Deploy from Visual Studio Terminal

Execute the following code from the Visual Studio terminal that goes to the corresponding directory.

python cosmos_get_started.py

Then the following is output.

Read item with id Smith_d457f895-3756-49cd-a629-e7708d7ed252. Operation consumed 1 request units
Read item with id Johnson_3bd2aa16-742f-4aed-9a95-3adc548a94e3. Operation consumed 1 request units
Read item with id Wakefield_c73aa98d-7104-4b01-a3da-4946081575ff. Operation consumed 1 request units
Query returned 2 items. Operation consumed 3.09 request units

When I checked the Portal page, I was able to confirm that I was able to operate Cosmos DB.

Deep digging

I was a little addicted to this time when the following error occurred.

Traceback (most recent call last):
File "cosmos_get_started.py", line 1, in <module>
    from azure.cosmos import exceptions, CosmosClient, PartitionKey
ImportError: cannot import name 'exceptions' from 'azure.cosmos' (...\lib\site-packages\azure\cosmos\__init__.py)

This error is caused by an incorrect installation of the Azure Cosmos DB SDK. If you have the -perp version of the Azure Cosmos SDK installed, you will run into this error. The solution is a workaround as described in the Python documentation below.

https://pypi.org/project/azure-cosmosdb-table/ (Reference source) https://docs.microsoft.com/ja-jp/azure/cosmos-db/table-sdk-python

Also, the following page describes the correct installation method of the Python SDK, so it will be helpful to take a look!

https://github.com/Azure/azure-cosmos-table-python/tree/master/azure-cosmosdb-table

Recommended Posts

Working with Azure CosmosDB from Python (quick start digging)
Working with Azure CosmosDB from Python Part.2
Python quick start
Python --Quick start of logging
Working with LibreOffice in Python
Working with sounds in Python
Start / stop GCE from python
With skype, notify with skype from python!
Call C from Python with DragonFFI
Using Rstan from Python with PypeR
Working with LibreOffice in Python: import
Trial of voice recognition using Azure with Python (input from microphone)
Install Python from source with Ansible
Create folders from '01' to '12' with python
Run Aprili from Python with Orange
Use Azure Blob Storage from Python
Call python from nim with Nimpy
Read fbx from python with cinema4d
Working with DICOM images in Python
Efficiently develop Azure Python apps with CI/CD
Collecting information from Twitter with Python (Twitter API)
Receive textual data from mysql with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Create wordcloud from your tweet with python3
Try working with binary data in Python
Using Python and MeCab with Azure Databricks
Tweet from python with Twitter Developer + Tweepy
Post Test 3 (Working with PosgreSQL in Python)
Decrypt files encrypted with openssl from python with openssl
[Python] A quick web application with Bottle!
[Azure] Hit Custom Vision Service with Python
Try calling Python from Ruby with thrift
Scraping from an authenticated site with python
Use C ++ functions from python with pybind11
[Python] Start diary from today Atcorder ABC058-B
Use Python and MeCab with Azure Functions
Working with GPS on Raspberry Pi 3 Python
Collecting information from Twitter with Python (Environment construction)
Csv output from Google search with [Python]! 【Easy】
Start / stop GCE from python
Extract text from PowerPoint with Python! (Compatible with tables)
Image processing from scratch with python (5) Fourier transform
Start a simple Python web server with Docker
Wrap C with Cython for use from Python
~ Tips for Python beginners from Pythonista with love ① ~
Make OpenCV3 available from python3 installed with pyenv
Image processing from scratch with python (4) Contour extraction
Generate an insert statement from CSV with Python.
Try working with Mongo in Python on Mac
Bulk download images from specific URLs with python
Install vim7.3 (+ python2.4) from source (compatible with Gundo.vim)
Use Python and word2vec (learned) with Azure Databricks
Wrap C ++ with Cython for use from Python
Wav file generation from numeric text with python
Read line by line from a file with Python
Make JSON into CSV with Python from Splunk
Homebrew Vim doesn't start with Python 3.8 error Note
From Python environment construction to virtual environment construction with anaconda
Use Python / Django with Windows Azure Cloud Service!