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 Python virtual environment gives a project its own set of installed packages. Think of it as a separate toolbox: a chatbot can use one version of a package while another project uses a different version, without the two installations conflicting. Create an environment before installing a project’s packages. 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 environments with a shared set of packages. section 6.1

An environment is a folder on disk. On macOS, its bin folder contains its Python executable and pip installer, while installed packages live under lib. The pyvenv.cfg file identifies the directory as a virtual environment. section 6.2.4

Create and use an environment

In a terminal, move into your project folder, then run: python3.13 -m venv .venv source .venv/bin/activate pip install requests The first command uses Python’s built-in venv module to create a folder named .venv. Use .venv rather than .env, which is often used for secrets and configuration. Creation normally returns to the prompt without output; wait for it to finish before activating. On macOS, activation changes the terminal’s settings so Python and pip use the environment. A (.venv) prefix in the prompt signals that it is active. Run deactivate when you are finished. section 6.2.2 section 6.2.3 section 6.3.1 section 6.7

To share or rebuild the setup, run pip freeze > requirements.txt while the environment is active. This records installed packages and exact versions. Share requirements.txt rather than the .venv folder; in a newly created and activated environment, run pip install -r requirements.txt to install from the file. section 6.7 section 6.6

Check which environment you are using

If Python reports that it cannot find Streamlit, it may be running outside the environment where Streamlit was installed. Activate the project’s environment, then run which python3 and python3 -m pip list | grep streamlit to check the interpreter path and package. If Streamlit is missing, install it with pip install streamlit. Run the app with python3 -m streamlit run app.py to use the same Python interpreter. section 15.1.5

Try comparing two projects: install requests in one environment and flask in the other. Activate each in turn and run pip list to see how their packages differ. section 6.6

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

How deep it goes
Walks through creation, activation, package installation, recording and rebuilding dependencies, and troubleshooting, with an overview of environment folder contents.
Examples
Uses requests and flask in separate projects, a requirements-file rebuild, and a missing Streamlit import to demonstrate the workflow.
What it assumes
Uses terminal navigation and teaches environment creation and activation step by step.
What you can do after
Operate/configure a virtual environment using venv and pip, share its requirements, rebuild it, and check for interpreter mismatches.
How it connects
Connects package version conflicts and dependency management with pip, requirements files, 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.