Skip to main content

Setting Up a Standardized Python ML Environment



Requirements

The xFusionCorp Industries data science team needs a standardised Python environment for their new ML project. Set up a virtual environment with the required ML libraries on the controlplane host.

Create a Python virtual environment named ml-env under /root/code/ using python3 -m venv.

Activate the environment and install the following packages: numpy, pandas, scikit-learn, and matplotlib.

Generate a requirements.txt file using pip freeze and save it at /root/code/requirements.txt.


Note

This document focuses on the implementation approach. The task statement above is preserved as the original requirement source.

Prerequisites

  • Access to the controlplane host with a user that has sufficient privileges to create directories under /root.
  • Python 3 and the venv module must already be installed.
  • Internet access or repository access must be available for downloading Python packages.
  • The working directory /root/code will be used for the virtual environment and requirements file.

Steps

# Create the working directory if it does not already exist
mkdir -p /root/code

# Create the Python virtual environment
python3 -m venv /root/code/ml-env

# Activate the virtual environment
source /root/code/ml-env/bin/activate

# Install required machine learning libraries
pip install numpy pandas scikit-learn matplotlib

# Generate the requirements.txt file
pip freeze > /root/code/requirements.txt

Verification

Validate that the virtual environment and required packages were configured successfully.

Verify the virtual environment exists

ls -ld /root/code/ml-env

Expected result:

/root/code/ml-env

Activate the environment

source /root/code/ml-env/bin/activate

Expected shell prompt:

(ml-env)

Verify installed packages

pip list

Expected packages include:

  • numpy
  • pandas
  • scikit-learn
  • matplotlib

Test Python package imports

python3 -c "import numpy, pandas, sklearn, matplotlib; print('All packages imported successfully')"

Expected output:

All packages imported successfully

Verify the generated requirements file

cat /root/code/requirements.txt

Expected output contains package entries similar to:

numpy==<version>
pandas==<version>
scikit-learn==<version>
matplotlib==<version>

Confirm the active Python interpreter

which python

Expected output:

/root/code/ml-env/bin/python

Resources