Loading lesson...
Loading lesson...
venv to isolate project dependencies.env files with startup validationThe command-line skills from Module 3 are the prerequisite for everything in this module. Here you will create the isolated development environment, configure secure credential storage, and set up the editor that will carry you through the rest of the course.
With the learning outcomes established, this module begins by examining python virtual environments in depth.
A virtual environment is an isolated Python installation for a specific project. Each virtual environment has its own copy of Python and its own set of installed packages. Changes in one virtual environment do not affect others or the system Python installation. This prevents the class of error where project A breaks because project B required a different version of the same library.
Create a virtual environment using Python's built-in venv module: navigate to your project folder, then run python3 -m venv .venv. This creates a hidden directory called .venv containing a private Python installation and a bin directory of executables. The dot prefix makes it hidden in most file browsers, which is conventional.
Activate the environment with source .venv/bin/activate on macOS and Linux, or .venv\Scripts\Activate.ps1 on Windows PowerShell. Your terminal prompt changes to show the active environment name: (.venv)appears at the start. Verify you are using the right Python with which python, which should point inside your .venv directory. Deactivate withdeactivate when you are done.
After installing all packages your project needs, record them withpip freeze > requirements.txt. This file captures the exact version of every installed package. Commit requirements.txt to version control. Never commit the .venv directory itself: it is large, platform-specific, and can always be recreated from the requirements file.
“The venv module supports creating lightweight virtual environments, each with their own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment's base Python.”
Python documentation - docs.python.org/3/library/venv.html
Virtual environments solve the multi-project dependency conflict problem at the operating system level. The base Python provides the interpreter; the virtual environment provides isolated package storage. Every Python project you build should start with python3 -m venv .venv before any pip install command.
Common misconception
“You only need a virtual environment for large projects with many dependencies.”
Dependency conflicts can occur with as few as two packages. A project that uses anthropic version 0.18 and another that uses anthropic version 0.25 cannot coexist in the same Python environment without one overwriting the other. Virtual environments cost nothing to create and take seconds to set up. Use one for every project, regardless of size.
With an understanding of python virtual environments in place, the discussion can now turn to node.js project setup, which builds directly on these foundations.
Node.js project isolation works differently from Python. Instead of a separate interpreter installation, Node.js projects store their dependencies in anode_modules directory inside the project folder. Each project has its own node_modules and its own package.json that records what is installed. Dependencies do not conflict between projects because each project carries its own copy.
Begin a new Node.js project with npm init -y. This createspackage.json with default values. Install the Anthropic SDK withnpm install @anthropic-ai/sdk. npm records the exact version installed in package-lock.json. Commit both package.json andpackage-lock.json to version control. Never commit node_modules: add it to .gitignore immediately.
If Node.js is not installed, the recommended approach is nvm (Node Version Manager), which lets you install and switch between multiple Node.js versions without affecting the system. Install nvm, then run nvm install --lts to get the long-term support version. Confirm with node --version, which should show v18 or newer for current Anthropic SDK support.
For TypeScript (TS) projects, which are preferred in this course for type safety, add npm install typescript @types/node --save-dev and initialise a TypeScript configuration with npx tsc --init. The --save-devflag records these as development dependencies that are not included in a production build.
“npm is the world's largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organisations use npm to manage private development as well.”
npm Documentation - docs.npmjs.com, Introduction
npm's scale is both its strength and its risk surface. The left-pad incident demonstrated that even tiny packages can become critical dependencies across thousands of projects. Using package-lock.json pins the exact version tree your project uses, preventing unexpected changes when a dependency is updated or removed.
With an understanding of node.js project setup in place, the discussion can now turn to api key management, which builds directly on these foundations.
Every AI agent project requires at least one API key. An API key is a secret credential that authenticates your code to an AI provider's service. Anthropic keys begin with sk-ant-; OpenAI keys begin with sk-proj-. Both grant billing access and should be treated with the same care as a password.
Create a .env file at the project root containing your keys inKEY=value format. Add this file to .gitignore immediately, as described in Module 3. Then install the dotenv library for your language: in Python, pip install python-dotenv; in Node.js, npm install dotenv.
In Python, call load_dotenv() at the start of your script. Theos.getenv("ANTHROPIC_API_KEY") function then returns the value. Validate that the key is present at startup by raising an error if it is missing. A clear startup error (ANTHROPIC_API_KEY not set. Check your .env file) is far easier to debug than a vague authentication error deep in the call stack when the key is finally needed.
In Node.js, call dotenv.config() before any other imports that use environment variables, then access values with process.env.ANTHROPIC_API_KEY. Apply the same startup validation: check the value is present and non-empty before your agent code runs.
Common misconception
“It is fine to hardcode API keys while developing locally, then remove them before deploying.”
Hardcoded keys appear in every file save, every autosave backup, every IDE search index, and every git commit. The intention to remove them before deploying is a reliable path to accidental exposure: the Uber incident involved exactly this pattern. The .env approach costs one minute to set up and protects against permanent credential exposure. Use it from the first line of every project.
With an understanding of api key management in place, the discussion can now turn to ide setup: vs code, which builds directly on these foundations.
VS Code (Visual Studio Code) is the most widely used editor for Python and JavaScript/TypeScript development. It is free, open-source, and has excellent extensions for AI agent development. The key extensions for this course are:Python (ms-python.python) for syntax highlighting and virtual environment detection; Pylance (ms-python.vscode-pylance) for fast type checking and autocomplete; and Prettier(esbenp.prettier-vscode) for consistent code formatting.
After opening a Python project in VS Code, select the correct interpreter by pressingCmd + Shift + P (macOS) or Ctrl + Shift + P (Windows/Linux), typing "Python: Select Interpreter," and choosing the interpreter inside your.venv directory. VS Code will then use that environment's packages for linting and autocomplete, so missing imports show as errors before you run the code.
Install extensions from the command line using code --install-extension ms-python.pythonand similarly for each extension ID. This approach is faster than the GUI when setting up a new machine and can be scripted for team standardisation.
docs.python.org/3/library/venv.html
Official guide to creating and managing virtual environments using the built-in venv module. Quoted in Section 4.1 to establish the canonical approach to Python project isolation.
github.com/nvm-sh/nvm
The standard tool for managing multiple Node.js versions without system-level conflicts. Referenced in Section 4.2 as the recommended installation method for Node.js on macOS and Linux.
OWASP Secrets Management Cheat Sheet
cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
Industry standard guidance on handling API keys and other secrets. Referenced in Section 4.3 to ground the .env and .gitignore approach in a recognised security control framework.
github.com/theskumar/python-dotenv
The de facto standard library for loading .env files in Python projects. Referenced in Section 4.3 as the implementation detail for the API key loading pattern.
docs.npmjs.com
Official reference for the Node.js package manager. Quoted in Section 4.2 to contextualise npm's scale and the importance of package-lock.json for reproducible builds.
Module 4 of 25 in Foundations