Using Python to operate MongoDB study notes on Fedora

1. Create a Python virtual environment

Python2 and Python3 have been installed in Fedora, and the directory of the executable file is in the /usr/bin/ directory, where the soft link is

python->python2
python2->python2.x
python3->python3.x

Next, Python3 is used to operate MongoDB, so in order not to pollute the original system environment and to isolate this specific application, a Python virtual environment is used.

Create a virtual environment

grammar

$ python3 -m venv /path/to/new/virtual/environment

Example

$ python3 -m venv py3mongodb
或者
$ python3 -m venv ~/py3mongodb

After the execution is complete, the directory of the Python virtual environment will be generated, here is py3mongodb.

Use the specified virtual environment

grammar

$ source <venv>/bin/activate

Example

$ source py3mongodb/bin/active
或者
$ source ~/py3mongodb/bin/active

At this time, the result of executing the "which python" command is ~py3mongodb/bin/python.
At this time, the result of executing the "python -V" command is Python 3.xx

Exit the current virtual environment

grammar

$ deactivate

Python's virtual environment reference:
https://docs.python.org/3/tutorial/venv.html
https://docs.python.org/3/library/venv.html#module-venv

2. Install the Python driver for MongoDB

Online installation is
recommended to use pip installation

$ python -m pip install pymongo

upgrade pymongo

$ python -m pip install --upgrade pymongo

Source code installation
prerequisite dependencies Installation
contains C extension dependencies

$ sudo yum install gcc python-devel
$ git clone git://github.com/mongodb/mongo-python-driver.git pymongo
$ cd pymongo/
$ python setup.py install

No C extension dependencies

$ python setup.py --no_ext install

For more official and specific instructions, see the official MongoDB documentation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324657306&siteId=291194637