# Day 1: Create a Python Virtual Environment for ML
🎯 Objective
Create an isolated Python virtual environment for a Machine Learning project and install the essential ML libraries.
In this task, we will:
🧠 Concept
A Python virtual environment provides an isolated environment for a Python project.
Instead of installing packages globally:
System Python
├── numpy
├── pandas
├── scikit-learn
└── matplotlibwe create a project-specific environment:
Project
│
├── ml-env/
│ ├── Python
│ └── Packages
│
└── requirements.txtThis prevents dependencies from different projects from conflicting with each other.
For example:
Project A
└── numpy 1.x
Project B
└── numpy 2.xEach project can maintain its own package versions.
📂 Step 1: Check Current Directory
Check the current working directory:
pwdpwd stands for:
Print Working DirectoryExample:
/root/codeThis tells us where the virtual environment will be created.
🐍 Step 2: Check Python Version
Verify that Python 3 is installed:
python3 --versionExample:
Python 3.x.xThis confirms that Python is available before creating the environment.
📦 Step 3: Create the Virtual Environment
Create a virtual environment called:
ml-envRun:
python3 -m venv ml-envCommand Breakdown
python3Runs Python 3.
-mRuns a Python module as a command.
venvPython's built-in virtual environment module.
ml-envThe directory/name of the new environment.
The structure becomes:
/root/code/
│
└── ml-env/
├── bin/
├── include/
├── lib/
└── pyvenv.cfg🔍 Step 4: Verify the Environment
List files in the current directory:
ls -lahYou should see:
ml-envThen inspect the environment:
ls ml-envTypical contents:
bin
include
lib
lib64
pyvenv.cfgThe exact directories can vary slightly depending on the operating system and Python installation.
🚀 Step 5: Activate the Virtual Environment
Activate the environment:
source /root/code/ml-env/bin/activateIf you're already inside /root/code, you can simply use:
source ml-env/bin/activateAfter activation, the terminal usually changes to something like:
(ml-env) root@server:/root/code#The (ml-env) prefix indicates that the virtual environment is active.
🧠 What Activation Does
Before activation:
python
↓
System Python
pip
↓
System packagesAfter activation:
python
↓
ml-env/bin/python
pip
↓
ml-env/bin/pipThis means packages installed using pip will belong to this environment rather than the global Python installation.
You can verify this with:
which pythonand:
which pipThey should point somewhere under:
/root/code/ml-env/bin/🤖 Step 6: Install ML Libraries
Install the required Machine Learning libraries:
pip install numpy pandas scikit-learn matplotlibThis installs four commonly used Python data science and ML packages.
NumPy
numpyUsed for:
Example:
import numpy as npPandas
pandasUsed for:
Example:
import pandas as pdScikit-learn
scikit-learnUsed for traditional Machine Learning tasks such as:
Python imports it using:
import sklearnMatplotlib
matplotlibUsed for:
Common import:
import matplotlib.pyplot as plt📦 Step 7: Check Installed Packages
You can inspect the packages installed in the environment:
pip listYou should find packages including:
numpy
pandas
scikit-learn
matplotlibAdditional dependencies will also appear because these libraries depend on other Python packages.
📝 Step 8: Generate requirements.txt
Save the installed dependencies:
pip freeze > requirements.txtHow It Works
pip freeze
│
│ installed package versions
▼
requirements.txtThe > operator redirects the output into the file.
Instead of printing:
numpy==...
pandas==...
scikit-learn==...
matplotlib==...to the terminal, it saves it to:
requirements.txt🔍 Step 9: Check requirements.txt
Display the contents:
cat requirements.txtYou should see package versions similar to:
matplotlib==...
numpy==...
pandas==...
scikit-learn==...There will normally be additional packages because pip freeze records the environment's installed dependencies.
🔄 Recreate the Environment
One major advantage of requirements.txt is reproducibility.
Another developer can create a virtual environment:
python3 -m venv ml-envActivate it:
source ml-env/bin/activateThen install the same dependencies:
pip install -r requirements.txtThe flow becomes:
requirements.txt
│
│ pip install -r
▼
Python Virtual Environment
│
├── numpy
├── pandas
├── scikit-learn
└── matplotlib📴 Deactivate the Environment
When finished working inside the environment:
deactivateThe (ml-env) prefix will disappear from the terminal.
This returns you to the system Python environment.
🕘 Check Command History
To review previously executed shell commands:
historyThis is useful for:
⚙️ Complete Workflow
pwd
python3 --version
python3 -m venv ml-env
ls -lah
ls ml-env
source /root/code/ml-env/bin/activate
pip install numpy pandas scikit-learn matplotlib
pip freeze > requirements.txt
cat requirements.txt
history⚠️ Important Notes
For Git projects, add it to .gitignore:
ml-env/The files you normally keep are:
project/
├── .gitignore
├── requirements.txt
└── source coderather than committing the entire virtual environment.
🧪 Validation Checklist
📌 Summary
Create the environment:
python3 -m venv ml-envActivate it:
source /root/code/ml-env/bin/activateInstall the Machine Learning packages:
pip install numpy pandas scikit-learn matplotlibSave dependencies:
pip freeze > requirements.txtThe complete concept is:
Python Project
│
│ python3 -m venv
▼
ml-env
│
│ activate
▼
Isolated Python Environment
│
│ pip install
▼
┌─────────────────────┐
│ numpy │
│ pandas │
│ scikit-learn │
│ matplotlib │
└─────────────────────┘
│
│ pip freeze
▼
requirements.txtThe key takeaway is:
> A Python virtual environment isolates project dependencies, while requirements.txt records those dependencies so the environment can be reproduced consistently.