sys.path.append(r"HIDE\pyRevitEnv\lib\site-packages")
from pymongo import MongoClient
I get an error
CPython Traceback:
ImportError : cannot import name 'SON' from 'bson' (\pyRevit-Master\site-packages\bson\__init__.py)
File "\Assembly.tab\Export.panel\mongoDB.pushbutton\script.py", line 13, in <module>
from pymongo import MongoClient
File "\pyRevitEnv\lib\site-packages\pymongo\__init__.py", line 91, in <module>
from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION, has_c
File "\pyRevitEnv\lib\site-packages\pymongo\common.py", line 39, in <module>
from bson import SON
pyRevitLabs.PythonNet
at Python.Runtime.Runtime.CheckExceptionOccurred()
at Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
at Python.Runtime.PyScope.Exec(String code, PyDict locals)
at PyRevitLabs.PyRevit.Runtime.CPythonEngine.Execute(ScriptRuntime& runtime)
It appears that bson is part of pyRevit-Master, which does not include SON for import.
ChatGPT had me try uninstall and reinstall in my enve. I did and can import pymongo when in VSCode. This means it is working in the venv. Which I think narrows the issue to be a conflict with the bson in the pyRevit-Master.
ChatGPT suggested searching in vsCode “from bson” on the folder structure of the pyRevit-master to see where it is being referenced. I did this but did not see any import references. If not then Chat suggests removing from the folder.
I am unsure if that is a bad idea.
Does anyone know what in pyRevit depends on that bson package?
EDIT:
I renamed the bson folder in the pyRevitmaster and was able to load pyMongo in a script. Hence there is an issue with pyMongo in the env and the bson lib in the pyRevitmaster.
I think SON is not supported in IronPyhon (or python 2.7).
You might try to use collections.OrderedDict as an alternative, but I have not tried this myself, so it might not work.
Or you could try to write your script in CPython. (But CPython in pyRevit is not yet totally stable.)
ChatGPT is “always” trying to use Python 3, even if you are using custom GPTs like “PyRevit Helper”, but using a custom GPT gives you code that is usually more aligned to work in pyRevit, evben if you have to remind it to write IronPython compatable code.