Today, I built a Python project to interact with the Mailchimp API. This post captures the workflow I followed, along with key lessons learned.
1. Setting Up the Environment
Open VS Code and Create the Project Directory
First, I navigated to my GitHub folder on my local machine.
Using the terminal, I created a new directory for my project:
mkdir mailchimp
Then, I set my working directory:
cd mailchimp
Create a Python Virtual Environment
To isolate my project dependencies, I created a virtual environment:
python -m venv mailchimp_env
Prepare the Environment
- Bash Terminal Setup: I used a bash terminal in VS Code to confirm my file path with
cd
. - Create a
.env
File:
I created a blank.env
file to securely store my Mailchimp credentials:touch .env
After adding my Mailchimp credentials, I saved the file (I actually didn't at first but eventually figured out what I'd done wrong). - Create a
.gitignore
File:
To prevent sensitive and unnecessary files from being added to Git, I created a.gitignore
file:touch .gitignore
Inside.gitignore
, I added:*.env *.*_env *\data*
- Activate the Python Environment:
After creating the environment, I activated it and performed a quick test to ensure everything was set up.
2. Version Control with Git and GitHub
Initialize Git Repository
I initialized an empty Git repository locally:
git init
Next, I staged all my project files:
git add
.
Finally, I committed the staged files with a meaningful message:
git commit -m "Initial commit"
Line Ending Configuration
During this step, I noticed the line endings were automatically converted from LF to CRLF. This is how new lines are formatted on Windows systems, so it’s not an issue.
Connect to GitHub
I went to GitHub and created a new repository for my project without a README file. Back in my terminal, I linked the local repository to the remote repository:
git remote add origin <link to repository>.git
To push my work to GitHub:
git push -u origin master
The -u
flag sets the upstream branch, which makes future pushes simpler.
Master and main are names of branches which can be used interchangeably. This isn't a problem if I have either main or master branch.
3. Branching for Feature Development
To follow good development practices, I created a new branch to work on my Python script:
git checkout -b python-feature
I wrote my Python script in this branch and only published it to master
when it was in a stable or meaningful state.
4. Installing Necessary Packages
In the PowerShell terminal, I installed the required packages for the project:
python-dotenv
: To load environment variables from the.env
file.- Mailchimp Python SDK:
Mailchimp offers an official Python client library, which I installed directly from their GitHub repository.
Note: The os
module, which I needed to access environment variables, is a native Python library and doesn’t require pip installation.
5. Mailchimp API Objectives
Here’s an outline of the data I want to extract from Mailchimp via its API:
- Campaigns
- Email Activity (requires campaign IDs)
- Lists
- List Members (requires list IDs from the Lists endpoint)
- Reports
- Unsubscribes (requires campaign IDs)
By the end of this project, I aim to have a set of scripts that can extract, process, and store these datasets for further analysis.
Key Takeaways
- Environment Setup: Setting up a virtual environment and
.env
file is essential for clean and secure development. - Git Workflow: Syncing with GitHub and creating feature branches ensures smooth collaboration and version control.
- Mailchimp SDK: The official Python library simplifies interaction with the Mailchimp API.
- Planning API Calls: Identifying dependencies (like needing campaign IDs for certain endpoints) is crucial for efficient API workflows.