Set Up Your AI Dev Environment
Set up your AI development environment for Applied AI engineering — hands-on Python setup, troubleshooting, and next steps.
Focus: set up your ai dev environment
Every AI project — whether you're calling an LLM API, building a RAG pipeline, or fine-tuning a model — starts with the same unglamorous bottleneck: a working development environment. Hours of debugging import errors, version conflicts, and missing credentials can kill your momentum before you write a single line of model code. This lesson gives you a repeatable, battle-tested setup so you spend your time building, not fighting your machine.
The problem this lesson solves
When you're starting with Applied AI engineering, the biggest blocker isn't the concepts — it's the setup. You install packages with pip install openai, only to discover the CUDA version doesn't match, or Python 3.9 breaks a library that requires 3.10+. Environment mismatches lead to cryptic errors like ModuleNotFoundError: No module named 'torch' or ImportError: libcudnn.so.8: cannot open shared object file. Without a clear setup process, you waste hours troubleshooting, and worse, you lose confidence.
You need a reproducible, isolated, and testable foundation that works for both CPU-based development and GPU-accelerated workloads. This lesson walks you through exactly that — from Python version management to virtual environments, dependency pinning, and validating your stack.
Core concept / mental model
Think of your AI dev environment as a controlled laboratory for your code. You wouldn't run chemical experiments without a fume hood and labeled containers; similarly, you shouldn't run AI experiments on a bare system where every pip install leaks into your global Python.
The mental model has three pillars:
- Isolation — Each project gets its own sandbox (virtual environment) with its own dependencies. What you install for one project never breaks another.
- Reproducibility — Dependencies are pinned to exact versions (or ranges) so anyone (including future you) can recreate the same environment.
- Tooling — You use purpose-built tools that automate this process, from Python version managers to containerization.
We'll focus on Python 3.10+, the modern standard for AI/ML libraries like PyTorch, Hugging Face Transformers, and LangChain.
How it works step by step
Let's break down the setup into five discrete steps. Follow them in order — each builds on the last.
1. Install a Python version manager
A Python version manager lets you install and switch between multiple Python versions without affecting system Python. The two most common are:
- pyenv (macOS/Linux) — simple and well-supported
- uv (cross-platform) — faster, modern, and also handles package management
Here's a quick comparison:
| Tool | Purpose | Platform | Speed | When to choose |
|---|---|---|---|---|
| pyenv | Python version manager only | macOS, Linux, WSL | Slow (compiles or downloads) | You want maximum compatibility with community guides |
| uv | Version manager + package manager | macOS, Linux, Windows | Very fast (Rust-based) | You want modern speed and fewer tools |
| conda | Environment + package manager | All | Moderate | You work with binary packages (e.g., R, C++ libraries) |
2. Create a project directory and virtual environment
Once your Python version is ready, create an isolated environment for your AI project. This prevents dependency conflicts.
# Create a project folder
mkdir ai-playground && cd ai-playground
# Create a virtual environment (Python 3.10+)
python -m venv .venv
# Activate it (macOS/Linux)
source .venv/bin/activate
# On Windows: .venv\Scripts\activate
Pro tip: Always activate your environment before installing anything. A quick check — run
which python— should point to your project's.venvfolder.
3. Install core AI packages
Now install the essential libraries. Use pip directly for simplicity, or uv if you chose that route.
# Inside the activated environment
pip install --upgrade pip
pip install numpy pandas matplotlib jupyter
pip install openai langchain httpx # for LLM APIs
If you're working with deep learning, you'll also want PyTorch. For CPU-only, the default install works. For GPU, check the official install command for your CUDA version.
# CPU version (safe fallback)
pip install torch torchvision torchaudio
4. Pin dependencies for reproducibility
Once your environment works, freeze the versions into a requirements file. This makes your environment shareable and reproducible.
pip freeze > requirements.txt
Your requirements.txt will look something like:
numpy==1.26.4
openai==1.30.1
langchain==0.2.5
5. Verify the environment works
Do a quick sanity check before starting any real work. Write a small Python script that imports your key libraries and prints a version.
import sys
import numpy as np
import openai
def verify_environment():
print(f"Python version: {sys.version}")
print(f"NumPy version: {np.__version__}")
print(f"OpenAI version: {openai.__version__}")
# A tiny computation to confirm NumPy works
arr = np.array([1, 2, 3])
print(f"NumPy sum: {arr.sum()}")
if __name__ == "__main__":
verify_environment()
Expected output (versions may differ):
Python version: 3.10.12
NumPy version: 1.26.4
OpenAI version: 1.30.1
NumPy sum: 6
Hands-on walkthrough
Let's run through a complete, end-to-end setup exercise. This is the exact workflow you'll use for every AI project.
Exercise: Build a minimal AI-ready environment
Step 1: Create a project and isolated environment
mkdir my-ai-project && cd my-ai-project
python -m venv .venv
source .venv/bin/activate # macOS/Linux
Step 2: Install core packages and save requirements
pip install numpy pandas openai langchain # add more as needed
pip freeze > requirements.txt
Step 3: Write a quick test to confirm everything imports
Create test_setup.py with the following content:
import numpy as np
import pandas as pd
import openai
def test_libraries():
df = pd.DataFrame({"feature": [1, 2, 3], "label": [0, 1, 0]})
scaled = df["feature"] / df["feature"].max()
print(f"Dataframe shape: {df.shape}")
print(f"Scaled values: {scaled.tolist()}")
print(f"OpenAI client available: {hasattr(openai, 'OpenAI')}")
if __name__ == "__main__":
test_libraries()
Run it:
python test_setup.py
Expected output:
Dataframe shape: (3, 2)
Scaled values: [0.5, 1.0, 0.0]
OpenAI client available: True
If you see this output, your environment is ready for AI development.
A realistic AI test: calling an LLM API
Once the basics work, you should test connectivity to an LLM API. Let's use OpenAI as an example (you can adapt to any provider).
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
response = client.chat.completions.create(
model="gpt-3.5-turbo", # or another available model
messages=[{"role": "user", "content": "Hello, AI!"}]
)
print(response.choices[0].message.content)
Pro tip: Store your API key as an environment variable using
export OPENAI_API_KEY='your-key'(macOS/Linux) orset OPENAI_API_KEY=your-key(Windows). Never hard-code keys in your scripts.
If you don't have an API key yet, you can still verify the setup by checking that the openai library imports correctly.
Compare options / when to choose what
You have three main paths for setting up an AI dev environment. Here's how to decide:
| Approach | Pros | Cons | Best for |
|---|---|---|---|
| Local virtual env (venv) | Simple, no extra tools, fast | Requires manual dependency management, not great for complex dependencies | Quick experiments and learning |
| Conda | Handles binary packages, good for scientific computing | Slower environment resolution, larger installs | Data scientists who need libraries like R or mixed-language stacks |
| Docker containers | Perfect reproducibility, portable across machines | Steep learning curve, adds overhead | Production deployments, team collaboration |
| uv | Very fast, modern, combined version + package management | Still evolving, less community-written tutorials | Developers who value speed and want a single tool |
Recommendation: Start with a plain venv for this course. It's the simplest, most transparent, and works across all platforms. As your projects scale, you can graduate to uv or Docker.
Troubleshooting & edge cases
Even with a clear process, you'll hit snags. Here are the most common issues and concrete fixes.
ModuleNotFoundError after installation
If you run a script and get ModuleNotFoundError: No module named 'numpy', it means the package isn't installed in the active environment.
- Fix: Ensure the correct virtual environment is activated by running
which python. Deactivate and reactivate if needed, then runpip install numpyagain.
pip installs to the wrong Python
Sometimes pip points to a different Python than python. This happens when your PATH is misconfigured.
- Fix: Run
python -m pip install <package>instead ofpip install <package>. This ensures you install into the same Python interpreter you're using.
CUDA/GPU errors
When importing PyTorch you might see AssertionError: Torch not compiled with CUDA enabled. This means the CPU-only version was installed.
- Fix: Uninstall and reinstall the correct PyTorch build using the official PyTorch get-started page. Choose your OS, package manager, and CUDA version (or CPU).
conda vs pip dependency conflicts
You might end up with two package managers that fight over the same package. This leads to broken environments.
- Fix: Choose one primary package manager per project. If you use Conda, install everything with Conda; if you use
venv, usepip. Avoid mixing unless you know what you're doing.
API key not found
If you see AuthenticationError from an LLM SDK, the API key isn't set correctly.
- Fix: Double-check that the environment variable is set in the same shell where you run your script. Use
print(os.getenv("OPENAI_API_KEY"))to debug — never print the full key in logs.
Dependency version hell
Installing a new package often upgrades a dependency that breaks another package. This is known as the "dependency hell".
- Fix: Use
pip freeze > requirements.txtto capture working versions. If you need to roll back, create a new virtual environment and install from that file.
What you learned & what's next
You now have a repeatable process for setting up an AI dev environment. Specifically, you can:
- Explain why isolation and reproducibility matter for AI projects.
- Create an isolated Python environment using
venvorconda. - Install core AI libraries (NumPy, pandas, OpenAI, LangChain) and pin dependencies.
- Verify your environment with a quick test script.
- Troubleshoot common issues like wrong Python version, missing modules, and CUDA problems.
Your environment is ready for the next lesson in the track: Making Your First LLM API Call. You'll use this exact setup to send your first prompt and handle the response. Keep your requirements.txt handy — you'll extend it with new libraries as we go.
Final pro tip: Treat your dev environment like a living document. Update
requirements.txtwhenever you add a dependency, and commit it to version control. Future you will thank you.
Now go build something.
Practice recap
Create a new directory, set up a virtual environment, install numpy and requests, and write a script that fetches a URL and prints its status code. Then run pip freeze > requirements.txt and inspect the file to see the exact dependencies.
Common mistakes
- Installing packages with
pip installbefore activating your virtual environment — this pollutes the global Python and creates version conflicts. - Hardcoding API keys directly in scripts — always use environment variables to keep secrets out of code.
- Mixing
pipandcondain the same environment — this can cause dependency conflicts that are hard to debug. - Forgetting to pin dependency versions — without
pip freeze, you can't reproduce the environment later.
Variations
- Use
uvinstead ofpipfor faster installs and a single tool for environment and package management. - Use Docker to containerize your entire dev environment for perfect portability across machines.
- Use
pyenv+venvon macOS/Linux to manage multiple Python versions per project.
Real-world use cases
- Kicking off a new AI research project where you need a clean Python environment with PyTorch and Transformers, isolated from other work.
- Setting up a team CI pipeline that runs tests on your AI models using a
requirements.txtfile to ensure consistent dependencies. - Deploying an AI service on a remote server using Docker and environment variables to keep API keys secure.
Key takeaways
- Use isolated virtual environments (
venvorconda) to avoid dependency conflicts between AI projects. - Pin dependencies with
pip freeze > requirements.txtfor reproducibility. - Install a Python version manager to easily switch between Python 3.10+ and other versions.
- Verify your environment with a simple import test before starting real work.
- Troubleshoot common issues by checking
which python, usingpython -m pip, and re-installing GPU-enabled packages when needed. - Store API keys in environment variables, never in code.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.