2  Python Environment

2.1 Purpose

A Python environment determines which Python interpreter and packages a project uses.

2.2 Inspect the current interpreter

import sys

print(sys.executable)
print(sys.version)
/home/isaias/analisis_datos/python_book/.venv/bin/python3
3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0]

2.3 Inspect the current working directory

from pathlib import Path

print(Path.cwd())
/home/isaias/analisis_datos/python_book/foundations

2.4 Create a virtual environment

Run this in the terminal, not in a Python code chunk:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip

In order to reinstall your environment you can create a requirements.txt that is a file that have a list of libraries you use in the project.

python -m pip freeze > requirements.txt

And later you can reinstall your environment with this:


python -m pip install -r requirements.txt

2.5 Things to remember

  • A virtual environment isolates project dependencies.
  • VS Code must use the same interpreter as the environment.
  • python -m pip makes the relationship between Python and pip explicit.