Working with PTVS, IronPython and MongoDB

喜你入骨 提交于 2019-12-20 03:08:48

问题


I want to develop an applocation using the PTVS (Python Tools for Visual Studio) and i download the PTVS pluging and IronPython for Visual Studio 2012, it works perfectly.

My question here is,

Can i use MongoDB with PTVS and ItonPython?

If i can, how can i do it?

I already tried to install it by clicking on Install Python Package, but every time ask me to install pip and fails on install. Then of course the command pip install pymongo fails because pip is not installed.

Here is the error:

Installing 'pip' package manager.
Downloading setuptools from https://go.microsoft.com/fwlink/?LinkId=317603
Installing from setuptools-2.2
<string>:1: DeprecationWarning: object.__init__() takes no parameters for type KeyedRef
running install
Traceback (most recent call last):
  File "setup.py", line 202, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\Lib\distutils\core.py", line 151, in setup
  File "C:\Program Files (x86)\IronPython 2.7\Lib\distutils\dist.py", line 952, in      run_commands
  File "C:\Program Files (x86)\IronPython 2.7\Lib\distutils\dist.py", line 971, in     run_command
  File "c:\users\dservicio1\appdata\local\temp\ptvs-zwgdmk-setuptools\setuptools-    2.2\setuptools\command\install.py", line 64, in run
AttributeError: 'module' object has no attribute '_getframe'Traceback (most recent call     last):
  File "C:\Program Files (x86)\Microsoft Visual Studio     11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\pip_downloader.py",     line 61, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\Lib\subprocess.py", line 512, in     check_call
subprocess.CalledProcessError: Command '['C:\\Program Files (x86)\\IronPython     2.7\\ipy.exe', 'setup.py', 'install']' returned non-zero exit status 1
'pip' failed to install. Exit code: 1
Installing 'pymongo'
Unhandled exception:
Traceback (most recent call last):
  File "C:\Program Files (x86)\IronPython 2.7\Lib\runpy.py", line 170, in run_module
  File "C:\Program Files (x86)\IronPython 2.7\Lib\runpy.py", line 103, in     _get_module_details
ImportError: No module named pip
'pymongo' failed to install. Exit code: 1

Any suggestions?

Thanks!


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/22099096/working-with-ptvs-ironpython-and-mongodb

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