Setting up a Python Environment with MongoDB
In order to be able to connect to MongoDB with Python, you need to install the PyMongo driver package. In Python, the best practice is to create what is known as a “virtual environment” in which to install your packages. This isolates them cleanly from any “system” packages you have installed and yields the added bonus of not requiring root privileges to install additional Python packages. The tool to create a “virtual environment” is called virtualenv.
There are two approaches to installing the virtualenv tool on your
system—manually and via your system package management tool. Most modern
UNIX-like systems will have the virtualenv tool in their package
repositories. For example, on Mac OS X with Mac Ports, you can run
sudo port install py27-virtualenv to
install virtualenv for Python 2.7. On Ubuntu you can run sudo apt-get install python-virtualenv. Refer to
the documentation for your OS to learn how to install it on your specific
platform.
In case you are unable or simply don’t want to use your system’s
package manager, you can always install it yourself, by hand. In order to
manually install it, you must have the Python setuptools package. You may
already have setuptools on your system. You can test this by running
python -c import setuptools on the
command line. If nothing is printed and you are simply returned to the
prompt, you don’t need to do anything. If an ImportError is raised, you
need to install setuptools.
To manually install ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access