|
| 1 | +PyMongo 4 Migration Guide |
| 2 | +========================= |
| 3 | + |
| 4 | +.. contents:: |
| 5 | + |
| 6 | +.. testsetup:: |
| 7 | + |
| 8 | + from pymongo import MongoClient, ReadPreference |
| 9 | + client = MongoClient() |
| 10 | + collection = client.my_database.my_collection |
| 11 | + |
| 12 | +PyMongo 4.0 brings a number of improvements as well as some backward breaking |
| 13 | +changes. This guide provides a roadmap for migrating an existing application |
| 14 | +from PyMongo 3.x to 4.x or writing libraries that will work with both |
| 15 | +PyMongo 3.x and 4.x. |
| 16 | + |
| 17 | +PyMongo 3 |
| 18 | +--------- |
| 19 | + |
| 20 | +The first step in any successful migration involves upgrading to, or |
| 21 | +requiring, at least that latest version of PyMongo 3.x. If your project has a |
| 22 | +requirements.txt file, add the line "pymongo >= 3.12, < 4.0" until you have |
| 23 | +completely migrated to PyMongo 4. Most of the key new methods and options from |
| 24 | +PyMongo 4.0 are backported in PyMongo 3.12 making migration much easier. |
| 25 | + |
| 26 | +.. note:: Users of PyMongo 2.X who wish to upgrade to 4.x must first upgrade |
| 27 | + to PyMongo 3.x by following the :doc:`migrate-to-pymongo3`. |
| 28 | + |
| 29 | +Python 3.6+ |
| 30 | +----------- |
| 31 | + |
| 32 | +PyMongo 4.0 drops support for Python 2.7, 3.4, and 3.5. Users who wish to |
| 33 | +upgrade to 4.x must first upgrade to Python 3.6+. Users upgrading from |
| 34 | +Python 2 should consult the :doc:`python3`. |
| 35 | + |
| 36 | +Enable Deprecation Warnings |
| 37 | +--------------------------- |
| 38 | + |
| 39 | +:exc:`DeprecationWarning` is raised by most methods removed in PyMongo 4.0. |
| 40 | +Make sure you enable runtime warnings to see where deprecated functions and |
| 41 | +methods are being used in your application:: |
| 42 | + |
| 43 | + python -Wd <your application> |
| 44 | + |
| 45 | +Warnings can also be changed to errors:: |
| 46 | + |
| 47 | + python -Wd -Werror <your application> |
| 48 | + |
| 49 | +.. note:: Not all deprecated features raise :exc:`DeprecationWarning` when |
| 50 | + used. See `Removed features with no migration path`_. |
| 51 | + |
| 52 | +Removed features with no migration path |
| 53 | +--------------------------------------- |
0 commit comments