Python 3 FAQ

What Python 3 versions are supported?

PyMongo supports CPython 3.4+ and PyPy3.5+.

Are there any PyMongo behavior changes with Python 3?

Only one intentional change. Instances of bytes are encoded as BSON type 5 (Binary data) with subtype 0. In Python 3 they are decoded back to bytes. In Python 2 they are decoded to Binary with subtype 0.

For example, let’s insert a bytes instance using Python 3 then read it back. Notice the byte string is decoded back to bytes:

  1. Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
  2. [GCC 4.9.3] on linux
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import pymongo
  5. >>> c = pymongo.MongoClient()
  6. >>> c.test.bintest.insert_one({'binary': b'this is a byte string'}).inserted_id
  7. ObjectId('4f9086b1fba5222021000000')
  8. >>> c.test.bintest.find_one()
  9. {'binary': b'this is a byte string', '_id': ObjectId('4f9086b1fba5222021000000')}

Now retrieve the same document in Python 2. Notice the byte string is decoded to Binary:

  1. Python 2.7.6 (default, Feb 26 2014, 10:36:22)
  2. [GCC 4.7.3] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import pymongo
  5. >>> c = pymongo.MongoClient()
  6. >>> c.test.bintest.find_one()
  7. {u'binary': Binary('this is a byte string', 0), u'_id': ObjectId('4f9086b1fba5222021000000')}

There is a similar change in behavior in parsing JSON binary with subtype 0. In Python 3 they are decoded into bytes. In Python 2 they are decoded to Binary with subtype 0.

For example, let’s decode a JSON binary subtype 0 using Python 3. Notice the byte string is decoded to bytes:

  1. Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
  2. [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> from bson.json_util import loads
  5. >>> loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}')
  6. {'b': b'this is a byte string'}

Now decode the same JSON in Python 2 . Notice the byte string is decoded to Binary:

  1. Python 2.7.10 (default, Feb 7 2017, 00:08:15)
  2. [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> from bson.json_util import loads
  5. >>> loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}')
  6. {u'b': Binary('this is a byte string', 0)}

Why can’t I share pickled ObjectIds between some versions of Python 2 and 3?

Instances of ObjectId pickled using Python 2 can always be unpickled using Python 3.

If you pickled an ObjectId using Python 2 and want to unpickle it using Python 3 you must pass encoding='latin-1' to pickle.loads:

  1. Python 2.7.6 (default, Feb 26 2014, 10:36:22)
  2. [GCC 4.7.3] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import pickle
  5. >>> from bson.objectid import ObjectId
  6. >>> oid = ObjectId()
  7. >>> oid
  8. ObjectId('4f919ba2fba5225b84000000')
  9. >>> pickle.dumps(oid)
  10. 'ccopy_reg\n_reconstructor\np0\n(cbson.objectid\...'
  11. Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
  12. [GCC 4.9.3] on linux
  13. Type "help", "copyright", "credits" or "license" for more information.
  14. >>> import pickle
  15. >>> pickle.loads(b'ccopy_reg\n_reconstructor\np0\n(cbson.objectid\...', encoding='latin-1')
  16. ObjectId('4f919ba2fba5225b84000000')

If you need to pickle ObjectIds using Python 3 and unpickle them using Python 2 you must use protocol <= 2:

  1. Python 3.6.5 (default, Jun 21 2018, 15:09:09)
  2. [GCC 7.3.0] on linux
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import pickle
  5. >>> from bson.objectid import ObjectId
  6. >>> oid = ObjectId()
  7. >>> oid
  8. ObjectId('4f96f20c430ee6bd06000000')
  9. >>> pickle.dumps(oid, protocol=2)
  10. b'\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...'
  11. Python 2.7.15 (default, Jun 21 2018, 15:00:48)
  12. [GCC 7.3.0] on linux2
  13. Type "help", "copyright", "credits" or "license" for more information.
  14. >>> import pickle
  15. >>> pickle.loads('\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...')
  16. ObjectId('4f96f20c430ee6bd06000000')

Table of Contents

Previous topic

Changelog

Next topic

PyMongo 3 Migration Guide

This Page

Quick search