Working with PTVS, IronPython and MongoDB

一世执手 提交于 2019-12-02 01:31:42

Unfortunately, IronPython support for pip and setuptools is still flaky. You can try to configure pip manually by following these instructions, and then you should be able to install packages from PTVS, but not all packages will work with those older versions of setuptools.

Furthermore, many packages will not work with IronPython, period. Basically, if the package has any native code in it (.pyd files), it will not work, because IronPython does not implement the CPython extensibility API. I suspect that PyMongo will be one of those.

Is there any particular reason why you're trying to use IronPython for this? Note that PTVS fully supports regular Python.

you can try to install from source. Download source tarball, unpack and locate setup.py.

ipy.exe setup.py install

or if you prefer to keep your system location clean

ipy.exe setup.py install --user

You will very likely discover missing dependencies, which you have to resolve manually.

It looks like it may even work: http://api.mongodb.org/python/current/installation.html#installing-without-c-extensions

You may not be able to use pymongo with IronPython, but you can use the C#/.NET driver for MongoDB from IronPython.

Information on the driver is here. As explained in this link, you can install with nuget (PM> Install-Package mongocsharpdriver), or just download the dlls.

Once installed, you can use the assemblies in the normal way in IronPython:

    # Add reference to the Mongo C# driver
    import clr
    clr.AddReferenceToFileAndPath("MongoDB.Bson.dll")
    clr.AddReferenceToFileAndPath("MongoDB.Driver.dll")

Then use according to the MongoDB C# Driver API, for example:

    # Get the MongoDB database
    from MongoDB.Driver import MongoClient
    client = MongoClient("mongodb://localhost")
    server = client.GetServer()
    database = server.GetDatabase("test")

    # Get a collection
    collection = database.GetCollection("users")

    # Add a document
    from MongoDB.Bson import BsonDocument
    user = BsonDocument({'first_name':'John', 'last_name':'Smith'})
    collection.Insert(user)

See the the MongoDB C# Driver API for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!