@@ -11,215 +11,235 @@ Quick Start
11
11
:depth: 2
12
12
:class: singlecol
13
13
14
+ .. facet::
15
+ :name: genre
16
+ :values: tutorial
17
+
18
+ .. meta::
19
+ :keywords: pypi, pip, xcode
20
+
14
21
This tutorial is intended as an introduction to working with
15
22
**MongoDB** and **PyMongo**.
16
23
17
24
Download and Install
18
25
--------------------
19
26
20
- **PyMongo** is in the `Python Package Index
21
- <http://pypi.python.org/pypi/pymongo/>`_.
27
+ **PyMongo** is in the `Python Package Index.
28
+ <http://pypi.python.org/pypi/pymongo/>`__
22
29
23
- .. warning:: **Do not install the "bson" package from pypi.** PyMongo comes
24
- with its own bson package; doing "pip install bson" or "easy_install bson"
30
+ .. warning::
31
+
32
+ Do not install the ``bson`` package from PyPI. PyMongo comes
33
+ with its own ``bson`` package. Running ``pip install bson`` or ``easy_install bson``
25
34
installs a third-party package that is incompatible with PyMongo.
26
35
27
36
Installing with pip
28
37
-------------------
29
38
30
- We recommend using `pip <http://pypi.python.org/pypi/pip>`_
31
- to install pymongo on all platforms:
39
+ We recommend using `pip <http://pypi.python.org/pypi/pip>`__
40
+ to install PyMongo on all platforms:
32
41
33
42
.. code-block:: python
34
43
35
- $ python3 -m pip install pymongo
44
+ $ python3 -m pip install pymongo
36
45
37
- To get a specific version of pymongo :
46
+ To get a specific version of PyMongo :
38
47
39
48
.. code-block:: python
40
49
41
- $ python3 -m pip install pymongo==3.5.1
50
+ $ python3 -m pip install pymongo==3.5.1
42
51
43
- To upgrade using pip:
52
+ To upgrade by using pip:
44
53
45
54
.. code-block:: python
46
55
47
- $ python3 -m pip install --upgrade pymongo
56
+ $ python3 -m pip install --upgrade pymongo
48
57
49
58
Dependencies
50
59
------------
51
60
52
61
PyMongo supports CPython 3.7+ and PyPy3.7+.
53
62
54
- Required dependencies
55
- .....................
56
-
57
- Support for mongodb+srv:// URIs requires `dnspython
58
- <https://pypi.python.org/pypi/dnspython>`_
63
+ Support for mongodb+srv:// URIs requires `dnspython.
64
+ <https://pypi.python.org/pypi/dnspython>`__
59
65
60
- Installing from source
66
+ Installing from Source
61
67
----------------------
62
68
63
- If you'd rather install directly from the source (i.e. to stay on the
64
- bleeding edge), install the C extension dependencies then check out the
69
+ If you'd rather install PyMongo directly from the source,
70
+ install the C- extension dependencies, and then check out the
65
71
latest source from GitHub and install the driver from the resulting tree:
66
72
67
73
.. code-block:: python
68
74
69
- $ git clone https://github.com/mongodb/mongo-python-driver.git pymongo
70
- $ cd pymongo/
71
- $ pip install .
75
+ $ git clone https://github.com/mongodb/mongo-python-driver.git pymongo
76
+ $ cd pymongo/
77
+ $ pip install .
72
78
73
- Installing from source on Unix
74
- ..............................
79
+ Installing from Source on Unix
80
+ ``````````````````````````````
75
81
76
- To build the optional C extensions on Linux or another non-macOS Unix you must
77
- have the GNU C compiler (gcc) installed. Depending on your flavor of Unix
78
- (or Linux distribution) you may also need a python development package that
82
+ To build the optional C extensions on Linux or another non-macOS Unix, you must
83
+ have the GNU C compiler (`` gcc`` ) installed. Depending on your flavor of Unix
84
+ (or Linux distribution), you may also need a Python development package that
79
85
provides the necessary header files for your version of Python. The package
80
- name may vary from distro to distro .
86
+ name might vary among distrubutions .
81
87
82
- Debian and Ubuntu users should issue the following command:
88
+ If you're using Debian or Ubuntu, run the following command:
83
89
84
90
.. code-block:: python
85
91
86
- $ sudo apt-get install build-essential python-dev
87
-
88
- Users of Red Hat based distributions (RHEL, CentOS, Amazon Linux, Oracle Linux,
89
- Fedora, etc.) should issue the following command:
90
-
91
- .. code-block:: python
92
-
93
- $ sudo yum install gcc python-devel
94
-
95
- Installing from source on macOS / OSX
96
- .....................................
92
+ $ sudo apt-get install build-essential python-dev
97
93
98
- If you want to install PyMongo with C extensions from source you will need
99
- the command line developer tools. On modern versions of macOS they can be
100
- installed by running the following in Terminal (found in
101
- /Applications/Utilities/):
94
+ If you're using a Red Hat-based distribution (RHEL, CentOS, Amazon Linux, Oracle Linux,
95
+ Fedora, etc.), run the following command:
102
96
103
97
.. code-block:: python
104
98
105
- xcode-select -- install
99
+ $ sudo yum install gcc python-devel
106
100
107
- For older versions of OSX you may need Xcode. See the notes below for various
108
- OSX and Xcode versions.
101
+ Installing from Source on macOS or OSX
102
+ ``````````````````````````````````````
109
103
110
- **Snow Leopard (10.6)** - Xcode 3 with 'UNIX Development Support'.
104
+ If you want to install PyMongo with C extensions from source, you will need
105
+ the command line developer tools. On modern versions of macOS, you can
106
+ install these tools by running the following command in your terminal:
111
107
112
- **Snow Leopard Xcode 4**: The Python versions shipped with OSX 10.6.x
113
- are universal binaries. They support i386, PPC, and x86_64. Xcode 4 removed
114
- support for PPC, causing the distutils version shipped with Apple's builds of
115
- Python to fail to build the C extensions if you have Xcode 4 installed. There
116
- is a workaround:
108
+ .. code-block:: bash
117
109
118
- .. code-block:: python
110
+ xcode-select --install
119
111
120
- # For some Python builds from python.org
121
- $ env ARCHFLAGS='-arch i386 -arch x86_64' python -m easy_install pymongo
112
+ The following list describes the installation requirements for older versions of OSX:
122
113
123
- See `http://bugs.python.org/issue11623 <http://bugs.python.org/issue11623>`_
124
- for a more detailed explanation.
114
+ - **Lion (10.7) and newer**: PyMongo's C extensions can be built against
115
+ versions of Python 3.7+ downloaded from python.org. In all cases, you must install Xcode
116
+ with "UNIX Development Support".
117
+
118
+ Starting with Xcode version 5.1, ``clang``
119
+ throws an error when it encounters compiler flags that it doesn't recognize.
120
+ This may cause C extension builds to fail with an error similar to the following:
125
121
126
- **Lion (10.7) and newer** - PyMongo's C extensions can be built against
127
- versions of Python 3.7+ downloaded from python.org. In all cases Xcode must be
128
- installed with 'UNIX Development Support'.
122
+ .. code-block:: bash
129
123
130
- **Xcode 5.1**: Starting with version 5.1 the version of clang that ships with
131
- Xcode throws an error when it encounters compiler flags it doesn't recognize.
132
- This may cause C extension builds to fail with an error similar to:
124
+ clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
133
125
134
- .. code-block:: python
126
+ You can run one of the following commands as a workaround:
135
127
136
- clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
128
+ .. code-block:: python
137
129
138
- There are workarounds:
130
+ # Apple specified workaround for Xcode 5.1
131
+ # easy_install
132
+ $ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install pymongo
133
+ # or pip
134
+ $ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install pymongo
139
135
140
- .. code-block:: python
136
+ # Alternative workaround using CFLAGS
137
+ # easy_install
138
+ $ CFLAGS=-Qunused-arguments easy_install pymongo
139
+ # or pip
140
+ $ CFLAGS=-Qunused-arguments pip install pymongo
141
141
142
- # Apple specified workaround for Xcode 5.1
143
- # easy_install
144
- $ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install pymongo
145
- # or pip
146
- $ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install pymongo
142
+ - **Snow Leopard (10.6)**: Xcode 3 with "UNIX Development Support".
147
143
148
- # Alternative workaround using CFLAGS
149
- # easy_install
150
- $ CFLAGS=-Qunused-arguments easy_install pymongo
151
- # or pip
152
- $ CFLAGS=-Qunused-arguments pip install pymongo
144
+ The Python versions that shipped with OSX 10.6.x
145
+ are universal binaries. They support i386, PPC, and x86_64 architectures. Xcode 4 removed
146
+ support for PPC, causing the ``distutils`` version shipped with Apple's builds of
147
+ Python to fail to build the C extensions if you have Xcode 4 installed. There
148
+ is a workaround:
153
149
150
+ .. code-block:: python
154
151
155
- Installing from source on Windows
156
- .................................
152
+ # For some Python builds from python.org
153
+ $ env ARCHFLAGS='-arch i386 -arch x86_64' python -m easy_install pymongo
157
154
158
- If you want to install PyMongo with C extensions from source the following
159
- requirements apply to both CPython and ActiveState's ActivePython:
155
+ See `http://bugs.python.org/issue11623 <http://bugs.python.org/issue11623>`__
156
+ for a more detailed explanation.
160
157
161
- Windows
162
- ~~~~~~~
158
+ Installing from Source on Windows
159
+ `````````````````````````````````
163
160
164
- Install Visual Studio 2015+.
161
+ If you want to install PyMongo with C extensions from source,
162
+ CPython and ActiveState's ActivePython require Visual Studio 2015 or later.
165
163
166
164
.. _install-no-c:
167
165
168
166
Installing Without C Extensions
169
167
-------------------------------
170
168
171
- By default, the driver attempts to build and install optional C
169
+ By default, PyMongo attempts to build and install optional C
172
170
extensions (used for increasing performance) when it is installed. If
173
- any extension fails to build the driver will be installed anyway but a
171
+ any extension fails to build, the driver will still be installed, but a
174
172
warning will be printed.
175
173
176
- If you wish to install PyMongo without the C extensions, even if the
177
- extensions build properly, it can be done using a command line option to
178
- *pip install*:
174
+ If you wish to install PyMongo without the C extensions, run the following command:
179
175
180
176
.. code-block:: python
181
177
182
- $ NO_EXT=1 python -m pip install .
178
+ $ NO_EXT=1 python -m pip install .
179
+
180
+ Installing a Beta or Release-Candidate Version
181
+ ----------------------------------------------
183
182
184
- Installing a beta or release candidate
185
- --------------------------------------
183
+ MongoDB will occasionally tag a beta or release-candidate version for testing by
184
+ the community before final release. These releases will not be uploaded to PyPI, but you
185
+ can find them on the `GitHub tags page <https://github.com/mongodb/mongo-python-driver/tags>`__
186
+ for PyMongo.
186
187
187
- MongoDB, Inc. may occasionally tag a beta or release candidate for testing by
188
- the community before final release. These releases will not be uploaded to pypi
189
- but can be found on the
190
- `GitHub tags page <https://github.com/mongodb/mongo-python-driver/tags>`_.
191
- They can be installed by passing the full URL for the tag to pip:
188
+ To install one of these versions, pass the full URL for the version as an argument pip,
189
+ as follows:
192
190
193
191
.. code-block:: python
194
192
195
- $ python3 -m pip install https://github.com/mongodb/mongo-python-driver/archive/4.4.0b0.tar.gz
193
+ $ python3 -m pip install https://github.com/mongodb/mongo-python-driver/archive/4.4.0b0.tar.gz
196
194
197
195
Test Your PyMongo Installation
198
196
------------------------------
199
197
200
- In the Python shell, the following should run without raising an exception:
198
+ In the Python shell, the following command should run without raising an exception:
201
199
202
200
.. code-block:: python
203
201
204
- >>> import pymongo
202
+ >>> import pymongo
205
203
206
204
Create a Deployment
207
205
-------------------
208
206
209
- This tutorial assumes that a MongoDB instance is running on the
210
- default host and port. Assuming you have `downloaded and installed
211
- <https://www.mongodb.com/docs/manual/installation/>`_ MongoDB, you
212
- can start it like so:
207
+ Follow the `Install MongoDB
208
+ <https://www.mongodb.com/docs/manual/installation/>`__ guide to install MongoDB locally.
209
+
210
+ Create a Python Application
211
+ ---------------------------
212
+
213
+ In your shell, run the following command to create a directory for your application:
213
214
214
215
.. code-block:: bash
215
216
216
- $ mongod
217
+ mkdir python_quickstart
217
218
218
- To test your code :
219
+ Run the following command to navigate into the project directory :
219
220
220
- .. code-block:: python
221
+ .. code-block:: bash
222
+
223
+ cd python_quickstart
224
+
225
+ Create a file named ``quickstart.py`` to contain your application, and then copy
226
+ the following code into ``quickstart.py``:
227
+
228
+ .. code-block:: python
229
+
230
+ from pymongo import MongoClient
231
+
232
+ client = MongoClient()
233
+ client.drop_database("test-database")
234
+
235
+ In the terminal, run the following command to start your application:
236
+
237
+ .. code-block:: bash
238
+
239
+ python3 quickstart.py
221
240
222
- from pymongo import MongoClient
241
+ .. note::
223
242
224
- client = MongoClient()
225
- client.drop_database("test-database")
243
+ This application has no output, but should return you to the command prompt if
244
+ successful.
245
+
0 commit comments