You copy a machine learning example from a tutorial, run it, and get ModuleNotFoundError: No module named 'sklearn'. Before you train a model, you need a place to run code with the right libraries installed. To set up a machine learning environment in Python, you have two good options: Google Colab, which runs in your browser with nothing to install, and a local setup on your own computer with a virtual environment, Jupyter and scikit-learn.
Both take under 20 minutes. Start with Colab if you want to begin right now, and set up locally when you start real projects.
What you need first: basic Python, from how to install Python and set up VS Code and the Python course.
Key terms
- Library (or package): code someone else wrote that you install and import, such as pandas or scikit-learn.
- pip: Python's tool for installing libraries from PyPI, the public package index.
- Virtual environment: a separate folder of installed libraries for one project, so projects can't break each other.
- Notebook: a document of code cells that you run one at a time, with each cell's output shown below it.
- Kernel: the Python process that runs a notebook's code; it decides which installed libraries the notebook can see.
The tools to set up Python for machine learning
| Tool | What it does |
|---|---|
| Python | The language everything runs on |
| NumPy | Fast arrays and maths |
| pandas | Tables of data: load, clean, filter, group |
| Matplotlib and Seaborn | Charts |
| scikit-learn | Classic machine learning algorithms, metrics and data tools |
| Jupyter | Notebooks: code, results and charts together |
Later, for deep learning, you'll add PyTorch or TensorFlow. You don't need them yet.
Option 1: Google Colab (no installation)
Colab is a free Jupyter notebook service from Google that runs in your browser.
- Go to colab.research.google.com and sign in with a Google account.
- Click New notebook.
- Type this in the first cell and press Shift + Enter to run it:
import numpy, pandas, sklearn, matplotlib
print("NumPy", numpy.__version__)
print("pandas", pandas.__version__)
print("scikit-learn", sklearn.__version__)
NumPy 2.3.2
pandas 2.3.1
scikit-learn 1.7.2
You'll see version numbers like these; yours may be newer. __version__ (two underscores each side) is a value most libraries store with their version number. Note that you install the library as scikit-learn but import it as sklearn.
All the main ML libraries come preinstalled. Colab also offers a free GPU, via Runtime › Change runtime type, which you'll want for deep learning later.
Limitations: sessions disconnect after a period of inactivity, files you upload disappear when the session ends unless you save them to Google Drive, and free GPU time is limited.
Option 2: a local setup
Step 1: install Python
If Python isn't installed yet, follow how to install Python and set up VS Code. Check it in a terminal:
python --version
Use python3 on macOS and Linux.
Step 2: create a project folder and a virtual environment
A virtual environment keeps each project's libraries separate, so upgrading a library for one project can't break another.
mkdir ml-course
cd ml-course
python -m venv .venv
Then activate it:
| System | Command |
|---|---|
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
| Windows (Command Prompt) | .venv\Scripts\activate.bat |
| macOS and Linux | source .venv/bin/activate |
Your prompt now starts with (.venv). Activate it every time you open a new terminal for this project.
Step 3: install the libraries
python -m pip install --upgrade pip
python -m pip install numpy pandas scikit-learn matplotlib seaborn jupyterlab
Step 4: save your library list
python -m pip freeze > requirements.txt
requirements.txt lists exact versions. Anyone, including future you on a new laptop, can recreate the environment with python -m pip install -r requirements.txt.
Step 5: start Jupyter
jupyter lab
JupyterLab opens in your browser. Click Python 3 under Notebook to create a notebook.
Prefer VS Code? Install the Python and Jupyter extensions, create a file ending in .ipynb, and choose your .venv as the kernel in the top-right corner.
Check that everything works
Run this in a notebook cell. It trains a real model on a dataset that ships with scikit-learn:
Download the data: iris.csv (4 KB), or from the original source, the UCI Machine Learning Repository. The code below loads it for you, so you only need the file to open it in a spreadsheet or work offline: load it with
pd.read_csv("iris.csv").
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model = DecisionTreeClassifier(random_state=42).fit(X_train, y_train)
print("Accuracy:", model.score(X_test, y_test))
Accuracy: 1.0
If you see an accuracy, your machine learning environment is ready. (A perfect score is normal on this tiny, easy dataset. Real data is never this kind.)
train_test_splitholds back some flowers for testing; with notest_size, it keeps 25% aside.random_state=42makes the random split the same on every run, so your result matches this page..fit(...)returns the trained model itself, which is whyDecisionTreeClassifier(...).fit(...)can be written on one line.
Predict, then run: change random_state=42 to random_state=7 in train_test_split only. Will the accuracy stay at 1.0?
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=7)
model = DecisionTreeClassifier(random_state=42).fit(X_train, y_train)
print("Accuracy:", round(model.score(X_test, y_test), 3))
Accuracy: 0.921
It drops to 0.921: 3 of the 38 test flowers are now wrong. A different random split puts different flowers in the test set, and the score moves. Neither number is "the" accuracy: each is one estimate from one split. That's why later lessons use cross-validation, which averages over several splits. Experiment: try three or four other random_state values and write down the scores to see how much they vary.
A tidy project structure
ml-course/
├── .venv/ # the virtual environment (don't edit or commit)
├── data/ # raw CSV files
├── notebooks/ # exploration
├── src/ # reusable .py code
├── models/ # saved trained models
└── requirements.txt
If you use Git, add .venv/ to .gitignore, and usually data/ too: datasets are often large or private.
Do I need a GPU?
Not yet. Classic machine learning with scikit-learn runs comfortably on any laptop from the last several years, using the processor. You'll want a GPU for deep learning on images, audio or large text models, and Colab's free GPU covers learning. Don't buy hardware before you need it.
Alternatives you'll hear about
- Anaconda / Miniconda: a different installer that bundles Python and data libraries and manages environments with
conda. Popular in data science, especially for libraries that are hard to install with pip. - uv: a newer, very fast tool that replaces pip and venv. Worth trying once you're comfortable.
- Kaggle Notebooks: like Colab, free in the browser, with Kaggle's datasets built in.
All of them work. Pick one and get on with learning: the setup matters far less than practice.
Fixing common problems
| Problem | Fix |
|---|---|
ModuleNotFoundError: No module named 'sklearn' | Install scikit-learn (not sklearn) into the active environment: python -m pip install scikit-learn |
| Installed a library, but the notebook can't find it | The notebook kernel uses a different Python. In VS Code, select the .venv kernel; in Jupyter, start it from the activated environment |
| PowerShell says running scripts is disabled | Run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, or use Command Prompt and activate.bat |
jupyter is not recognised | The environment isn't active, or JupyterLab isn't installed in it |
| Plots don't appear | In a script, end with plt.show(); notebooks show them automatically |
Practice
- Create a virtual environment called
.venv, activate it, and install pandas. How can you check it's installed?
Show the answer
python -m venv .venv
.venv\Scripts\Activate.ps1 # Windows; macOS/Linux: source .venv/bin/activate
python -m pip install pandas
python -m pip show pandas
pip show prints the version and location. python -m pip list lists everything installed.
- A classmate runs the iris example and gets a different accuracy from yours. Name two possible reasons.
Show the answer
They may have used a different random_state (or none), so their test set holds different flowers. Or they have a different scikit-learn version, which can occasionally change results. Fixing random_state and saving versions in requirements.txt make results reproducible.
- Your notebook says
No module named 'pandas', butpip install pandassays it's already installed. What's going on?
Show the answer
pip installed pandas into a different Python from the one the notebook uses. Select the right kernel (your .venv), or run %pip install pandas inside the notebook, which installs into the notebook's own Python.
- Why install with
python -m pip install ...rather than justpip install ...?
Show the answer
python -m pip runs the pip that belongs to the Python you're about to use, which is the active virtual environment's Python. A bare pip command can belong to a different Python on your computer, so the library gets installed somewhere your code can't see it.
- You share your project folder with a friend, including
.venv. It doesn't work on their laptop. What should you have shared instead?
Show the answer
requirements.txt (and your code), not .venv. A virtual environment is tied to the computer and Python it was made on. Your friend creates their own with python -m venv .venv and installs the same versions with python -m pip install -r requirements.txt.
Interview questions
Why use a virtual environment?
To isolate each project's dependencies. Different projects can need different library versions, and a virtual environment prevents conflicts and makes the setup reproducible with a requirements file.
What is the difference between pip and conda?
pip installs Python packages from PyPI into an existing Python. conda is a package and environment manager that can also install Python itself and non-Python dependencies, such as C libraries, from its own channels.
What is a Jupyter kernel?
The process that runs a notebook's code. Each kernel is tied to one Python environment, so the libraries available in a notebook are those installed in its kernel's environment.
Key takeaways
- Google Colab is the fastest start: free, in the browser, with ML libraries and a GPU.
- Locally, use a virtual environment per project, and install with
python -m pip. - The core stack is NumPy, pandas, Matplotlib, scikit-learn and Jupyter.
- Save your library versions in
requirements.txt. - You don't need a GPU until deep learning.
Frequently asked questions
Should I use Anaconda or pip for machine learning?
Either works. pip with a virtual environment is lighter and is the standard Python way; Anaconda or Miniconda helps with libraries that are hard to build on some systems. For this course's libraries, pip is enough.
Is Google Colab good enough for learning machine learning?
Yes. It has every library this course uses already installed and a free GPU for deep learning later. Its limits are that sessions time out and uploaded files disappear unless you save them to Google Drive.
Which Python version should I use for machine learning?
A recent stable version that your libraries support, such as Python 3.12 or 3.13. The newest release sometimes takes a few months to be supported by every library.
What to learn next
- See what a model actually receives in data, features and labels.
- Walk through a complete project in the machine learning workflow.
- Brush up on data handling with the pandas DataFrame tutorial.