Virtual Environment Setup

When to study this module

Now that you already know the basic syntax, this is a good time to learn how to work with a clean and reproducible environment. If you want to keep practicing pure language first, you can come back later, but this is where the course starts to feel more professional.

Module Objectives

By completing this module you will understand the complete workflow:

  1. pyenv: How to have multiple Python versions installed (3.10, 3.11, 3.12) without conflicts.
  2. venv: How to create an isolated space for each project using one of those versions.
  3. pip: How to install libraries inside that isolated space.

The Professional Workflow

Before diving into commands, it’s crucial to understand how these three pieces fit together. Many tutorials teach them separately, but they’re designed to be used together.

Imagine you’re a house builder:

  1. pyenv (The Land): You choose what foundation to build on. Do you want to use Python 3.10 or Python 3.12? pyenv allows you to have both available and choose one for each job.
  2. venv (The House): Once you’ve chosen the land (the Python version), you build an isolated house (the virtual environment). What happens inside the house, stays in the house. If you break something inside, it doesn’t affect the neighbor’s house (another project).
  3. pip (The Furniture): You have an empty house. Now you use pip to bring in the furniture (libraries like requests, pandas, django) that you need specifically for that house.
graph TD
    subgraph PC ["Your Computer"]
        style PC fill:#f9f9f9,stroke:#333,stroke-width:2px

        subgraph Pyenv ["pyenv Manager"]
            style Pyenv fill:#e1f5fe,stroke:#0277bd
            P310["Python 3.10"]
            P312["Python 3.12"]
        end

        subgraph Proy1 ["Project A"]
            style Proy1 fill:#e8f5e9,stroke:#2e7d32
            Env1["Virtual Environment (.venv)"]
            Lib1["Libraries: Django 3.2"]
        end

        subgraph Proy2 ["Project B"]
            style Proy2 fill:#fff3e0,stroke:#ef6c00
            Env2["Virtual Environment (.venv)"]
            Lib2["Libraries: Django 4.0"]
        end

        P310 -->|Creates| Env1
        P312 -->|Creates| Env2
        Env1 -->|Uses pip for| Lib1
        Env2 -->|Uses pip for| Lib2
    end

pyenv: Managing Multiple Versions

In the real world, you’ll work on old projects that need Python 3.8 and new projects that use Python 3.12. Installing and uninstalling versions manually is chaos. pyenv solves this by allowing you to install multiple versions side by side.

Installing pyenv

On Windows, we use pyenv-win.

  1. Open PowerShell as Administrator.
  2. Run these commands to install it:
1Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
2Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; ./install-pyenv-win.ps1
  1. Close and reopen the terminal for changes to take effect.
  2. Verify it works: pyenv --version

On Unix systems, we use the automatic installer.

  1. Open your terminal and run:
1curl https://pyenv.run | bash
  1. Configure your shell (add this to your .bashrc or .zshrc):
1export PYENV_ROOT="$HOME/.pyenv"
2[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
3eval "$(pyenv init -)"
  1. Restart the terminal.
  2. Verify: pyenv --version

How to use pyenv

The flow is simple: You install a version and then decide where to use it Globally (entire PC) or Locally (just this folder).

  1. See what you can install:
    1pyenv install --list
  2. Install a specific version (for example, 3.11.5):
    1pyenv install 3.11.5
  3. Set it for your current project: Navigate to your project folder and run:
    1pyenv local 3.11.5
    This creates a hidden .python-version file. Now, every time you enter this folder, pyenv will magically switch your python command to use version 3.11.5.

venv: Creating the Isolated Space

Now that pyenv has selected the correct Python version for your folder, the next step is to create the virtual environment.

Think of the virtual environment as a .venv folder that contains a small copy of that Python you selected, ready to receive libraries. If you don’t use venv, libraries would be installed in your system’s global Python, mixing with those from other projects and causing conflicts.

Steps to create the environment

  1. Make sure you’re in your project folder (where you did the pyenv local).

  2. Run the command to create the environment:

    1python -m venv .venv

    (Note: .venv is just the folder name. You can call it whatever you want, but .venv is the standard).

  3. IMPORTANT! Activate the environment: Creating it isn’t enough; you have to “enter” it.

1.venv\Scripts\Activate.ps1

(If you see a security error, run: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser)

1source .venv/bin/activate

You’ll know you’re inside because your terminal will show (.venv) at the beginning of the line. Until you see this, don’t install anything.


pip: Furnishing Your Environment

Now you’re inside your virtual environment (.venv). It’s an empty room. It’s time to use pip to install the tools (libraries) your project needs.

The magic command

1pip install package_name

For example, if you need requests for web requests:

1pip install requests

This package will be saved inside the .venv/Lib/site-packages folder, completely isolated from your system.

Best Practices: requirements.txt

A professional project must be reproducible. If your colleague downloads your code, they must be able to install the same libraries with the same versions.

  1. Freeze the current state: Once your project works, save the list of everything you’ve installed:

    1pip freeze > requirements.txt

    This creates a text file with the exact shopping list.

  2. Install from the list: When someone (or yourself on another PC) wants to set up the environment, they just need to do:

    1pip install -r requirements.txt

Integration Exercise: All Together

Let’s simulate a real project from scratch to consolidate the flow: pyenv + venv + pip.

Exercise: ‘Data Analyzer’ Project

Imagine you’re asked for a project that must work with Python 3.10 and needs the pandas library.

Follow these steps in order:

  1. Prepare the ground (pyenv):

    • Create a folder data-analyzer and enter it.
    • Make sure you have Python 3.10 installed (pyenv install 3.10.11).
    • Set the local version for this folder: pyenv local 3.10.11.
    • Check that python --version says 3.10.11.
  2. Build the house (venv):

    • Create the virtual environment: python -m venv .venv.
    • Activate it (find the command for your system above).
    • Make sure you see (.venv) in your terminal.
  3. Furnish (pip):

    • Update pip (always a good idea): python -m pip install --upgrade pip.
    • Install the library: pip install pandas.
    • Check what was installed: pip list.
    • Generate the registry file: pip freeze > requirements.txt.
  4. Cleanup:

    • Exit the virtual environment: deactivate.
    • Try doing pip list now. You’ll see that pandas is no longer there! That’s the magic of isolation.

Workflow Command Summary

Step Tool Typical Command
1 pyenv pyenv local 3.X.X (Choose project version)
2 python python -m venv .venv (Create isolated environment)
3 Shell source .venv/bin/activate (Enter the environment)
4 pip pip install package (Install tools)
5 pip pip freeze > requirements.txt (Save state)

Quiz

Test your understanding of the workflow:

🎮 Quiz: Virtual Environment

0 / 0
Loading questions...

Previous: Fundamentals Next: Data Structures