Virtual Environment Setup
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.
By completing this module you will understand the complete workflow:
- pyenv: How to have multiple Python versions installed (3.10, 3.11, 3.12) without conflicts.
- venv: How to create an isolated space for each project using one of those versions.
- 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:
- pyenv (The Land): You choose what foundation to build on. Do you want to use Python 3.10 or Python 3.12?
pyenvallows you to have both available and choose one for each job. - 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).
- pip (The Furniture): You have an empty house. Now you use
pipto bring in the furniture (libraries likerequests,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
endpyenv: 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.
- Open PowerShell as Administrator.
- 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- Close and reopen the terminal for changes to take effect.
- Verify it works:
pyenv --version
On Unix systems, we use the automatic installer.
- Open your terminal and run:
1curl https://pyenv.run | bash- Configure your shell (add this to your
.bashrcor.zshrc):
1export PYENV_ROOT="$HOME/.pyenv"
2[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
3eval "$(pyenv init -)"- Restart the terminal.
- 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).
- See what you can install:
1pyenv install --list - Install a specific version (for example, 3.11.5):
1pyenv install 3.11.5 - Set it for your current project:
Navigate to your project folder and run:
This creates a hidden
1pyenv local 3.11.5.python-versionfile. Now, every time you enter this folder, pyenv will magically switch yourpythoncommand 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
-
Make sure you’re in your project folder (where you did the
pyenv local). -
Run the command to create the environment:
1python -m venv .venv(Note:
.venvis just the folder name. You can call it whatever you want, but.venvis the standard). -
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/activateYou’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_nameFor example, if you need requests for web requests:
1pip install requestsThis 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.
-
Freeze the current state: Once your project works, save the list of everything you’ve installed:
1pip freeze > requirements.txtThis creates a text file with the exact shopping list.
-
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.
Imagine you’re asked for a project that must work with Python 3.10 and needs the pandas library.
Follow these steps in order:
-
Prepare the ground (pyenv):
- Create a folder
data-analyzerand 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 --versionsays 3.10.11.
- Create a folder
-
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.
- Create the virtual environment:
-
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.
- Update pip (always a good idea):
-
Cleanup:
- Exit the virtual environment:
deactivate. - Try doing
pip listnow. You’ll see that pandas is no longer there! That’s the magic of isolation.
- Exit the virtual environment:
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: