Module 02

So you want to tame virtual environments

Environments are how we stop one experiment, plugin, or notebook setup from quietly sabotaging another a week later.

Estimated time: 25 to 35 min Prerequisite: Python installed locally

Why environments matter

Biology research workflows often mix plotting libraries, notebook tools, imaging packages, and domain-specific plugins. Installing everything into one global Python can make it hard to tell which package version belongs to which project.

An environment is a dedicated workspace for one project or one tutorial path. It gives you a cleaner mental model and makes it easier to share steps with collaborators.

A useful rule of thumb is: if a workflow matters enough to revisit, it matters enough to keep its dependencies isolated.

Choose the route that fits your needs

venv

Good when you want the simplest standard-library approach and you are mainly installing Python packages with pip.

conda

Helpful when you want a broader environment manager, especially for scientific software stacks or GUI-heavy tools where binary dependencies matter.

A gentle recommendation

If everything here feels brand new, it is reasonable to understand the idea with venv first and only then learn conda as a second route.

Teaching tip

It is often easier to present one recommended route first, then include the alternative for readers who already know they need it. For this course, venv is the lightest starting point and conda is a strong option for napari-focused setups.

Create an environment with venv

python -m venv sywtdiawp-env
source sywtdiawp-env/bin/activate
python -m pip install --upgrade pip

On Windows PowerShell, activation is commonly:

sywtdiawp-env\Scripts\Activate.ps1

To leave the environment:

deactivate

What these commands mean

  • The first command creates the environment folder.
  • The activation command changes your shell so installs go into that environment.
  • Upgrading pip early helps reduce package-install friction later.

Common follow-up commands

python -m pip install numpy pandas matplotlib
python -m pip list
deactivate

Create an environment with conda

conda create -n sywtdiawp python=3.11
conda activate sywtdiawp

To leave the environment:

conda deactivate

Conda can feel heavier than venv, but many researchers find it helpful once projects involve broader scientific software stacks.

Common follow-up commands

conda install numpy pandas matplotlib
conda list
conda deactivate

Confirm which Python you are using

python --version
python -c "import sys; print(sys.executable)"

The executable path should point inside your environment folder or your conda environment. This is one of the most useful checks when something feels off.

Beginners often skip this check, but it is one of the fastest ways to diagnose why a package seems installed yet still will not import.

Suggested environment strategy for this course

  1. Use one general learning environment for Jupyter notebooks.
  2. Create a separate environment for napari if GUI dependencies become messy.
  3. Document the commands you used in a text file or notebook markdown cell.
  4. Do not install unrelated packages “just in case.” Keep environments focused.

Practice activity

  1. Create one environment using the method you prefer.
  2. Activate it and run python --version.
  3. Print the interpreter path with sys.executable.
  4. Deactivate the environment and note what changed in the shell.

Helpful verification snippet

python -c "import sys; print(sys.executable)"
python -c "import site; print(site.getsitepackages())"

The executable path is the most important line here. It tells you exactly which Python you are talking to.

Common mistakes

  • Installing packages before activating the environment.
  • Forgetting which terminal window still has the environment active.
  • Mixing global Python, venv, and conda in the same walkthrough without telling the learner.
  • Assuming a notebook kernel uses the same environment as the current shell without checking.

Stop here for today if needed

This is a good place to pause once you can create, activate, and deactivate an environment without guessing. That repetition is more valuable than rushing to the next module.

Ready for the next module

  • You can create and activate an environment with at least one method.
  • You can identify which Python executable the environment is using.
  • You know whether you want to use the same environment for notebooks and napari or separate ones.