为什么要在pybson(= bson,GitHub:py-bson)之后安装pymongo才能成功导入pybson? [英] Why do I have to install pymongo after pybson (=bson, GitHub:py-bson) to get pybson imported successfully?

查看:113
本文介绍了为什么要在pybson(= bson,GitHub:py-bson)之后安装pymongo才能成功导入pybson?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

+++


编辑:将问题发布很长时间后,我注意到这是无法使用pymongo 2.2连接到MongoDB 2.0.5数据库,表示您已经在安装pymongo之前先安装bson。我在这里不是在问这个已知的解决方案,而是在问这个所需安装顺序的原因。我要添加一个小东西,我正在安装 bson模块为 pybson,从而可以区分两个软件包的 import bson 名称冲突。


+++


我正在使用一种解决方法来避免pymongo的bson模块和bson的名称冲突(在GitHub上为py-bson)bson模块:我正在将bson软件包安装为pybson,请参见 https://github.com/py-bson/bson/issues/70


通过eve软件包的pip安装会安装bson和pymongo,这会破坏pymongo ,我们的主要思路是:


pymongo 不会带来 bson 作为依赖,它只有自己的
bson 实施。问题是 pymongo 将其 bson 安装为站点中的
a顶级目录-packages /
从而覆盖那里的任何
现有 bson


但这并不能解释为什么安装顺序[1。 bson,2。pymongo]解决了这个问题,相反,您希望它完全相反!


对于我来说,我已经安装了一个新系统,使用anaconda作为基础。我已经使用 pip install pybson 安装了bson,它说:


Traceback(最近一次调用是最近一次):


文件,第1行,在
import pybson#中与bson相同


文件 C:\ \用户\Admin\anaconda3\lib\站点包\pybson_ init _。py,第23行,来自.objectid import ObjectId


文件 C:\Users\Admin\anaconda3\lib\site-packages\pybson\objectid.py,第30行,来自bson.py3compat的
导入PY3,bytes_from_hex,string_type,text_type


ModuleNotFoundError:没有名为'bson'的模块


在安装pymongo之后除了pybson之外,使用 conda install pymongo import pybson 语句也可以工作。为什么?

解决方案

引用的问题必须反过来: pip install bson pip安装pymongo之后会干扰pymongo的bson依赖关系,以便pymongo的bson模块不再起作用。可能不是因为bson覆盖了pymongo的bson依赖关系,而是因为名称冲突:两个软件包都使用了相同的bson模块 bson,并且产生的效果与bson覆盖了pymongo的bson一样。


足够奇怪:如问题所示,您除了(py)bson外,还必须安装pymongo。

使用 pip安装bson时,请先安装pymongo的bson依赖项,然后再安装bson pip安装pymongo 会干扰pymongo的bson,请参见无法使用pymongo 2.2连接到MongoDB 2.0.5数据库,我们可以假设两个bson模块之间存在名称冲突由最后安装的那个控制。似乎(py)bson软件包除了其自己的bson模块外还需要主要的pymongo bson依赖关系,并且(py)bson在以下情况下将pymongo依赖项导入为 import bson :其内部脚本,即使它本身具有bson模块。我不知道控制是否是由覆盖引起的,或者这仅仅是python环境的问题。前者更有可能,因为安装顺序(首先是 pip install pymongo ,然后是 pip install pymongo )与 pip install pybson 而不是 pip install bson 安装bson无关,请参见通过eve包进行pip安装会安装bson和pymongo中断pymongo


+++

EDIT: Long time after putting the question online I noticed that this is a spin-off of Can't connect to MongoDB 2.0.5 database with pymongo 2.2 which says that you have to install bson before you install pymongo. I am asking here not for this already known solution, but for the reason of this needed install order. And I add a small thing, I am installing "bson" module as "pybson", which makes it possible to distinguish the import bson name clash of the two packages.

+++

I am using a workaround to avoid the name clash of pymongo's bson module and bson's (py-bson on GitHub) bson module: I am installing bson package as pybson, see https://github.com/py-bson/bson/issues/70.

From the answer at pip install of eve package installs bson and pymongo which breaks pymongo, we get the main idea:

pymongo doesn't bring bson as a dependency, it just has its own bson implementation. The problem is pymongo installs its bson as a top-level directory in site-packages/ thus overwriting any existing bson there.

But this does not explain why the install order [1. bson, 2. pymongo] solves the issue, instead you would expect it to be exactly the other way round!

In my case, I have installed a new system, using anaconda as the base. I had installed bson using pip install pybson, and it said:

Traceback (most recent call last):

File "", line 1, in import pybson # same as bson

File "C:\Users\Admin\anaconda3\lib\site-packages\pybson_init_.py", line 23, in from .objectid import ObjectId

File "C:\Users\Admin\anaconda3\lib\site-packages\pybson\objectid.py", line 30, in from bson.py3compat import PY3, bytes_from_hex, string_type, text_type

ModuleNotFoundError: No module named 'bson'

After installing pymongo in addition to pybson, using conda install pymongo, the import pybson statement worked. Why?

解决方案

The quoted idea of the question must be put the other way round: pip install bson after pip install pymongo disturbs pymongo's bson dependency so that pymongo's bson module will not work anymore. It is probably not because bson overwrites pymongo's bson dependency, but rather because of the name clash: both packages use the same bson module 'bson', and that causes the same effect as if bson overwrote pymongo's bson.

Strange enough: you have to install pymongo in addition to (py)bson, as the question states. That hints at bson using the bson dependency of pymongo in its own package.

As installing bson with pip install bson after pip install pymongo will disturb pymongo's bson, see Can't connect to MongoDB 2.0.5 database with pymongo 2.2, we can assume that there is a name clash between the two bson modules which is dominated by the one that is installed as the last. It seems as if the (py)bson package needs a dominating pymongo bson dependency in addition to its own bson module, and (py)bson imports the pymongo dependency as import bson in its internal scripts, even though it has the bson module itself. I do not know whether the domination is caused by overwriting, or whether this is just a problem of the python environment. The former is more likely, since the order of installs (first pip install bson, afterwards pip install pymongo), becomes irrelevant as soon as you install bson with pip install pybson instead of pip install bson, see pip install of eve package installs bson and pymongo which breaks pymongo.

这篇关于为什么要在pybson(= bson,GitHub:py-bson)之后安装pymongo才能成功导入pybson?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆