Skip to main content

Python environment setup

This guide gets your computer ready for the course, starting from nothing: you don’t need to have used a terminal or installed Python before. Do it before the first session, because the downloads are large. It takes about 30–45 minutes.

What you’ll install, all in one place (a conda environment called bda):

A single command in Step 4 installs everything except VS Code, which you install in Step 6. You don’t need to install Java or PySpark separately.

TipYou’re done when the check notebook shows all ✅

Setup isn’t finished when the installation ends: it’s finished when the check-setup notebook in Step 7 shows ✅ for every check.

Choose your path

New to Python

You’ve never installed Python, or you’re not sure. Follow every step, starting with Step 1.

Start at Step 1

Already have Anaconda or Miniforge

Already have Miniforge? Continue from Step 3. If you have Anaconda, install Miniforge too (Step 2).

Go to Step 2

Can’t install software

For example, a locked-down work laptop. Run the notebooks in your browser with Google Colab instead.

Use Google Colab

Before you start

  • Operating system: Windows 10 or 11, macOS 12 or later, or Linux.
  • Memory (RAM): 8 GB minimum.
  • Disk space: about 5 GB free, plus room for the datasets.
  • Admin rights: not needed. Miniforge and the course environment install into your own user folder. You only need admin rights for the optional WSL2 setup below.
  • Internet: a reasonable connection for the downloads (about 1–2 GB).

Everything in this course works on normal Windows. If you’re comfortable with Linux, you can instead use WSL2 (Ubuntu inside Windows, needs admin rights to install), where Spark runs a little more smoothly and writing files from Spark needs no extra step. If you do, follow the Linux instructions below inside your Ubuntu terminal.

The Module 4 Hadoop labs run on a shared cluster in Google Cloud, so there is nothing extra to install for them; see Hadoop cluster (Google Cloud Dataproc).


Step 1: Open a terminal

A terminal is a window where you type commands instead of clicking. You’ll use it for a few commands during setup, then mostly to start Jupyter.

You don’t need a terminal yet: Miniforge, installed in Step 2, comes with its own terminal, Miniforge Prompt. After Step 2, open it from the Start menu (type “Miniforge Prompt”). If you already have Anaconda, use Anaconda Prompt instead.

Use Miniforge Prompt (or Anaconda Prompt) for every command in this guide, not the ordinary Command Prompt or PowerShell.

Open Terminal: press Cmd + Space, type Terminal, and press Enter.

Open your Terminal app from the applications menu, or press Ctrl + Alt + T on most distributions.

A two-minute terminal primer

  • Where am I? The terminal always works “inside” a folder. The prompt usually shows which one.

  • Move into a folder with cd (“change directory”). cd bda goes into the folder called bda; cd .. goes up one folder:

    cd bda
  • List what’s in the folder: ls on macOS and Linux, dir on Windows.

  • Copy and paste commands from this page instead of typing them: use the copy button on each code box. To paste into the terminal, press Ctrl + V (Windows), Cmd + V (macOS) or Ctrl + Shift + V (Linux). Press Enter to run the command.

  • Where a step shows different commands for Windows and for macOS/Linux, run only the one for your computer.

  • If a command is still running, wait until the prompt comes back before typing the next one.

Step 2: Install conda

conda installs Python and packages and keeps each project’s packages in a separate environment.

We recommend installing Miniforge even if you already have Anaconda. It uses only the free conda-forge channel, avoids the very slow “Solving environment” problem, and the rest of this guide assumes its folder (miniforge3).

Install Miniforge, a small, free conda installer, as shown below.

  • Windows: both can be installed; use Miniforge Prompt (not Anaconda Prompt) for the course.
  • macOS/Linux: answer yes to “initialize conda”; Miniforge then becomes the default conda in new terminals.

The Miniforge installers for Windows, macOS and Linux are in the installers/ folder on the course USB drive. Copy the one for your computer and run it as described below.

  1. Go to https://conda-forge.org/download/ and download the Windows installer (Miniforge3-Windows-x86_64.exe).

  2. Double-click it and accept the defaults. Choose Just Me when asked; that doesn’t need admin rights.

  3. When it finishes, open Miniforge Prompt from the Start menu and check:

    conda --version

In Terminal, run these two commands one at a time. The first downloads the installer; the second runs it.

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh

Press Enter to scroll through the licence, type yes to accept it, press Enter to accept the install location, and answer yes when asked to initialize conda. Then close Terminal and open it again, and check:

conda --version

In a terminal, run:

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh

Accept the licence and the default location, and answer yes when asked to initialize conda. Close the terminal, open a new one, and check:

conda --version

If conda --version prints something like conda 25.x, conda is installed.

Anaconda’s own package channel requires a paid licence for organisations with 200 or more staff, which can include government agencies. Miniforge uses only the free conda-forge channel. If you already have Anaconda, you can use it for this course.

Slow or no internet? Install from the USB drive

If your internet connection is slow, install the course environment from the USB drive instead of downloading it. The drive’s installers/ folder has a ready-made copy of the environment for each operating system (bda-env-<os>).

No USB drive? Download your pack: Windows · macOS (Apple silicon) · macOS (Intel) · Linux.

ImportantThis replaces conda env create

Installing from the USB drive replaces Step 4 (conda env create -f environment.yml). Do one or the other, not both.

You need conda installed first (Step 2). If you don’t have the course files yet, also do Step 3.

  1. Copy bda-env-windows-x64.zip from the USB drive’s installers/ folder to your Downloads folder.

  2. Open Miniforge Prompt and create the environment folder:

    mkdir %USERPROFILE%\miniforge3\envs\bda
  3. Extract the pack into it:

    tar -xf %USERPROFILE%\Downloads\bda-env-windows-x64.zip -C %USERPROFILE%\miniforge3\envs\bda
  4. Activate the environment and finish the installation:

    conda activate bda
    conda-unpack
  5. Register the environment as a kernel:

    conda activate bda
    python -m ipykernel install --user --name bda --display-name "Python (bda)"

    This makes the course environment show up as Python (bda) in VS Code and JupyterLab.

  1. Check which Mac you have: Apple menu → About This Mac. If Chip says Apple M…, use bda-env-macos-arm64.tar.gz; if Processor says Intel, use bda-env-macos-intel.tar.gz. Copy that file from the USB drive’s installers/ folder to your Downloads folder.

  2. In Terminal, create the environment folder:

    mkdir -p ~/miniforge3/envs/bda
  3. Extract the pack into it (use macos-intel instead of macos-arm64 on an Intel Mac):

    tar -xzf ~/Downloads/bda-env-macos-arm64.tar.gz -C ~/miniforge3/envs/bda
  4. Activate the environment and finish the installation:

    conda activate bda
    conda-unpack
  5. Register the environment as a kernel:

    conda activate bda
    python -m ipykernel install --user --name bda --display-name "Python (bda)" --env JAVA_HOME "$CONDA_PREFIX/lib/jvm"

    This makes the course environment show up as Python (bda) in VS Code and JupyterLab.

  1. Copy bda-env-linux-x64.tar.gz from the USB drive’s installers/ folder to your Downloads folder.

  2. In a terminal, create the environment folder:

    mkdir -p ~/miniforge3/envs/bda
  3. Extract the pack into it:

    tar -xzf ~/Downloads/bda-env-linux-x64.tar.gz -C ~/miniforge3/envs/bda
  4. Activate the environment and finish the installation:

    conda activate bda
    conda-unpack
  5. Register the environment as a kernel:

    conda activate bda
    python -m ipykernel install --user --name bda --display-name "Python (bda)" --env JAVA_HOME "$CONDA_PREFIX/lib/jvm"

    This makes the course environment show up as Python (bda) in VS Code and JupyterLab.

conda-unpack takes a minute and prints nothing when it succeeds. The start of your prompt changes to (bda). If you installed Anaconda rather than Miniforge, use anaconda3 instead of miniforge3 in the paths above.

Then skip Step 4 and continue at Step 5 on Windows, or at Step 6: Install VS Code on macOS and Linux.

Step 3: Get the course files

  1. Download the course files: BDA-with-Python-main.zip.

  2. Unzip it. You get a folder called BDA-with-Python-main.

  3. Rename that folder to bda and move it somewhere with a short path and no spaces:

    • Windows: C:\bda
    • macOS / Linux: your home folder, so it becomes ~/bda
  4. In your terminal, go into the folder.

    Windows:

    cd C:\bda

    macOS / Linux:

    cd ~/bda
  5. Check you’re in the right place: dir (Windows) or ls (macOS/Linux) should list environment.yml, notebooks and README.md.

If you use git, clone the repository into a short path with no spaces, then go into it.

Windows:

git clone https://github.com/worldbank/BDA-with-Python.git C:\bda
cd C:\bda

macOS / Linux:

git clone https://github.com/worldbank/BDA-with-Python.git ~/bda
cd ~/bda

With git you can get later updates to the course with git pull.

WarningAvoid spaces and special characters in the path

Spark and Java often fail on paths such as C:\Users\Jean Dupont\Mes documents\cours. Keep the course folder at C:\bda or ~/bda.

Step 4: Create the course environment

This step installs Python, Jupyter, Java 17, PySpark and all the course packages into one environment called bda, using the environment.yml file in the course folder. You don’t need to install Java or PySpark separately. Java is installed automatically with the environment. You don’t need to download it from Oracle or create any account. It takes 5–15 minutes, depending on your connection.

Installed the environment from the USB drive? Skip this step.

In your terminal, inside the course folder (Step 3), run:

conda env create -f environment.yml

Wait until the prompt comes back and you see a message about how to activate the environment. Then activate it:

conda activate bda

The start of your prompt changes from (base) to (bda). Then register the environment as a kernel:

Windows:

conda activate bda
python -m ipykernel install --user --name bda --display-name "Python (bda)"

macOS / Linux:

conda activate bda
python -m ipykernel install --user --name bda --display-name "Python (bda)" --env JAVA_HOME "$CONDA_PREFIX/lib/jvm"

This makes the course environment show up as Python (bda) in VS Code and JupyterLab.

Need another package later? With the environment active, run conda install -c conda-forge <package>, or pip install <package> if it isn’t on conda-forge.

Step 5: Windows only: set up Spark for Windows

Spark on Windows needs two small helper files (winutils.exe and hadoop.dll) and a few settings so that VS Code, JupyterLab and the terminal all find Java and Spark. You do this once, and it doesn’t need admin rights. macOS and Linux users skip this step.

Plug in the course USB drive first if you have it: the commands copy the two files from it automatically.

Close VS Code and JupyterLab. Open PowerShell (Start menu → type PowerShell), paste the whole block below, and press Enter.

& {
# ============================================================
# Where things are
# ============================================================
$envDir = "$env:USERPROFILE\miniforge3\envs\bda"
$course = "C:\bda"
if (-not (Test-Path "$envDir\python.exe")) {
    Write-Host "Course environment not found at $envDir. Finish Step 4 first." -ForegroundColor Red
    return
}

# ============================================================
# Hadoop helper files: C:\hadoop\bin\winutils.exe and hadoop.dll
# ============================================================
New-Item -ItemType Directory -Force C:\hadoop\bin | Out-Null
$have = (Test-Path C:\hadoop\bin\winutils.exe) -and (Test-Path C:\hadoop\bin\hadoop.dll)

if (-not $have) {
    # Look on the USB drive (any drive letter) and in the course folder
    $sources = @()
    foreach ($d in (Get-PSDrive -PSProvider FileSystem).Root) {
        $sources += "${d}installers", "${d}installers\hadoop\bin"
    }
    $sources += "$course\tools\hadoop\bin"

    foreach ($src in $sources) {
        if ((Test-Path "$src\winutils.exe") -and (Test-Path "$src\hadoop.dll")) {
            Copy-Item "$src\winutils.exe", "$src\hadoop.dll" C:\hadoop\bin -Force
            Write-Host "Copied Hadoop files from $src"
            $have = $true
            break
        }
    }
}

if (-not $have) {
    # Last resort: download them
    $base = "https://raw.githubusercontent.com/kontext-tech/winutils/master/hadoop-3.4.0-win10-x64/bin"
    curl.exe -L -o C:\hadoop\bin\winutils.exe "$base/winutils.exe"
    curl.exe -L -o C:\hadoop\bin\hadoop.dll   "$base/hadoop.dll"
}
Unblock-File C:\hadoop\bin\*

# ============================================================
# Save the settings permanently for this user (no admin needed)
# ============================================================
[Environment]::SetEnvironmentVariable("JAVA_HOME",  "$envDir\Library\lib\jvm", "User")
[Environment]::SetEnvironmentVariable("SPARK_HOME", "$envDir\Lib\site-packages\pyspark", "User")
[Environment]::SetEnvironmentVariable("HADOOP_HOME", "C:\hadoop", "User")
[Environment]::SetEnvironmentVariable("PYSPARK_PYTHON", "$envDir\python.exe", "User")
[Environment]::SetEnvironmentVariable("PYSPARK_DRIVER_PYTHON", "$envDir\python.exe", "User")
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*C:\hadoop\bin*") {
    [Environment]::SetEnvironmentVariable("Path", "$userPath;C:\hadoop\bin", "User")
}

# ============================================================
# Check
# ============================================================
Get-ChildItem C:\hadoop\bin | Select-Object Name, Length
Write-Host "Done. Close and reopen PowerShell, Miniforge Prompt, VS Code and JupyterLab." -ForegroundColor Green
}

You should see hadoop.dll and winutils.exe listed and a green Done. The settings only apply to programs opened after this step, so close and reopen everything.

  • “Course environment not found”: finish Step 4 first. If you use Anaconda instead of Miniforge, change miniforge3 to anaconda3 in the $envDir line near the top.
  • Antivirus blocks the files: copy winutils.exe and hadoop.dll by hand from the USB drive’s installers folder into C:\hadoop\bin, then run the block again.
  • Spark later fails with UnsatisfiedLinkError … NativeIO$Windows.access0: install the Microsoft Visual C++ Redistributable (x64), restart, and try again.

Step 6: Install VS Code

Visual Studio Code (VS Code) is a free editor from Microsoft. It is the course’s code editor. Running notebooks in VS Code is optional: if a notebook fails in VS Code but works in the browser (Step 7a), use the browser.

The VS Code installers for Windows, macOS and Linux are in the installers/ folder on the course USB drive. Copy the one for your computer and run it as described below.

  1. Download and install VS Code from https://code.visualstudio.com.

    • Windows: choose the User Installer; it doesn’t need admin rights. Accept the defaults.
    • macOS: open the downloaded file and drag Visual Studio Code into Applications.
    • Linux: install the .deb or .rpm package for your distribution.

    The first time VS Code opens, it may ask you to sign in or offer GitHub Copilot (an AI assistant). You don’t need either for this course: close the Chat panel and continue without signing in. We recommend not using Copilot during the course so you can focus on learning the code yourself.

    Optional tip: to hide the AI features completely, open Settings (Ctrl+, on Windows/Linux, Cmd+, on macOS), search for disable AI features, and tick it.

  2. Install the Python extension (by Microsoft). Open the Extensions view (Ctrl+Shift+X on Windows and Linux, Cmd+Shift+X on macOS), search for Python and install it.

  3. Install the Jupyter extension (by Microsoft). This is required: without it, VS Code can’t open notebooks or let you choose the course environment.

  4. Open the course folder: File → Open Folder, then choose your course folder (C:\bda or ~/bda).

  5. Select the bda environment as the Python interpreter. Press Ctrl+Shift+P (Cmd+Shift+P on macOS), type Python: Select Interpreter, and choose the one with bda in its name.

  6. Select the notebook kernel. Open a notebook, click Select Kernel (top right) → Jupyter Kernels → Python (bda). Alternative: Python Environments → bda.

If bda doesn’t appear: check the Jupyter extension is installed, check you ran the ipykernel install command from Step 4, then press Ctrl+Shift+P (Cmd+Shift+P) → Developer: Reload Window.

Step 7: Test your setup

Setup isn’t finished until this step shows all ✅. Most problems show up here, not during installation, so don’t skip it.

7a. Run the check notebook in your browser (recommended)

  1. Open Miniforge Prompt (Windows) or Terminal (macOS/Linux).

  2. Activate the environment, go to the course folder and start JupyterLab.

    Windows:

    conda activate bda
    cd C:\bda
    jupyter lab

    macOS / Linux:

    conda activate bda
    cd ~/bda
    jupyter lab
  3. A browser tab opens. Keep the terminal window open while you work; closing it stops Jupyter.

  4. In the file list on the left, open notebooks → 00-intro-to-python → 00-check-setup.ipynb (view it on this site).

  5. Choose Run → Run All Cells. Every line should show ✅.

jupyter notebook also works if you prefer the classic interface.

We recommend the browser for this check: it always uses the fully activated course environment, so Java and Spark are found, and there are no AI assistants to distract you.

Every check prints ✅ or ❌, and the last cell tells you whether you’re ready. Each ❌ comes with a one-line fix.

Success looks like: ✅ on every line, and a last cell that says you’re ready for the course. Some WARN lines from Spark (e.g. about native-hadoop libraries) are normal and can be ignored.

The Spark UI: the notebook ends with a short “See Spark in action” cell. While it runs, open http://localhost:4040 to see the Spark UI; we use it in class.

7b. Or run it in VS Code (optional)

  1. In VS Code, with the course folder open (Step 6), open notebooks → 00-intro-to-python → 00-check-setup.ipynb in the Explorer on the left.
  2. Check that the kernel name in the top right shows bda. If not, click it and choose Jupyter Kernels → Python (bda).
  3. Click Run All at the top of the notebook. Running everything takes a minute or two.

Step 8: Get the datasets

The datasets are too large for the repository, and the notebooks expect them in the data/raw/ folder of your course folder. The Course datasets page says where to get each one, how to copy them, and what each file contains.


ImportantEvery time you work on the course

Open your terminal (Miniforge Prompt on Windows) and run these three commands. On Windows, the second one is cd C:\bda.

conda activate bda
cd ~/bda
jupyter lab

Optional, in VS Code: open VS Code; it reopens your course folder, with bda as the kernel.

Troubleshooting

WarningJava version matters

The most common error in this course is the wrong Java version. PySpark 4 needs Java 17 or later, and JAVA_HOME must point to it. Older setup guides say “Java 7 or above”; that is no longer true.

First, check Java. The course environment contains Java 17, and conda’s openjdk package sets JAVA_HOME automatically whenever you run conda activate bda. Check that java -version reports openjdk version "17...", and that JAVA_HOME is a path inside your conda environment.

macOS / Linux / WSL2:

conda activate bda
java -version
echo $JAVA_HOME

Windows (Miniforge Prompt or Anaconda Prompt):

conda activate bda
java -version
echo %JAVA_HOME%

Then find your error message:

Error message (or symptom) Likely cause Fix
conda: command not found / “not recognized” conda isn’t on the path of this terminal On Windows, use Miniforge Prompt or Anaconda Prompt rather than Command Prompt; on macOS/Linux, close the terminal and open a new one after installing.
JAVA_HOME is not set / Java gateway process exited before sending its port number Java missing, or JAVA_HOME not set Use the course environment (conda activate bda, then start Jupyter), or install Java manually. Open a new terminal.
UnsupportedClassVersionError / class file version 61.0 Java too old (8 or 11) Use the course environment, or install Java 17 and point JAVA_HOME to it. Check with java -version.
Works in a terminal but not in a notebook The notebook uses a different kernel/environment In VS Code, click the kernel name (top right) and choose Python Environments → bda. In JupyterLab, choose the kernel from the bda environment, or start jupyter lab after conda activate bda.
Windows: HADOOP_HOME and hadoop.home.dir are unset, winutils.exe, NativeIO$Windows Missing Hadoop helper files Do Step 5, or use WSL2.
Check notebook fails in VS Code (Java or Spark not found) but works in the browser VS Code isn’t using the activated course environment Use the browser (Step 7a); in VS Code, make sure you chose Python (bda) and reopened VS Code after Step 5.
WinError 2 / “The specified file cannot be found” when starting Spark Step 5 not done (or done before reopening VS Code) Run Step 5, then close and reopen VS Code.
UnsatisfiedLinkError … NativeIO$Windows.access0 hadoop.dll missing or blocked Run Step 5 again; install the Visual C++ Redistributable (x64).
Python worker failed to connect back (Windows) Spark picks a different Python Set PYSPARK_PYTHON=python in your environment variables, or use WSL2.
PySpark warns it doesn’t fully support pandas ≥ 3, or a Spark↔︎pandas conversion fails The environment has pandas 3 or later Run conda install -n bda "pandas<3".
Address already in use / Spark UI on port 4041 Another Spark session is still running Run spark.stop() in the other notebook, or restart its kernel.
OutOfMemoryError: Java heap space Default driver memory is too small for the data Add .config("spark.driver.memory", "4g") to the SparkSession.builder.
Paths with spaces or non-English characters fail Spark/Hadoop path handling Keep the course folder in a simple path, e.g. C:\bda or ~/bda.

Still stuck? Open an issue on the course repo with your operating system, the command you ran, java -version output, and the full error message.

Alternatives and advanced

Open a notebook in Colab (each course notebook has an Open in Colab badge) and run in the first cell:

!pip install -q "pyspark>=4.0,<5"

If Spark complains about the Java version, also run:

!apt-get install -y -qq openjdk-17-jdk-headless > /dev/null
import os
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-17-openjdk-amd64"

Colab sessions reset after inactivity, so you’ll repeat this each time, and you’ll need to upload datasets or mount Google Drive.

Install Docker Desktop, then from the course folder run:

docker run -it --rm -p 8888:8888 -p 4040:4040 \
  -v "$PWD":/home/jovyan/work \
  quay.io/jupyter/pyspark-notebook

Open the http://127.0.0.1:8888/lab?token=... link that’s printed. Your course folder is under work/. Java and Spark are already set up inside the container.

On Windows PowerShell, replace "$PWD" with ${PWD} and the \ line breaks with backticks, or put everything on one line.

Skip this if you created the bda environment in Step 4: it already contains Java 17 and PySpark. Installing a second Java can make things worse.

What PySpark needs:

Component Version Notes
Python 3.10 or later The course environment uses 3.11
Java (JDK) 17 or later PySpark 4 does not run on Java 8 or 11
PySpark 4.x Installed with conda or pip
  1. Install Java 17. Download the free Eclipse Temurin JDK 17 for your OS from https://adoptium.net/temurin/releases/?version=17 (no account needed) and run the installer.

    • macOS alternative: brew install --cask temurin@17
    • Ubuntu/WSL2 alternative: sudo apt install openjdk-17-jdk
  2. Set JAVA_HOME to the JDK folder.

    • macOS: add export JAVA_HOME=$(/usr/libexec/java_home -v 17) to ~/.zshrc, then open a new terminal.
    • Linux/WSL2: add export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 (check the folder name with ls /usr/lib/jvm) to ~/.bashrc.
    • Windows: the Temurin installer has an option “Set JAVA_HOME variable”; tick it. Otherwise: Start → Edit the system environment variables → Environment Variables → New (System variables) → name JAVA_HOME, value e.g. C:\Program Files\Eclipse Adoptium\jdk-17.x.x-hotspot. Then edit Path and add %JAVA_HOME%\bin. Open a new terminal.
  3. Install PySpark in your Python environment:

    pip install "pyspark>=4.0,<5"

    If you use conda, prefer conda install -c conda-forge pyspark rather than mixing pip into a conda environment.

name: bda
channels:
  - conda-forge
dependencies:
  - python=3.11
  - openjdk=17            # Java for Spark; sets JAVA_HOME when the env is activated
  - pyspark>=4.0,<5
  - jupyterlab
  - notebook
  - pandas
  - numpy
  - pyarrow
  - matplotlib
  - seaborn
  - plotly
  - scikit-learn
  - requests
  - beautifulsoup4
  - nltk
  - dask
  - geopandas             # used by src/mob_data_utils.py
  - pip
  - pip:
      - wikipedia
      - pandas-datareader