Set Up a Python AI Environment

Set up a Python AI development environment in this Applied AI engineering tutorial — hands-on steps, troubleshooting, and what to study next.

Focus: set up a python ai development environment

Sponsored

You've built solid Python foundations over the last 25 lessons, but now you're about to take on your first real AI project. You install TensorFlow, then PyTorch, then a LangChain template, and suddenly pip is fighting with itself, your GPU is invisible to your code, and you can't tell which package version your experiments actually ran on. That mess isn't a sign you're bad at coding — it's a sign you skipped the setup phase. In this lesson, you'll learn how to set up a Python AI development environment that is reproducible, isolated, and fast enough to prototype and productionize like a professional applied AI engineer.

The Problem This Lesson Solves

Most AI tutorials skip straight to the model code, but that's like teaching someone to drive by handing them keys to a car with no oil. Here's the real pain you've likely already felt:

  • Dependency hell: pip install torch works on your laptop, then breaks your teammate's setup because they have a different CUDA version.
  • Silent version drift: You ran an experiment last week that gave you 94% accuracy, but now import transformers pulls a newer tokenizer that changes your results — and you have no idea why.
  • Environment pollution: You install packages globally, and suddenly your data-science project and your Django web app are fighting over the same NumPy version.
  • GPU misconfiguration: Your code runs fine on CPU but grotesquely slow, and you only discover later that CUDA wasn't installed properly.

These problems don't just waste hours — they undermine the trust you place in your own results. A proper AI development environment isn't a luxury; it's a foundational practice that lets you focus on your model, not your package manager.

Core Concept / Mental Model

Think of your AI development environment as a clean lab bench for every experiment. Each project gets its own isolated workspace (a virtual environment), a dependency manifest (a file that says exactly what's on the bench), and a reproducible launch sequence (how you step into that workspace).

The mental model breaks down into three layers:

  1. The base Python interpreter — the raw runtime that executes your code.
  2. The virtual environment — a per-project copy of that runtime with its own site-packages folder, so package versions don't leak between projects.
  3. The orchestration layer — tools like uv, poetry, or conda that manage the virtual environment, dependencies, and lock files for you.

Pro tip: AI projects are especially sensitive to isolation because deep-learning frameworks like PyTorch and TensorFlow have complex binary dependencies (CUDA, cuDNN) that can't share a global namespace without conflicts.

Once you internalize this three-layer model, every setup step becomes predictable: create a lockfile → create an environment from it → install dependencies → verify the GPU works → run your experiment.

How It Works Step by Step

Here is the recommended, battle-tested sequence for setting up a Python AI development environment:

1. Choose Your Package Manager

Modern AI development has moved beyond bare pip + venv. You have three strong contenders:

  • pip + venv — Python's built-in tools, simple and universal, but slow and lockfiles (requirements.txt) are often incomplete.
  • uv — a fast, Rust-based drop-in replacement for pip/venv that generates a proper uv.lock file. It's the current darling of the Python community for a reason.
  • conda / miniconda — best for managing non-Python binary dependencies like CUDA. If you're on Windows or need strict GPU control, conda is a safe choice.

2. Create a Project-First Structure

Never install AI packages globally. Create a dedicated project folder with a pyproject.toml (or requirements.txt) at its root. This manifest becomes your single source of truth.

3. Generate a Lockfile

A lockfile pins exact versions of every package — including transitive dependencies — so your environment is byte-for-byte reproducible. uv.lock does this for uv, and poetry.lock does it for Poetry.

4. Create and Activate the Virtual Environment

The tool you chose creates a .venv folder inside your project. Activation is just a shell command that points your python to that folder's interpreter.

5. Install AI Frameworks Incrementally

Install your core frameworks (PyTorch or TensorFlow) first, then add higher-level libraries like transformers or langchain. This order helps you isolate version conflicts.

6. Verify Everything with a Hello-World Script

Run a tiny script that imports your framework and checks for GPU availability. If it fails, you know it's your environment — not your model code.

Pro tip: Always document your setup steps in a README.md. Your future self will thank you when you come back to a project six months later.

Hands-On Walkthrough

Let's walk through the entire process using uv — the fastest and most modern approach. You'll build a minimal AI environment with PyTorch and the Hugging Face Transformers library.

First, install uv on macOS or Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh

On Windows (PowerShell), use:

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

Now create your project structure and virtual environment:

mkdir my_ai_project && cd my_ai_project
uv init

# This creates pyproject.toml and .venv automatically.
# Add your AI dependencies (this command also generates uv.lock)
uv add torch transformers

When the command finishes, you'll see a uv.lock file. Let's verify the environment works:

# Activate the virtual environment (on Linux/macOS)
source .venv/bin/activate

# On Windows: .venv\Scripts\activate

python --version
# Should output Python 3.12.x (or whatever you specified)

python -c "import torch; print('CUDA available:', torch.cuda.is_available())"
# Expected output (on a GPU machine): CUDA available: True
# On a CPU-only machine: CUDA available: False

Now write a tiny transformer script to confirm everything is wired up. Create check_env.py:

from transformers import pipeline

classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
result = classifier("I'm so happy to have a clean AI environment!")
print(result)
# Expected output: [{'label': 'POSITIVE', 'score': 0.9998...}]

Run it after you've activated the environment:

python check_env.py

You should see a positive sentiment prediction, proving that transformers, torch, and the model download all work. If this runs, your environment is production-ready for AI development.

Pro tip: Always run a version check and GPU check as part of your CI pipeline. It's the single best defense against silent environment rot.

Compare Options / When to Choose What

The table below compares the three most common ways to set up a Python AI development environment, so you can pick the right tool for your context.

Approach Strengths Weaknesses Best For
pip + venv Built into Python, zero extra install Slow resolution, lockfiles require extra tooling (pip-tools) Quick scripts, educational projects
uv Extremely fast, single binary, modern lockfile Newer, less battle-tested in legacy corporate envs Most new Python projects — our default
conda Manages non-Python binaries (CUDA, cuDNN) Slower, heavier, can conflict with pip GPU-heavy DL projects on Windows/legacy stacks

When to choose what:

  • Start with uv unless you have a specific reason not to. It's fast, reproducible, and becoming the industry standard.
  • Use conda if you're on Windows and need to install CUDA drivers as a package, or if your team already standardizes on conda.
  • Fall back to pip + venv when you're in a constrained environment (e.g., a university server with no admin rights) and can't install third-party tools.

Troubleshooting & Edge Cases

You'll hit these issues at some point; here's how to diagnose them fast.

ImportError: No module named 'torch'

You're either not in the activated virtual environment, or you installed torch globally by accident. Fix: which python, source .venv/bin/activate, then uv add torch.

torch.cuda.is_available() returns False even with an NVIDIA GPU

This is the most common GPU issue. Check your CUDA driver version:

nvidia-smi
# If it errors, you don't have NVIDIA drivers installed.

Then reinstall PyTorch with the matching CUDA version, e.g.:

uv pip install torch --index-url https://download.pytorch.org/whl/cu121

Version mismatch between transformers and torch

Sometimes a new transformers version requires a newer torch, causing cryptic errors. Fix: run uv lock --upgrade-package torch to update both in sync.

uv add is slow or hangs

It may be a network issue or a huge dependency tree. Try uv add --no-cache to skip the cache, or set a custom index via UV_DEFAULT_INDEX environment variable.

What You Learned & What's Next

You've now built a reproducible Python AI environment using uv, installed deep learning frameworks, and verified both CPU and GPU functionality. You understand the three-layer mental model — base interpreter, virtual environment, orchestration tool — and you know how to choose between pip/venv, uv, and conda based on your project's needs.

You've also practiced the essential first step of any applied AI workflow: isolate first, then code. This habit will save you countless debugging hours.

Next up: In the next lesson, we'll take this clean environment and build your first real AI application — calling a model to generate structured output. You'll see why a solid environment setup is the foundation for all that exciting work to come.

Quick recap of what you learned:

  • The importance of isolating AI dependencies per project.
  • How to use uv to create a lockfile-based environment in seconds.
  • How to verify GPU availability with a one-liner.
  • How to compare tools and pick the right one for your team.

Now go clean up that global site-packages mess you've been ignoring — and build something great.

Practice recap

Now it's your turn: create a brand-new project with uv init, add torch and transformers, and run a small sentiment analysis script. Then, delete the .venv folder and recreate it from your uv.lock file — this proves your environment is reproducible. Finally, paste the output of torch.cuda.is_available() into a comment in your code so you never forget whether you ran on GPU or CPU.

Common mistakes

  • Installing AI packages globally with pip, leading to version conflicts and environment pollution.
  • Forgetting to activate the virtual environment before running scripts, causing ImportError or wrong package versions.
  • Ignoring the lockfile and manually updating dependencies, resulting in non-reproducible experiments.
  • Not checking GPU availability before training, wasting hours on CPU-only runs.
  • Pinning only direct dependencies in requirements.txt while leaving transitive deps floating, breaking reproducibility.

Variations

  1. Use Poetry instead of uv for a more mature lockfile and dependency resolution ecosystem.
  2. Use Docker containers to package the entire AI environment, ensuring identical setups across all machines.
  3. Use Jupyter notebooks with ipykernel to run experiments inside your virtual environment.

Real-world use cases

  • Data science team at a startup standardizes on uv and uv.lock to reproduce experiments across machines.
  • ML engineer uses conda on a Windows gaming laptop to manage CUDA dependencies for local GPU training.
  • AI research lab ships a Docker image with a locked Python environment to ensure paper reproducibility on any cluster.

Key takeaways

  • Isolation via virtual environments is non-negotiable for AI projects.
  • Use uv for fast, reproducible environment setup; fall back to conda for binary GPU deps.
  • Always generate and commit a lockfile to guarantee byte-for-byte reproducibility.
  • Verify CUDA availability before running any training code.
  • Document your setup steps so your future self (and teammates) can reproduce them.
  • Start with a minimal environment and add AI frameworks incrementally to ease debugging.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.