Python ML Set Up Your Machine Learning Environment
A machine learning environment is a separate Python setup with the libraries installed. A virtual environment keeps those libraries apart from other projects.
Build the virtual environment
venv builds a folder named .mlvenv. The folder holds its own Python.
python3 -m venv .mlvenvSwitch the virtual environment on
Activate points the shell at the new Python. The prompt gains the folder name.
source .mlvenv/bin/activateInstall the libraries in one line
pip fetches seven libraries at once.
pip install numpy pandas scikit-learn matplotlib seaborn xgboost lightgbmCheck one library by hand
The check imports scikit-learn and prints its version. A version number means the install worked.
python -c "import sklearn; print(sklearn.__version__)"Example
import numpy
import pandas
import sklearn
import xgboost
import lightgbm
print("NumPy:", numpy.__version__)
print("pandas:", pandas.__version__)
print("scikit-learn:", sklearn.__version__)
print("XGBoost:", xgboost.__version__)
print("LightGBM:", lightgbm.__version__)Key points
- Virtual environment: a separate folder with its own Python and its own libraries
- venv: the tool that builds a virtual environment
- pip: the tool that installs Python libraries
- Library: a ready-made set of tools for one job, like NumPy for numbers
- Use Python 3.13. That is the version this course is written for.
- Install the libraries in one pip line. The line below installs NumPy, pandas, scikit-learn, Matplotlib, Seaborn, XGBoost, and LightGBM.
In this example, each import loads one library. Each print shows that library version. The five numbers prove the environment holds the pinned stack. A missing library would stop the program at its import line, so a clean run means the setup worked. The environment is one separate setup, and these five numbers show what lives inside it.
Note
The version numbers in the example come from the pinned stack. Your own numbers may be newer, and that is fine.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does pip do?