Python Virtual Environment Setup for ML Project
Objective
Set up a standardized Python virtual environment for a machine learning project and generate a dependency file for reproducibility.
Requirements
-
Create a Python virtual environment named
ml-env. -
Install the following packages:
numpypandasscikit-learnmatplotlib
-
Generate a
requirements.txtfile usingpip freeze.
Prerequisites
Verify Python 3 is installed:
python3 --version
Expected output:
Python 3.x.x
Step 1: Create the Working Directory
mkdir -p /root/code
Verify:
ls -ld /root/code
Step 2: Create the Virtual Environment
Create a virtual environment named ml-env under /root/code.
python3 -m venv /root/code/ml-env
Verify:
ls /root/code
Expected output:
ml-env
Step 3: Activate the Virtual Environment
source /root/code/ml-env/bin/activate
Your shell prompt should now display:
(ml-env)
Verify the Python interpreter:
which python
Expected output:
/root/code/ml-env/bin/python
Step 4: Install Required ML Libraries
Install the required machine learning and data analysis packages:
pip install numpy pandas scikit-learn matplotlib
Verify installed packages:
pip list
Example output:
Package Version
-------------- -------
numpy x.x.x
pandas x.x.x
scikit-learn x.x.x
matplotlib x.x.x
Step 5: Generate requirements.txt
Create a dependency snapshot using pip freeze.
pip freeze > /root/code/requirements.txt
Verify:
cat /root/code/requirements.txt
Example output:
numpy==2.x.x
pandas==2.x.x
scikit-learn==1.x.x
matplotlib==3.x.x
...
Validation Checklist
Verify Virtual Environment
ls -ld /root/code/ml-env
Verify Installed Packages
pip show numpy pandas scikit-learn matplotlib
Verify Requirements File
ls -l /root/code/requirements.txt
Complete Command Sequence
mkdir -p /root/code
python3 -m venv /root/code/ml-env
source /root/code/ml-env/bin/activate
pip install numpy pandas scikit-learn matplotlib
pip freeze > /root/code/requirements.txt
Expected Directory Structure
/root/code
├── ml-env/
│ ├── bin/
│ ├── lib/
│ └── pyvenv.cfg
└── requirements.txt
Common Issues
requirements.txt Not Found
Incorrect:
cat requirment.txt
Correct:
cat requirements.txt
Virtual Environment Not Activated
Check:
which python
If the output does not point to:
/root/code/ml-env/bin/python
activate the environment again:
source /root/code/ml-env/bin/activate
Empty requirements.txt
Ensure packages are installed before running:
pip freeze > requirements.txt
Verify package installation:
pip list