Quick Start: Python Virtual Environment, Initial set up & activation

I'm starting a bioinformatics team project soon, and whenever you begin a new project, it's a good idea to set up a designated virtual environment.

So, here’s a quick guide for my teammates—and for you, whoever’s reading this!


Why Use a Virtual Environment? (Skip if you want)

Case 1: Dependency Management

As our project grows, we'll likely use a lot of Python packages. These packages often come with complex dependencies—meaning package A needs B, B needs C, and so on.

If dependencies conflict, we might face unknown errors that take hours to fix. Instead of wasting time debugging, wouldn’t it be nice to delete everything and start fresh? A virtual environment makes that easy.

Case 2: Version Control

Not all projects use the same package versions, and not all packages stay up to date. Virtual environments let us keep separate package setups for different projects without breaking anything.

There are more reasons, but let’s just see how to set one up


How to Initialize, Activate, and Deactivate a Virtual Environment

0. Open Terminal / Shell

  • Windows: Press Win + R, type cmd, then hit Enter.
  • Mac: Search for "Terminal" in Launchpad.

1. Check if Python is Installed

Type the following in your terminal:

python --version python3 --version

If you see a version number (e.g., Python 3.x.x), you're good to go.

If you get an error, Python might not be installed. You’ll need to install it first (I'll cover that in another post).


2. Create a Virtual Environment

2-a. (Optional) Navigate to Your Project Folder

Do you know how to navigate the file system in your terminal? If not, don't worry. Totally fine. This step is optional, and I’ll write a separate article on it.

2-b. Initialize a Virtual Environment

In the terminal, run:

python -m venv myvenv

 

The last word (myvenv) is your virtual environment’s name. I named it myvenv because I hate calling it just "venv". It's too confusing in tutorials.


3. Activate the Virtual Environment

Before running your project or installing packages, you must activate the virtual environment. Think of it like opening a door and stepping into a special workspace.

3-a. Navigate to Your Virtual Environment's Folder (If you skipped 2-a, also skip it)

Go to the location where you created myvenv.

3-b. Run the Activation Command

Windows:


.\myvenv\Scripts\activate

Mac/Linux:


source myvenv/bin/activate

If activation is successful, you’ll see your terminal prompt change—something like this:


        (myvenv) $


That means you’re inside the virtual environment! 🎉


4. Deactivate the Virtual Environment

When you're done working, type:



deactivate

 


Now you're back to normal.

You can see (myvenv) has gone.



Now you know how to set up, activate, and deactivate a virtual environment

If you have any questions or got stuck, feel free to ask in the comments. 


Happy Bioinformatics! 


Comments