Tring to Deploying my Python function app Getting Error No module named 'bson'
ModuleNotFoundError: No module named 'bson'
from bson.json_util import dumps
The app is working fine locally and producing the correct output, but it is unable to deploy to Azure Function App by GitHub CI/CD. Getting message on GitHub successfully deployed but getting error on Application Insights log and function are not working.
Community Center Not monitored
-
Shireesha Eeraboina • 3,435 Reputation points • Microsoft External Staff • Moderator
2025-02-21T05:50:52.1233333+00:00 Hi @Keshav Kori ,
Welcome to Microsoft Q&A Platform!
It seems like you are encountering an issue with deploying your app to Azure Function App using GitHub CI/CD. The error message "ModuleNotFoundError: No module named 'bson'" indicates that the 'bson' module is missing in the deployment environment.
To resolve this issue, you can try the following steps:
- Ensure that the 'bson' module is included in your requirements.txt file. You can add it by running the following command in your terminal:
pip install pymongo
- Make sure that the 'bson' module is installed in the deployment environment. You can include the 'bson' module in your requirements.txt file and ensure that it gets installed during the deployment process.
- Check if the 'bson' module is included in the Azure Function App's Python environment. You may need to manually install the 'bson' module in the Azure Function App environment.
For more detailed guidance, refer to the official Azure documentation on deploying Python apps with dependencies:
- Develop and deploy Python Azure Functions using Visual Studio Code
- Azure Functions Python developer guide
By following these steps, you should be able to resolve the issue with the missing
bson
module in your Azure Function App environment. -
keshav kori • 0 Reputation points
2025-02-21T06:48:10.6733333+00:00 Hello Shireesha Eeraboina,
I appreciate the quick response. However,pymongo and bson
is already inrequirements.txt
, and the GitHub pipeline successfully installs it during the build -
Shireesha Eeraboina • 3,435 Reputation points • Microsoft External Staff • Moderator
2025-02-24T06:14:29.04+00:00 Hello @Keshav Kori ,
Thank you for the update. Since
pymongo
andbson
are already included in yourrequirements.txt
and are being installed during the build, please consider the following:- Ensure that the deployment environment aligns with your local environment.
- Check the logs for any discrepancies that may have occurred during the deployment process.
- Confirm that the correct Python version is being utilized in the Azure Function App.
If the issue continues, please provide the specific error logs for further assistance.
-
keshav kori • 0 Reputation points
2025-02-24T13:46:04.5033333+00:00 - Ensure that the deployment environment aligns with your local environment. As I can check both environments are the same.
- Check the logs for any discrepancies that may have occurred during the deployment process.
Getting successfully installs it during the build
Collecting pymongo==4.6.1 (from -r requirements.txt (line 8))
Downloading pymongo-4.6.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (22 kB)
- Confirm that the correct Python version is being utilized in the Azure Function App. I have used the same python for both local as well as Azure Function Python-10
This time we have removed individual
bson
install.Azure Function log errors
timestamp [UTC] message 2/24/2025, 1:28:28.302 PM Loading functions metadata 2/24/2025, 1:28:28.302 PM Reading functions metadata (Custom) 2/24/2025, 1:28:28.302 PM 0 functions found (Custom) 2/24/2025, 1:28:28.302 PM 0 functions loaded 2/24/2025, 1:28:28.303 PM Loading functions metadata 2/24/2025, 1:28:28.303 PM Reading functions metadata (Custom) 2/24/2025, 1:28:28.303 PM 0 functions found (Custom) 2/24/2025, 1:28:28.303 PM 0 functions loaded 2/24/2025, 1:28:00.055 PM Initializing Warmup Extension. 2/24/2025, 1:28:00.063 PM Initializing Host. OperationId: ''. 2/24/2025, 1:28:00.063 PM Host initialization: ConsecutiveErrors=0, StartupCount=3, OperationId= 2/24/2025, 1:28:00.068 PM Traceback (most recent call last): 2/24/2025, 1:28:00.068 PM File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 44, in call 2/24/2025, 1:28:00.068 PM return func(*args, **kwargs) 2/24/2025, 1:28:00.068 PM File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/loader.py", line 244, in index_function_app 2/24/2025, 1:28:00.068 PM imported_module = importlib.import_module(module_name) 2/24/2025, 1:28:00.068 PM File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module 2/24/2025, 1:28:00.068 PM return _bootstrap._gcd_import(name[level:], package, level) 2/24/2025, 1:28:00.068 PM File "<frozen importlib._bootstrap>", line 1050, in _gcd_import 2/24/2025, 1:28:00.068 PM File "<frozen importlib._bootstrap>", line 1027, in _find_and_load 2/24/2025, 1:28:00.068 PM File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked 2/24/2025, 1:28:00.068 PM File "<frozen importlib._bootstrap>", line 688, in _load_unlocked 2/24/2025, 1:28:00.068 PM File "<frozen importlib._bootstrap_external>", line 883, in exec_module 2/24/2025, 1:28:00.068 PM File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed 2/24/2025, 1:28:00.068 PM File "/home/site/wwwroot/function_app.py", line 4, in <module> 2/24/2025, 1:28:00.068 PM import MA 2/24/2025, 1:28:00.068 PM File "/home/site/wwwroot/MA.py", line 5, in <module> 2/24/2025, 1:28:00.068 PM from bson.json_util import dumps 2/24/2025, 1:28:00.068 PM ModuleNotFoundError: No module named 'bson'
-
Shireesha Eeraboina • 3,435 Reputation points • Microsoft External Staff • Moderator
2025-03-03T10:49:38.29+00:00 Hi @Keshav Kori ,
I apologize for the delay in my response and thank you for your understanding.
The error "No module named 'bson'" usually means that the bson module isn't installed in your Azure Function environment. Since you removed the separate bson installation, make sure that pymongo, which includes bson, is properly listed in your requirements.txt file.
After updating requirements.txt to include pymongo, be sure to redeploy your function app. Also, check that the deployment process installs all dependencies correctly without any errors.
To help you better understand, kindly refer to the documentation below:
- Troubleshoot Python errors in Azure Functions (python-mode-configuration)
- Troubleshoot Python errors in Azure Functions (python-mode-decorators)
I hope this answers your query! Let me know if you require any additional help or clarification.
-
Shireesha Eeraboina • 3,435 Reputation points • Microsoft External Staff • Moderator
2025-03-03T10:50:28.9466667+00:00 Removing Duplicate.
-
Shireesha Eeraboina • 3,435 Reputation points • Microsoft External Staff • Moderator
2025-03-04T09:52:13+00:00 Hi @Keshav Kori ,
Just checking in to see if the information above was helpful. If you have any further updates on this issue, please feel free to post them here.
-
keshav kori • 0 Reputation points
2025-03-04T10:38:57.28+00:00 Hi, Shireesha Eeraboina
Thanks for your reply.
I ran a very basic test: addedpymongo
and a simple import (from pymongo import MongoClient
) to myfunction_app.py
file. Surprisingly, I'm still getting an error related topymongo
. It's quite puzzling.ModuleNotFoundError: No module named 'pymongo'from pymongo import MongoClient
GitHub CI/CD build log details.
Run pip install -r requirements.txt Collecting azure-functions (from -r requirements.txt (line 5)) Downloading azure_functions-1.21.3-py3-none-any.whl.metadata (6.8 kB) Collecting pymongo (from -r requirements.txt (line 6)) Downloading pymongo-4.11.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (22 kB) Collecting dnspython<3.0.0,>=1.16.0 (from pymongo->-r requirements.txt (line 6)) Downloading dnspython-2.7.0-py3-none-any.whl.metadata (5.8 kB) Downloading azure_functions-1.21.3-py3-none-any.whl (185 kB) Downloading pymongo-4.11.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 113.3 MB/s eta 0:00:00 Downloading dnspython-2.7.0-py3-none-any.whl (313 kB) Installing collected packages: azure-functions, dnspython, pymongo Successfully installed azure-functions-1.21.3 dnspython-2.7.0 pymongo-4.11.2
-
Ranashekar Guda • 2,670 Reputation points • Microsoft External Staff • Moderator
2025-03-05T09:39:16.6233333+00:00 Hi @keshav kori,
Your question is more inclined towards Azure devops. Unfortunately. Azure devops is not supported in Microsoft Q&A I would suggest you to raise a question/ report your problem here in devops community here Developer Community
Sign in to comment