Dependency management is the silent killer of AI projects. If you install every library globally on your machine, you will inevitably cause fatal version conflicts between projects. Virtual Environments are mandatory engineering practice.
1The Dependency Crisis
Imagine you are building two AI models on your laptop. Project A is a legacy computer vision model that strictly requires TensorFlow 1.15. Project B is a modern language model that requires TensorFlow 2.10. If you try to install both globally, one will overwrite the other, and at least one project will instantly break.
The solution is a Virtual Environment. A virtual environment is an isolated directory (a 'sandbox') containing its own independent installation of Python and its own isolated library folder. When you run code inside that sandbox, it is completely blind to any other libraries installed elsewhere on your computer.
# Creating a new sandbox named 'ai_env' using Python 3.10
$ conda create --name ai_env python=3.10
# Or using Python's built-in venv module
$ python -m venv my_sandboxSolving environment: done
Preparing transaction: done
Executing transaction: done
2Activating the Sandbox
Creating the sandbox does nothing on its own. You must 'activate' it. Activation hijacks your terminal's PATH variable, temporarily pointing it away from the global system and directly into your sandbox folder.
You always know an environment is active because its name appears in parentheses at the very beginning of your terminal prompt (e.g., (ai_env) C:\>). Once activated, any library you install via pip is trapped securely inside that sandbox. When you are done working, you run conda deactivate to restore your system to its normal state.
# Activate the environment
$ conda activate ai_env
# Install a specific version of a library safely
(ai_env) $ pip install numpy==1.26.0
# Leave the environment
(ai_env) $ conda deactivate(ai_env) C:\> pip install numpy==1.26.0
Successfully installed numpy-1.26.0
3Reproducibility and Requirements
The ultimate goal of environment isolation is reproducibility. If you build a model on your laptop, you need a way to ensure it runs identically on a production server or on a coworker's machine.
You do this by generating a manifest of your sandbox. Using pip freeze > requirements.txt dumps a list of every library (and its exact version number) installed in your active sandbox into a text file. You commit that file to Git. When your coworker pulls your code, they simply run pip install -r requirements.txt inside their own blank sandbox, instantly cloning your exact working conditions.
# Generate the manifest file
(ai_env) $ pip freeze > requirements.txt
# The teammate creates a blank sandbox...
(coworker_env) $ pip install -r requirements.txtCollecting pandas==2.1.1 (from -r requirements.txt)
Installing collected packages...
Successfully installed
