DEV Community

M.T.Ramkrushna
M.T.Ramkrushna

Posted on

PYTHON ESSENTIALS FOR AI/ML (Virtual Environment)

πŸš€ What is a Virtual Environment?

A virtual environment is like giving each project its own private room with its own:

  • Python version
  • Installed packages
  • Dependencies

Real-life analogy:

Imagine you have:

  • A gym bag
  • A college bag
  • A travel bag

Each has its own items, and adding something to one doesn’t disturb the others.

Virtual environments do the same for your projects.


πŸš€ Why Do We Need Virtual Environments?

1️⃣ Avoid Version Conflicts

Project A needs:

numpy 1.20
Enter fullscreen mode Exit fullscreen mode

Project B needs:

numpy 1.26
Enter fullscreen mode Exit fullscreen mode

If you install globally, they fight.
With virtual environments β†’ no conflict.


2️⃣ Consistent Setups

Your project can be reproduced anywhere:

  • same Python version
  • same packages
  • same environment

This is crucial for:

  • AI/ML experiments
  • production apps
  • teamwork

3️⃣ Keeps System Clean

No messing up your global Python installation.

Real-life analogy:
Don’t mix all spices in one big jar β€” keep separate boxes.


πŸš€ Virtual Environment Options

Two standard ways:

βœ” venv

Built-in, lightweight, perfect for normal Python dev.

βœ” conda

Comes with Anaconda/Miniconda.
Great for AI/ML, because it handles:

  • Python versions
  • C dependencies
  • ML libraries (TensorFlow / PyTorch)
  • GPU packages

πŸš€ Using venv (Built-in Python)

πŸ›  Step 1: Create venv

python3 -m venv myenv
Enter fullscreen mode Exit fullscreen mode

This creates:

myenv/
    bin/
    lib/
    include/
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 2: Activate

Windows

myenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Mac/Linux

source myenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

After activation you’ll see:

(myenv) $
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 3: Install packages

pip install numpy
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 4: Deactivate

deactivate
Enter fullscreen mode Exit fullscreen mode

πŸš€ Using Conda Environments (Recommended for ML)

πŸ›  Step 1: Create environment

conda create -n myenv python=3.11
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 2: Activate

conda activate myenv
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 3: Install packages

conda install numpy
Enter fullscreen mode Exit fullscreen mode

or mix pip:

pip install pandas
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 4: Deactivate

conda deactivate
Enter fullscreen mode Exit fullscreen mode

πŸš€ Visual Explanation (ASCII)

How system looks WITHOUT virtual env:

Global Python
 β”œβ”€β”€ numpy 1.20
 β”œβ”€β”€ pandas 1.2
 β”œβ”€β”€ django 2.2
 └── tensorflow 1.0
Enter fullscreen mode Exit fullscreen mode

β†’ If you update one, everything breaks.


How system looks WITH virtual env:

projectA_env
   β”œβ”€β”€ numpy 1.20
   └── pandas 1.2

projectB_env
   β”œβ”€β”€ numpy 1.26
   └── django 4.0

ml_env
   β”œβ”€β”€ tensorflow 2.15
   └── torch 2.3
Enter fullscreen mode Exit fullscreen mode

Each one is independent.


πŸš€ Real-Life Examples

βœ” Example 1: Working on 2 AI Projects

  • Chatbot project uses Python 3.9 + TensorFlow
  • LLM fine-tuning uses Python 3.11 + PyTorch

With conda:

conda create -n chatbot python=3.9 tensorflow
conda create -n llm python=3.11 pytorch
Enter fullscreen mode Exit fullscreen mode

βœ” Example 2: Deploying a Backend Using venv

For Django/Flask apps:

python3 -m venv backend_env
source backend_env/bin/activate
pip install django gunicorn
Enter fullscreen mode Exit fullscreen mode

βœ” Example 3: Keeping experiments isolated

ML experimentation often means:

  • Trying different library versions
  • Custom builds
  • GPU-compatible wheels

Conda helps you switch quickly:

conda create -n test_torch python=3.10 pytorch=2.2
conda activate test_torch
Enter fullscreen mode Exit fullscreen mode

πŸš€ Bonus: Requirements Files

To share or reproduce your environment:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Install from it:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

πŸš€ Quick Summary

  • Virtual environments = isolated sandboxes
  • venv β†’ simple projects
  • conda β†’ ML/AI, CUDA, heavy libs
  • Prevent conflicts
  • Clean, reproducible environments

Top comments (0)