Back to Blog
Lesson 2 of the AI/ML Foundations: Core Concepts & First Models course
AI/MLJune 24, 20264 min read

Setting Up the Python ML Environment: A Practical Guide

Learn how to configure your Python environment for machine learning. We cover Anaconda/venv installation, library verification, and launching Jupyter Notebooks.

PythonJupyterenvironmentsetupmachine learningaimachine-learning

Previously in this course, we explored The Machine Learning Workflow: From Data to Deployment to understand the high-level lifecycle of an AI project. Now, it’s time to move from theory to practice by building your local development environment.

A robust machine learning environment acts as the foundation for every experiment you'll run. If your tools aren't configured correctly, you’ll spend more time debugging your installation than training models. In this lesson, we will establish your workspace using standard industry tools.

Why Isolated Environments Matter

In professional software engineering, we never install libraries globally. If Project A requires NumPy 1.20 and Project B requires NumPy 1.26, a global installation will break one of them. We use isolated environments to ensure that each project has its own specific dependencies, preventing "dependency hell."

For this course, we have two primary options for environment management: Anaconda (or its lightweight version, Miniconda) or standard Python venv.

Option 1: Anaconda (Recommended for Data Science)

Anaconda is the industry standard for data science because it handles both Python packages and non-Python dependencies (like C++ libraries) seamlessly.

  1. Download the Miniconda installer for your operating system.
  2. Run the installer and follow the prompts.
  3. Open your terminal (or Anaconda Prompt on Windows) and verify the installation by typing conda --version.

Option 2: venv (Standard Pythonic Approach)

If you prefer keeping your system lightweight, use the built-in venv module.

  1. Create a project folder: mkdir ml-course && cd ml-course
  2. Initialize the environment: python -m venv .venv
  3. Activate it:
    • macOS/Linux: source .venv/bin/activate
    • Windows: .venv\Scripts\activate

Installing the Core ML Stack

Close-up of multiple computer CPUs stacked on a wooden surface, showcasing technology components.

Once your environment is active, we need to install the "Big Three" libraries that power almost every machine learning workflow: NumPy, Pandas, and Scikit-Learn.

Run the following command in your terminal:

Bash
pip install numpy pandas scikit-learn jupyter
  • NumPy: The foundation for numerical computing in Python.
  • Pandas: Provides the DataFrame structure for data manipulation.
  • Scikit-Learn: The industry-standard library for traditional machine learning algorithms.
  • Jupyter: An interactive interface for running code in blocks, which is essential for data exploration and visualization.

Verifying Your Setup

Never assume an installation worked—always verify it. Create a file named verify_env.py and add the following script:

PYTHON
import numpy as np
import pandas as pd
import sklearn

print(f"NumPy version: {np.__version__}")
print(f"Pandas version: {pd.__version__}")
print(f"Scikit-Learn version: {sklearn.__version__}")
print("Environment setup verified successfully!")

Run this with python verify_env.py. If you see the version numbers printed without errors, your Python environment is ready.

Launching Jupyter

The Jupyter notebook environment is where we will conduct our exploratory data analysis. To launch it, run the following command in your terminal within your project directory:

Bash
jupyter notebook

Your default browser should open to a local URL (usually http://localhost:8888). Create a new notebook by clicking "New" -> "Python 3". In the first cell, type print("Hello, ML!") and press Shift + Enter to execute. If the cell runs and displays the output, you are successfully configured.

Hands-on Exercise

  1. Create a new folder named ml-project-root on your machine.
  2. Set up an isolated environment inside it (using either conda or venv).
  3. Install the libraries mentioned above.
  4. Launch Jupyter, create a notebook, and import pandas as pd. Create a simple dataframe with one column of numbers and display it.

Common Pitfalls

Close-up of a warning sign against swimming due to deep holes, surrounded by dry branches.

  • Wrong Interpreter: The most common issue is running pip install while your virtual environment is not active. Always check your command prompt for the (.venv) or (base) prefix before installing packages.
  • Path Conflicts: If you have multiple versions of Python installed, ensure your terminal is using the one you think it is by running which python (macOS/Linux) or where python (Windows).
  • Kernel Mismatch: If you launch Jupyter and it says "ModuleNotFoundError," it usually means you installed the libraries in your terminal but launched Jupyter from a different Python installation. Always install Jupyter inside the environment you intend to use.

By completing this setup, you have moved past the initial friction of development. You now have a stable, reproducible environment that will serve as the sandbox for the rest of this course.

Up next: Introduction to NumPy for Data Handling.

Similar Posts