liveBook · Manning

Chapter 6: Creating and managing Python virtual environments

What you will learn in chapter 6 of Build Applications with Local AI Models on a Mac

Introduces Python packages as reusable code and explains why virtual environments are a foundational practice before installing packages for the chatbot project.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces Python packages as reusable code and explains why virtual environments are a foundational practice before installing packages for the chatbot project.

Key terms: Python packages, virtual environments

6.1 Why virtual environments matter

This section explains why each Python project should have its own virtual environment before packages are installed.

Key terms: package, library, version, virtual environment, isolation, version conflict, reproducibility, cleanliness, virtual environment folder

6.2 Creating a virtual environment

6.2.1 Navigate to your project folder

6.2.2 Create the virtual environment

This subsection shows how to create a project-specific virtual environment, recognize its silent completion, and recover if creation fails.

Key terms: python3.13 -m venv .venv, .venv, virtual environment, VS Code’s integrated terminal, macOS Terminal, no such file or directory

6.2.3 Understanding the venv command

This section explains how to read the virtual environment creation command and why the book names its environment folder .venv.

Key terms: python3.13 -m venv .venv, venv module, .venv folder, .venv, .env

6.2.4 What is inside the venv folder?

This section explains the main contents of a virtual environment folder and why it resembles a separate Python installation inside a project.

Key terms: venv folder, bin/, lib/, pyvenv.cfg, system Python

6.3 Activating and deactivating a virtual environment

This section explains why creating a virtual environment is not enough: the terminal must be told to use it.

Key terms: virtual environment, activate, global installation of Python, system-wide

6.3.1 Activating the virtual environment

This section explains how activation makes terminal Python commands use the virtual environment and how the prompt shows that it is active.

Key terms: activation, environment variables, python3, pip, .venv, (.venv) prefix

6.3.2 Verifying activation

This subsection shows how to confirm that activation makes Python point to the project’s virtual environment rather than a system-wide installation.

Key terms: Python path, .venv folder, system-wide installation of Python

6.3.3 Deactivating the virtual environment

This section explains how to leave and return to a virtual environment and how to avoid using the wrong Python environment for a project.

Key terms: virtual environment, system installation of Python, (.venv) prefix, pip install, global Python installation, VS Code, .venv environment

6.4 Installing packages with pip

This section introduces pip as the standard Python package installer and notes that packages can be installed into an active virtual environment.

Key terms: pip, virtual environment, packages

6.4.1 What is pip?

This section explains where pip gets Python packages and why pip and pip3 are interchangeable inside an activated virtual environment.

Key terms: pip, Python Package Index, PyPI, Python packages, pip3, activated virtual environment, Python 3

6.4.2 Installing the Ollama Python package

This subsection explains why the Ollama Python library is needed to build a chatbot that uses a local Ollama server.

Key terms: Ollama Python library, Ollama server, scripts

6.4.3 Installing Streamlit

This section identifies Streamlit as the chatbot’s web-interface framework and explains why its many dependencies are normal and contained in the virtual environment.

Key terms: Streamlit, web interface, dependencies, virtual environment, venv folder

6.4.4 Checking the installed packages

This section shows that checking a virtual environment’s installed packages reveals both packages you installed directly and the dependencies they brought with them.

Key terms: virtual environment, dependencies, ollama, streamlit

6.5 Freezing and restoring dependencies

This section introduces requirements.txt as a way to record the packages and versions in a virtual environment so the setup can be shared or re-created.

Key terms: requirements.txt, virtual environment, package, version number

6.5.1 Creating the requirements.txt file

Explains how a requirements.txt file records the exact package versions in a Python environment so the environment can be re-created elsewhere.

Key terms: freezing, requirements.txt, ==

6.5.2 Restoring an environment from requirements.txt

This subsection explains how pip restores the packages and versions listed in requirements.txt.

Key terms: pip, -r flag, requirements.txt

6.5.3 Keeping requirements.txt updated

Explains how to keep a project's dependency list current and what to share instead of the virtual environment folder.

Key terms: freeze, requirements.txt, .venv, .gitignore, Git

6.5.4 The complete virtual environment workflow

6.6 Exercises

6.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Setting up VS Code and your Python development environment Chapter 7: Controlling LLMs with the Ollama Python library →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.