Code

Set Up Python, MongoDb Environment on MacOS Catalina

April 30, 2020
python-mongodb

Hello, world! In this tutorial, I will show you how to set up Python and MongoDb environment on MacOS Catalina. With some shell changes in Catalina, I had a hard time figuring out how to make my own environment of Python and MongoDb. I had to open so many tabs, and learn the hard way.

So, here’s a short guide for you to help you set up your own environment without much trouble. While this guide is specific to Catalina MacOS, very similar concepts should apply to other operating systems.

Install Homebrew

Firstly, you’ll need to install homebrew. It is a utility that lets you install programs from the terminal on Mac OS and it’s what we will use to install the rest of the software we need. Open up terminal and install homebrew using below command (or check out the latest from their website.)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install Python 3

Now that we have homebrew installed, we can use the following command to install Python 3. It comes with pip3 installed. It is the standard package installer for Python.

brew install python3

Install / Create Virtual Environment

Python lets you self-contain your projects within a virtual environment. There’s an extensive post about what Python Virtual Environments are, what they are used for and how they work here, so check it out if you want to learn more. You’ll have to install the Virtual Environment package with pip using the command below.

pip3 install virtualenv

Once that is installed, you’ll have to create a Virtual Environment on a path of your choice (any folder where you want to install your virtual environment). You’ll have to source from that folder so make sure to use one that you can easily find or remember.

virtualenv -p python3 <desired/path>

After this, you’ll have had successfully created your virtual environment for Python. Use the following command to activate this venv (short for virtual environment) and you’re ready to start scripting python.

source <desired/path>/bin/activate

Here is an example of mine and notice how there’s (venv) in my command line. That means, I’ve successfully activated my venv and I’m in it.

Abigails-MBP:~ abigail$ source /Users/abigail/pythonplays/venv/bin/activate
(venv) Abigails-MBP:~ abigail$ 

Create an Alias

This is an optional step if you don’t want to keep typing the same command every time you go in to your venv. In Catalina OS, you’ll have to create and edit a file called .zshrc. If it’s your first time encountering this file, chances are you haven’t created one. It stores your aliases, basically shortcut commands. So, on your terminal, just create one and edit with the below commands (you might need to enter your password.

Abigails-MBP:~ abigail$ sudo touch ~/.zshrc
Abigails-MBP:~ abigail$ vi ~/.zshrc

You’ll have to edit the empty file and add the alias you want to use. Once it is saved, you can use the command alias to see all your saved aliases. See my example below. It means I can activate my virtual environment by just typing abienv on my terminal.

Abigails-MBP:~ abigail$ alias
alias abienv='source /Users/abigail/pythonplays/venv/bin/activate'
Abigails-MBP:~ abigail$ abienv
(venv) Abigails-MBP:~ abigail$ 

Install MongoDb

Now that we have Python covered, we will install MongoDb using our trusty brew installer. Below commands show how to install and start MongoDb service so you can start using the database server.

brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community

Now, before you can use mongodb, you’ll have to create a /data/db folder for MongoDb to use. By default, mongodb looks for this in your root folders which are read-only in Catalina OS. The solution is to create /data/db somewhere else and specify this path when you run mongodb. Below is the command to specify the path and when running mongodb.

mongod --dbpath /Users/abigail/Documents/Mongo/data/db

If you encounter another error, like I did, something about the port socket being used or denied, you can specify a different port when running the service.

mongod --dbpath /Users/abigail/Documents/Mongo/data/db --port 27018

Install PyMongo

Now that you have both Python and Mongo, it’s time to install PyMongo! This is a tool that lets you connect Python to your MongoDb databases. Guess what the command is?

pip3 install pymongo

And that’s the anti-climactic wrap up of this short tutorial for installing Python, MongoDb and PyMongo on Catalina OS (Mac). Happy coding!

You Might Also Like

4
Share your thoughts

avatar
3 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
Abigail ZekeAbigail Zekekarthikkarthik Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
karthik
Guest
karthik

Thank you for posting this!!

karthik
Member

Hey! awesome post!

Abigail Zeke
Member
Abigail Zeke

Hai hai