Isolating Python Projects for Reproducible Medical Imaging Workflows

Author
Admin
Last updated
Aug 25, 2026

Difficulty: Beginner

Reading Time: 20–25 minutes

Prerequisites:

  • Python installed
  • Visual Studio Code installed
  • Basic command-line knowledge

Learning Objectives

After completing this tutorial, you will be able to:

  • Understand what a virtual environment is.
  • Learn why virtual environments are important.
  • Create and activate virtual environments.
  • Install packages inside an environment.
  • Configure Visual Studio Code to use a virtual environment.
  • Build reproducible Python projects for medical imaging.

Introduction

As your Python skills grow, you’ll start working on multiple projects. One project may require NumPy 2.x, while another depends on an older version. Without proper isolation, installing or updating packages for one project can accidentally break another.

A virtual environment solves this problem by creating an isolated Python installation for each project. Every project has its own packages, versions, and dependencies, making your work easier to maintain and share.

Whether you’re developing an AI model, processing DICOM images, or performing radiomics analysis, virtual environments are considered a best practice.


Why Use Virtual Environments?

Imagine you’re working on two different projects:

Project A

  • NumPy 2.1
  • Matplotlib 3.10
  • pydicom 3.0

Project B

  • NumPy 1.26
  • TensorFlow 2.15
  • MONAI 1.4

If all packages are installed globally, upgrading one library could break the other project.

A virtual environment keeps each project independent.


How Virtual Environments Work

Python

 Project A                                                                 Project B

             venv                                                                 venv

      NumPy 2.1                                                        NumPy 1.26

        pydicom                                                           TensorFlow

        Matplotlib                                                       MONAI

Each project uses its own Python environment without affecting the others.


Why This Matters in Medical Imaging

Medical imaging projects often require many specialized libraries.

Example:

Medical Imaging Project

├ pydicom

├ NumPy

├ SimpleITK

├ scikit-image

├ pandas

├ Matplotlib

├ PyTorch

└── MONAI

Keeping these dependencies isolated makes your projects reproducible and easier to share with colleagues.


Creating Your First Virtual Environment

Open a terminal in your project folder.

Example:

MedicalImagingProjects/

Run:

python -m venv .venv

This creates a folder named:

.venv

containing a separate Python installation.


Activating the Environment

Windows

.venv\Scripts\activate

macOS/Linux

source. venv/bin/activate

The terminal prompt changes to indicate that the environment is active.

Example:

(.venv) C:\MedicalImagingProjects>


Installing Packages

Once the environment is active, install packages using pip.

pip install numpy

pip install matplotlib

pip install pydicom

These packages are installed only inside the current environment.


Verifying the Installation

Open Python:

python

Then test:

import numpy as np
import pydicom
print(np. __version__)
print (pydicom. __version__)

If no errors occur, the packages were installed successfully.


Using Virtual Environments in Visual Studio Code

  1. Open your project folder in VS Code.
  2. Press Ctrl + Shift + P.
  3. Select Python: Select Interpreter.
  4. Choose the interpreter located in your. venv folder.

Example:

.venv/Scripts/python.exe

VS Code will now use your project’s isolated environment.


Saving Project Dependencies

When sharing your project, you don’t share the .venv folder. Instead, you create a list of installed packages.

Run:

pip freeze > requirements.txt

This creates:

requirements.txt

Example:

numpy==2.3.1

matplotlib==3.10.1

pydicom==3.0.1

Another user can recreate the environment with:

pip install -r requirements.txt


Typical Project Structure

CT_Image_Analysis/

├ venv/

├ data/

├ CT/

├ MRI/

├ notebooks/

├ analysis. Ipynb

├ scripts/

├ load_dicom.py

├requirements.txt

├README.md

├gitignore

Note: The .venv folder should not be uploaded to GitHub.


Common Beginner Mistakes

Installing Packages Before Activating the Environment

Packages are installed globally instead of inside the project.

Solution: Always activate the environment first.


Forgetting Which Interpreter VS Code Uses

If VS Code uses the global interpreter, installed packages may appear to be missing.

Solution: Select the. venv interpreter from the Command Palette.


Committing. venv to Git

The environment can contain thousands of files.

Solution: Add. venv/ to your. gitignore file.


Deleting requirements.txt

Without it, collaborators may not know which package versions your project requires.

Solution: Update requirements.txt whenever dependencies change.


Best Practices

✔ Create one virtual environment per project.

✔ Name it. venv for consistency.

✔ Keep the environment inside the project folder.

✔ Commit requirements.txt to version control.

✔ Ignore. venv in Git.

✔ Document installation steps in your README.md.


Exercises

Exercise 1

Create a folder named:

CT_Image_Analysis

Create a virtual environment inside it.


Exercise 2

Activate the environment.


Exercise 3

Install:

  • NumPy
  • Matplotlib
  • pydicom

Exercise 4

Create a script named:

check_environment.py

Add:

import numpy as np

import pydicom

print (“NumPy:”, np. __version__)

print(“pydicom:”, pydicom.__version__)

Run the script to verify the installation.


Exercise 5

Generate requirements.txt file using:

pip freeze > requirements.txt

Open the file and identify the installed package versions.


Summary

In this tutorial, you learned:

  • What virtual environments are.
  • Why should every Python project have its own environment.
  • How to create and activate a virtual environment.
  • How to install packages safely.
  • How to configure Visual Studio Code.
  • How to create a requirements.txt file for reproducible projects.

By using virtual environments, you’ll avoid dependency conflicts and make your medical imaging projects easier to reproduce and collaborate on.


Leave a Reply

Your email address will not be published. Required fields are marked *