liveBook · Manning

Virtual environment

As explained in Build Applications with Local AI Models on a Mac

A Python virtual environment is an isolated environment with its own installed packages.

A virtual environment gives a Python project its own packages. Think of it as a separate toolbox: your chatbot can use one version of a package while another project uses a different version, without the two interfering. Create one before installing packages for a project. section 6.1

Figure 6.1 Without virtual environments (right), all projects share the same packages, which can cause version conflicts. With virtual environments (left), each project gets its own isolated set of packages, keeping everything clean and conflict-free.

Compare separate project toolboxes with one shared set of packages to see where version conflicts arise. section 6.1

Create and use an environment

From your project folder, run `python3.13 -m venv .venv`, then `source .venv/bin/activate` on macOS. The `(.venv)` prompt prefix shows that it is active. Now `pip install requests` installs the package into that environment. Run `deactivate` when you are done. Creating `.venv` may produce no output; check that the folder exists before trying to activate it. section 6.2.2 section 6.3.1 section 6.6

The first `venv` in the creation command names Python’s built-in module; `.venv` names the folder it creates. On macOS, that folder holds the environment’s Python and pip in `bin/` and installed packages under `lib/`. Creating the folder does not activate it: until activation, the terminal still uses the system-wide Python installation. section 6.2.3 section 6.2.4 section 6.3

Keep the setup reproducible and check it when something goes wrong:

  • Run `pip freeze > requirements.txt` to record installed packages and exact versions. Share that file, not `.venv`; after creating and activating a new environment, run `pip install -r requirements.txt` to restore them. section 6.7
  • If Python reports `ModuleNotFoundError` for Streamlit, check for `(.venv)` in the prompt, then check `which python3` and whether Streamlit appears in `python3 -m pip list`. You may be running a different environment from the one where you installed it. section 15.1.5

How Build Applications with Local AI Models on a Mac covers it

How deep it goes
Walks through creation, activation, package installation, dependency restoration, and checks; identifies folder contents without requiring inspection of them.
Examples
Uses a chatbot and web scraper with conflicting packages, plus exercises that rebuild an environment and compare two projects’ installed packages.
What it assumes
Assumes access to Python and a terminal, but teaches virtual-environment creation and activation step by step.
What you can do after
Operate/configure it with venv and pip to isolate packages, reproduce an installation, and diagnose a missing Streamlit package.
How it connects
Links package versions and isolation to pip, requirements.txt, terminal activation, and Python interpreter selection.

Also explained in

Compare how 7 books explain virtual environment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Applications with Local AI Models on a Mac. The section links point to the parts of the book it draws on.