Supercharge Your Python Projects on MacOS: From Environment Setup to AI Agents
Welcome, fellow developers! If you're diving into the world of Python development on MacOS, you're in the right place. This guide will take you from setting up your Python environment to crafting your first AI-powered agent using OpenAI's Agent SDK. Whether you're a seasoned coder or just starting out, we'll make sure you have the tools and knowledge to build amazing things. Get ready to streamline your workflow and unleash the power of Python and AI!
Step 1: Setting Up Your Python Environment on MacOS
MacOS comes with Python pre-installed, but it’s often outdated and not recommended for development. Let’s get a modern, manageable version:
Homebrew: Your Package Manager Friend:
First, we need a package manager. Homebrew is the most popular option for MacOS. If you don’t have it already, open your terminal and run this command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
This command will download and execute the Homebrew installation script. Follow the prompts on screen. You might be asked to install Xcode command line tools, if you don't have them. It will also prompt for your password. After installation, you need to add Homebrew to your shell path:
For zsh (the default on newer MacOS):
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
Once install verify the installation with the following command:
brew version
Installing Python:
Now that Homebrew is set up, installing Python is a breeze:
brew install python
This installs the latest version of Python and Pip, the package installer for Python.
Verify Installation:
To verify Python is installed and working, run:
python3 --version pip3 --version
You should see the version numbers of your newly installed Python and Pip.
Step 2: Setting Up a Virtual Environment
Virtual environments are essential for keeping your project dependencies separate. This means different projects can use different versions of libraries without conflict. Here's how to create one:
Create a Virtual Environment with venv:
Navigate to your project folder or where you want to store your virtual environment and run:
mkdir scheduler_agent cd scheduler_agent python3 -m venv scheduler_agent_env
Replace scheduler_agent_env with your desired environment name. This creates a folder that contains a separate Python installation
Activating the Virtual Environment:
To start using your newly created environment, activate it using this command:
source scheduler_agent_env/bin/activate
You will know the environment is activated when your terminal prompt changes to include the name of the environment, ex. (scheduler_agent_env).
Deactivating the Virtual Environment:
When you are done with your environment, deactivate it using this command:
deactivate
Step 3: Starting a Project with OpenAI's Agent SDK
Now that your environment is ready, let's dive into OpenAI's Agent SDK.
Install the Open AI Agent SDK:
With your virtual environment activated, install the OpenAI Agent SDK and its dependencies via pip:
pip install openai-agents
Get an OpenAI API Key:
To use the Agent SDK, you'll need an API key from OpenAI. Visit the OpenAI website to obtain one.
Set your environment variable with the Open AI key.
export OPENAI_API_KEY=sk-...
Create a Simple Agent
Create a file called agent.py in your project directory.
touch agent.py
Paste the following code into it. This will be the beginning of your agent.
from agents import Agent, Runner
agent = Agent(name="Assistant", instructions="You are a helpful assistant")
result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)
This is the project structure for reference.
Sites/
└── scheduler_agent/
├── scheduler_agent_env
└── agent.py
Run your agent
From your terminal, run your newly created agent.
python agent.py
This script will run a simple agent that writes a simple haiku about recursion, the output should look like the following:
# Code within the code,
# Functions calling themselves,
# Infinite loop's dance.
Congratulations! You’ve successfully set up your Python development environment on macOS, created and activated a virtual environment, and ran a simple AI agent powered by OpenAI's Agent SDK. This is just the beginning! Feel free to explore the vast ecosystem of Python libraries and the possibilities of AI-powered agents. This is part one of this series, sign up for my email list to be notified when the second part comes up or connect with me on social media. Happy coding!