Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'abc' #82

Closed
yangzhao5566 opened this issue May 7, 2018 · 22 comments
Closed

ImportError: cannot import name 'abc' #82

yangzhao5566 opened this issue May 7, 2018 · 22 comments
Labels

Comments

@yangzhao5566
Copy link

yangzhao5566 commented May 7, 2018

from bson.py3compat import abc, string_type, PY3, text_type
ImportError: cannot import name 'abc'

@ppython
Copy link

ppython commented May 15, 2018

This is frustrating as there is no more tags(versions) on github to easily track the problem.

I'm also trying to revert to 0.5.2, and I get

bson/setup.py", line 8, in < module >
from pip import get_installed_distributions
ImportError: cannot import name get_installed_distributions

which is related to a bad use of pip (pypa/pip#5243)

If by any chance you happen to have this error with pymongo; try this https://stackoverflow.com/a/46166075

@Parkayun Parkayun added the bug label May 21, 2018
@JinXJinX
Copy link

uninstall bson, then install pymongo will solve this.

pip uninstall bson
pip install pymongo

I think another bson package is what you are looking for. https://github.com/mongodb/mongo-python-driver

@Affan-Ali
Copy link

Affan-Ali commented Oct 30, 2018

uninstall bson and pymongo and reinstall pymongo using conda:

pip uninstall bson
pip uninstall pymongo
conda install -c anaconda pymongo 

@shlomiLan
Copy link

shlomiLan commented Nov 20, 2018

I had to add pymongo==3.7.2 to my requirements file, with pymongo==3.7.1 I got this same error.

Update: still not working properly. My app runs on Heroku and only there I see this issue, can't reproduce on my local machine or on Travis-CI

@Parkayun
Copy link
Member

It seems that this issue is raising when installed py-bson and pymongo both.
But, this package designed with independent package (don't use pymongo togehter).

If anyone encounter this issue whithout pymongo, please comment it with how to reproduce(environments and more).

Thank you.

@shlomiLan
Copy link

shlomiLan commented Nov 21, 2018

I'm not using them both, I tried to install pymingo to fix this bug.
You can see here in which package s I use https://github.com/shlomiLan/slots_tracker_server/blob/master/requirements.txt

The problem is that I'm not getting this error on my local machine (Mac) or in the CI (Travis), only on heroku.
Let me know if there is any more information that I can give you.

Update: I have posted a question on StackOverFlow with more info

@hbrannan
Copy link

There seems to remain a lack of clarity, so, from the most useful SO post I found on this issue from @bauman.space:

pymongo>=2.7.1 directive overwrites your bson installation

from https://github.com/mongodb/mongo-python-driver

Do not install the "bson" package from pypi. PyMongo comes with its own bson package; doing easy_install bson or pip install bson installs a third-party package that is incompatible with PyMongo.

assuming you really want to use the 3rd party bson, you'll need to

pip uninstall bson (removes the overwritten bson that landed via pymongo package) pip install bson (reinstalls the py-bson package) or you could patch the pymongo install to remove the collections.abc line (evidently you don't really need it?), but messing with production packaged code is questionable.

pymongo module https://github.com/mongodb/mongo-python-driver/blob/master/bson/py3compat.py#L22-L25

py-bson module you are trying to use. https://github.com/py-bson/bson/blob/master/bson/py3compat.py#L22-L24

@shlomiLan
Copy link

There seems to remain a lack of clarity, so, from the most useful SO post I found on this issue from @bauman.space:

pymongo>=2.7.1 directive overwrites your bson installation

from https://github.com/mongodb/mongo-python-driver

Do not install the "bson" package from pypi. PyMongo comes with its own bson package; doing easy_install bson or pip install bson installs a third-party package that is incompatible with PyMongo.

assuming you really want to use the 3rd party bson, you'll need to

pip uninstall bson (removes the overwritten bson that landed via pymongo package) pip install bson (reinstalls the py-bson package) or you could patch the pymongo install to remove the collections.abc line (evidently you don't really need it?), but messing with production packaged code is questionable.

pymongo module https://github.com/mongodb/mongo-python-driver/blob/master/bson/py3compat.py#L22-L25

py-bson module you are trying to use. https://github.com/py-bson/bson/blob/master/bson/py3compat.py#L22-L24

I posted that question, you can see it in the previous comment :)
Now I get a different but similar error:

    Traceback (most recent call last):
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
         worker.init_process()
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
         self.load_wsgi()
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
         self.wsgi = self.app.wsgi()
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
         self.callable = self.load()
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
         return self.load_wsgiapp()
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
         return util.import_app(self.app_uri)
       File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
         __import__(module)
       File "/app/slots_tracker_server/__init__.py", line 5, in <module>
         from mongoengine import connect
       File "/app/.heroku/python/lib/python3.6/site-packages/mongoengine/__init__.py", line 2, in <module>
         from mongoengine import connection
       File "/app/.heroku/python/lib/python3.6/site-packages/mongoengine/connection.py", line 1, in <module>
         from pymongo import MongoClient, ReadPreference, uri_parser
       File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/__init__.py", line 77, in <module>
         from pymongo.collection import ReturnDocument
       File "/app/.heroku/python/lib/python3.6/site-packages/pymongo/collection.py", line 20, in <module>
         from bson.code import Code
       File "/app/.heroku/python/lib/python3.6/site-packages/bson/code.py", line 18, in <module>
         from bson.py3compat import abc, string_type, PY3, text_type
     ModuleNotFoundError: No module named 'bson.py3compat'

@hbrannan
Copy link

hbrannan commented Nov 27, 2018

Haha, yes, see that now. Well, thanks for posting.

I believe your pymongo file may remain corrupted/ partially overwritten by the earlier bson install. I had this same issue, and per the thread, attempted a pymongo reinstall to resolve.

My virtual env, however, seems to have a bit of an issue with updating, so reinstalling pymongo did not resolve the issue.

I deleted the generated .venv repo, and ran a blanket install. Now the issues are resolved for me.
I am using pipenv & had to create an empty .venv folder for that kind of reinstall to work. From comments online, it appears that people using virtualenv, at least, are having similar issues.

So for me,
pipenv uninstall bson
rm -rf .venv
mkdir .venv
pipenv install

@shlomiLan
Copy link

I'm running on Heroku so I can't control how the environment is been set up. I run heroku repo:purge_cache -a APP_NAME and redeploy the app from Git and now it seems to be working.

@zyl916369
Copy link

uninstall bson and pymongo and reinstall pymongo using conda:

pip uninstall bson
pip uninstall pymongo
conda install -c anaconda pymongo 

Thanks , solved the issue

@jesse-homes
Copy link

I have Python 3.7.0 running in a virtual environment on MacBook Pro Mojave 10.14.3
I had installed PyMongo before BSON, then doing geo 2d stuff needed BSON. Added BSON using pypl pip and would not work.
Uninstalled PyMongo and BSON
Then installed BSON [Successfully installed bson-0.5.7]
Then installed PyMongo [Successfully installed pymongo-3.7.2]
Now it works fine.

@lionandbull
Copy link

I had to add pymongo==3.7.2 to my requirements file, with pymongo==3.7.1 I got this same error.

Update: still not working properly. My app runs on Heroku and only there I see this issue, can't reproduce on my local machine or on Travis-CI

It works for me.

@JosephP91
Copy link

I have Python 3.7.0 running in a virtual environment on MacBook Pro Mojave 10.14.3
I had installed PyMongo before BSON, then doing geo 2d stuff needed BSON. Added BSON using pypl pip and would not work.
Uninstalled PyMongo and BSON
Then installed BSON [Successfully installed bson-0.5.7]
Then installed PyMongo [Successfully installed pymongo-3.7.2]
Now it works fine.

Confirmed. If you install bson first and then pymongo, it works.

LarsMichelsen added a commit to Checkmk/checkmk that referenced this issue Jul 26, 2019
This dependency is conflicting with pymongo and resulting in the
following exception:

----
ImportError while importing test module '/var/lib/jenkins/workspace/cmk_master/unit_tests/check_mk/tests/unit/agents/plugins/test_mk_mongodb.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/_pytest/python.py:507: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/py/_path/local.py:701: in pyimport
    __import__(modname)
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/_pytest/assertion/rewrite.py:304: in load_module
    exec(co, mod.__dict__)
unit/agents/plugins/test_mk_mongodb.py:5: in <module>
    from bson.json_util import loads
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/bson/json_util.py:116: in <module>
    from pymongo.errors import ConfigurationError
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/pymongo/__init__.py:77: in <module>
    from pymongo.collection import ReturnDocument
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/pymongo/collection.py:20: in <module>
    from bson.code import Code
../virtual-envs/2.7/.venv/local/lib/python2.7/site-packages/bson/code.py:18: in <module>
    from bson.py3compat import abc, string_type, PY3, text_type
E   ImportError: cannot import name abc
---

According to py-bson/bson#82 both
packages are conflicting and bson needs to be cleaned up.

Change-Id: I82383157176ba3230408285c142d7fc92cedccae
@dparkar
Copy link

dparkar commented Oct 16, 2019

Coming across the same thing today. Any suggestions on how to detect the root cause ?

@samhavens
Copy link

@dparkar the following (based on what worked for others in this thread) worked for me:

pip uninstall -y bson pymongo \
&& pip install bson==0.5.7 \
&& pip install pymongo==3.7.2

@sadakmed
Copy link

sadakmed commented Mar 6, 2020

@Affan-Ali this guy is great

pip uninstall bson
pip uninstall pymongo
conda install -c anaconda pymongo 

This solve it, is better to install with conda than pip if u r using anaconda

@ilfrich
Copy link

ilfrich commented Apr 20, 2020

TLDR; remove bson from your requirements.txt, as pymongo already provides it and check that none of your other dependencies include bson. Always put pymongo AFTER anything else in the requirements.txt.


The problem appears to be that pymongo already delivers the bson module and relies on it's own module: https://github.com/mongodb/mongo-python-driver/tree/master/bson

This is the problem with pip, where 2 packages can deliver the same module name.

The issue can be caused by the following 2 scenarios:

  • Scenario 1: You use pymongo and bson in the same requirements.txt file
  • Scenario 2: You use pymongo and a package that depends on bson

Steps to reproduce require the following 3 files:

  • Dockerfile
FROM python:3.6
COPY requirements.txt .
COPY test.py .
RUN pip install -r requirements.txt
RUN python3 test.py
  • requirements.txt
pymongo
bson
  • test.py
print("\n\nTest successful\n===============\n")

Then you simply run docker build -t bson-test-image --no-cache .

Weirdly, if you have a transitive dependency to bson (e.g. you depend on X and X depends on bson), it works ~50% of the time (I ran the build 10x and it failed half of the time). Weird race condition.

In summary: this repository should only be used, if you ONLY need bson functionality and don't want to use MongoDB. Otherwise omit it entirely as you can import bson if you have pymongo installed.

@exaland
Copy link

exaland commented Jun 7, 2020

I have same problem

I resolved with uninstall with pip and install with pip3

pip uninstall bson
pip uninstall pymongo

pip3 install bson
pip3 install pymongo

@Parkayun
Copy link
Member

Parkayun commented Jun 7, 2020

guys, I uploaded pybson to pypi for looking on demands. (same package with bson.)
pip install pybson
#41

@satendrasingh51
Copy link

This worked for me:

pip uninstall bson
pip uninstall pymongo
pip install pymongo

@Joshuah143
Copy link

Worth noting that I experienced the same issue today running the latest versions of each package. Solved by removing bson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests