banner



How To Install Pymongo On Windows

Introduction

If you lot'd like to connect to MongoDB for Python, you'll probably desire to apply Pymongo– the Python wrapper for MongoDB. When y'all utilise MongoDB and Python together, you can create, update and query collections with only a few simple lines of code. In this article, we'll explain how to install the Python driver for MongoDB and connect to MongoDB using Python.

Prerequisites

Before we setup the Python MongoDB client, there are a few important prerequisites that need to be in place. The organization requirements for this task include:

  • MongoDB must exist installed on the motorcar or server that is existence used to run Python scripts. You tin use the --version command in a terminal to confirm that it'south already installed:
  • Python also needs to be installed on the car or server. Python iii is the preferred version, as Python 2 is scheduled for depreciation.

Install the Python Parcel Installer ( pip3 ) for Python iii

Most current operating systems, including macOS and about Linux distributions, come with Python three; however, sometimes the PIP3 package manager is not included.

Installing the PIP3 parcel director for Python 3 on a APT distribution of Linux

On a distribution such as Ubuntu that uses the apt and apt-get repositories, the PIP3 parcel manager can be installed with the post-obit command:

ane

sudo apt install python3-pip

Installing PIP3 from source

You lot can also install PIP3 from a downloaded source using the post-obit control:

1

python3 path/to/pip-file/get-pip.py

Upgrading PIP for macOS and Linux

If you lot need to upgrade pip3 to the latest stable release on a UNIX-based operating system (such as Linux or macOS), use the command shown below:

one

sudo pip3 install -U pip3

Install the PyMongo library using Python'due south PIP package director

You lot'll need to install the MongoDB driver for Python on the machine or server where MongoDB is running.

Use the pip3 (or just pip for Python 2) bundle manager to install the MongoDB Python driver. MongoDB does have back up for Python 2.7 and even some express support for Python 2.6, but Python 2 is being deprecated and volition be losing much of its back up before 2020. Python 3 along with the PyMongo 3.10 Python driver for the MongoDB client are the recommended versions to use.

Installing the pymongo module using Python 3

Use the post-obit command to install the Python driver for MongoDB using Python 3:

Installing an older version of the PyMongo library

The post-obit command installs an older version of the PyMongo library for Python ii:

1

pip install pymongo==2.eight.1

Troubleshooting and uninstalling PyMongo

How to fix the ImportError: <span>No module named PyMongo fault

If you encounter a PyMongo version conflict, or if you need to uninstall PyMongo for some reason, simply utilise PIP's uninstall control followed by the bundle proper noun. It'south a good idea to employ sudo with this command to forestall any permissions-related issues:

1

sudo pip uninstall pymongo

Conflicts with Python's bson package

If you have trouble importing the PyMongo library, you might demand to uninstall and re-install the BSON package for Python. This tin too be achieved using PIP'south uninstall command:

1
two
three

pip uninstall bson
# reinstall the BSON library
pip install bson

Install PyMongo in the correct Python environment

If you're withal unable to import PyMongo after installing it with PIP, you can attempt installing it again a different mode. Try invoking Python directly, while using the -m option to instruct Python to run PIP as the main module.

Execute this control with sudo privileges to avoid permissions-related errors:

i

sudo python -m pip install pymongo

You can use the aforementioned command to install an older version of PyMongo in Python 2.seven:

1

sudo python -m pip install pymongo==two.8.1

Similarly, you tin can besides install PyMongo 3.8 using PIP for Python iii by executing the control in the python3 environment:

1

sudo python3 -1000 pip install pymongo==iii.8.0

Notation: As of June 2019, PyMongo version 3.viii is the latest stable release of the Python commuter.

Install PyMongo on Python two.7 with the -m option when python3 is the environment's default version of Python

Terminal output after installing PyMongo for Python 2.7

Access the MongoDB server in a Python virtual environs

At present that we've successfully setup the Python MongoDB driver, let'southward open up the IDLE virtual environs for Python by typing idle3 in the final. Yous can also go into the last's Python interpreter by typing python3 and pressing Return.

Get the version of PyMongo installed for Python

Once yous're within the Python environment, try importing the PyMongo library and getting its version using the following commands:

1
2

import pymongo
print ( "version:" , pymongo.version )

Using the IDLE environment for Python to import PyMongo and returning its version

Set upwards a development environs for the PyMongo MongoDB script

Adjacent, let's gear up a development environment for our script. To do this, y'all'll demand to create a projection directory either locally or on a server with individual-key remote SSH access, that will be used to connect Python to MongoDB.

Make a new project directory for the PyMongo Python scripts

In a UNIX-based terminal we can create a directory for our Python scripts and files using the mkdir command:

Notation: In this example, we used python-mongo for a directory name, but you can employ any proper name for this directory.

Create a new Python script for PyMongo inside the folder for the MongoDB project

Now, go into the new directory and create a new Python script using the touch command. Cull a unique name for the script, and remember that Python scripts utilise underscores ( _ ) instead of hyphens ( - ) in the file names:

i
ii

cd python-mongo
sudo bear upon my_script.py

Annotation: Using sudo privileges will restrict the file's permissions then that only the owner can write to it.

Edit the Python script used for the PyMongo customer connectedness

At this point, you lot're ready to edit your Python script. Use your favorite IDE or text editor to edit the Python script you just created in the PyMongo projection folder. If you're editing the script remotely, then use a final-based text editor like vim , gedit , or nano to edit it:

Once you lot're within the script, be certain to import all of the necessary modules and libraries. In this example, simply the MongoClient class of the PyMongo library is needed:

1

from pymongo import MongoClient

If you're using the nano text editor in a terminal window, press CTRL+O to relieve any changes, and then printing CTRL+X to go out out of nano in one case you're finished editing the script.

Connect to the MongoDB Server using the MongoClient class in Python

After you've imported the MongoClient course, y'all're free to use it to build a new client case of the Python driver. This client instance will exist used to make API calls:

1

mongo_client = MongoClient( )

Check if MongoDB is running on port 27017

The default port that the MongoDB server runs on is 27017 . If you have bug connecting to the server, use the lsof command in a terminal window to discover all processes running on that port.

UNIX terminal using the lsof command to grep processes running on port 27017 returning MongoDB processes

Admission the MongoDB customer object's HOST attribute to have information technology return connection information

One time you create a client instance of the MongoClient class, you can access its attributes to have it return more information about the connection to the MongoDB server.

The lawmaking below allows you to access the HOST attribute of the client case to get more information:

1
two

host_info = mongo_client[ 'HOST' ]
print ( "host:" , host_info)

Screenshot of a MongoDB client instance in the IDLE Python environment

Passing the domain and port parameters

There are two dissimilar ways to pass in the host domain and server'south port as parameters: They can be passed together as ane URI string parameter, or they tin can exist passed separately with the domain proper noun passed as a cord, and the port passed as a Python integer.

In this example, the host is being passed as but one string:

1
2

# one URI cord passed as a parameter:
mongo_client = MongoClient( 'mongodb://localhost:27017' )

The adjacent instance, nonetheless, uses two parameters—- a string for the domain, and an integer for the port that MongoDB is running on:

1
two

# the cord domain and integer port passed:
mongo_client = MongoClient( 'localhost' , 27017 )

Conclusion

If you'd like to connect to MongoDB using Python, the PyMongo library is a natural option. The PIP package manager makes it easy to install PyMongo and get everything ready up. Using the instructions and examples provided in this tutorial, y'all'll have no trouble installing the MongoDB Python library and creating a script that connects to MongoDB.

one
ii
3
4
5
vi
7
eight
9
10
11
12
13
fourteen
xv
16

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# import the complete PyMongo library and bank check its version
import pymongo
print ( "pymongo version:" , pymongo.version )

# import the MongoClient class
from pymongo import MongoClient

# build a new client instance for MongoDB passing
# the string domain and integer port to the host parameters
mongo_client = MongoClient( 'localhost' , 27017 )

host_info = mongo_client[ 'HOST' ]
print ( "\nhost:" , host_info)

Source: https://kb.objectrocket.com/mongo-db/how-to-install-pymongo-and-connect-to-mongodb-in-python-363

Posted by: branchcouchisem.blogspot.com

0 Response to "How To Install Pymongo On Windows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel