Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.56k stars 2.78k forks source link

ModuleNotFoundError: No module named 'azure.cosmos' #36330

Closed mapenzo closed 2 months ago

mapenzo commented 3 months ago

Describe the bug Trying to run my FastAPI app with Docker, I get the following error:

_ File "/app/common/azure/data/cosmos/init.py", line 1, in from common.azure.data.cosmos.cosmos_sql_client import CosmosSqlClient File "/app/common/azure/data/cosmos/cosmos_sql_client.py", line 10, in from azure.cosmos.cosmosclient import CosmosClient ModuleNotFoundError: No module named 'azure.cosmos'

In the development virtual environment, the API runs perfectly, and I can save and retrieve documents from Cosmos DB.

The issue occur when I tried to start the docker container app.

To Reproduce Steps to reproduce the behavior:

  1. Build the docker container: docker build -t mywebapi .
  2. Run the container locally: docker run -p 8000:8000 mywebapi
  3. Expected behavior The expected behavior is as the following image depicts image

Screenshots If applicable, add screenshots to help explain your problem.

Additional context I am using FastAPI and GUnicorn and in the following list are the packages I am using in the project. asyncio==3.4.3 aiohttp==3.9.5 fastapi==0.110.3 fastapi_msal[full]==2.1.5 uvicorn[standard]==0.23.2 uvicorn-worker==0.2.0 gunicorn==22.0.0 langchain-community==0.2.5 langchain-openai==0.1.8 pypdf==4.2.0 pydantic==2.7.4 pydantic-settings==2.3.3 python-dotenv==1.0.1 python-dateutil==2.9.0.post0 azure-core==1.28.0 azure-cosmos==4.7.0 azure-storage-blob==12.20.0 azure-storage-queue==12.10.0

github-actions[bot] commented 3 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @AbhinavTrips @bambriz @pilchie @pjohari-ms @simorenoh.

mapenzo commented 3 months ago

Doing some troubleshooting within the container, I checked the Python paths, and it seems to be okey.

image

The same error occur when I tried to import the CosmosClient from the Python console.

image

simorenoh commented 2 months ago

Hi @mapenzo, this seems to be an issue with docker and your application more so than any issue with the SDK - like you said, the SDK even works well in your development virtual environment. I would think this is coming from your docker application not downloading the proper library within its context, and as such being unable to find it - you may be missing a script for pip to install the needed dependencies. If this is still happening I recommend looking into that side of things, something like this may be what you're missing: https://stackoverflow.com/questions/61518741/python-project-cant-find-modules-inside-of-docker

mapenzo commented 2 months ago

Hi @simorenoh, thanks for taking the time to reply. I have finally found the problem. The problem is that if you set the PYTHONPATH variable in the docker file, Python will not be able to find any installed packages because it will first find packages in the paths defined in this environment variable, and of course not all installed packages are in those paths. The solution was to remove this environment variable from the Dockerfile.