Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
CHAPTER GUIDE · GETTING STARTED

Virtual environments

On this pageCreate a project environmentUse it for the projectLeave the environmentHow this relates to venv

Installing a package changes the environment in which it is installed. If two projects need different package versions, sharing one environment can create conflicts. A separate project environment keeps one project’s requirements from changing another’s.

An environment is not the project folder: the folder holds your program files, while the environment supplies Python and its installed packages.

Create a project environment

For the Anaconda route used in this book, conda can create an isolated environment with its own Python and packages. In Anaconda Prompt on Windows or the configured Terminal on macOS/Linux, enter:

conda create --name book-practice python numpy
Listing 41 — Create a named environment containing Python and NumPy.

Review the proposed installation and confirm it. book-practice is the environment’s name; it does not create or replace your project’s source folder. Naming the packages in the command ensures that this new environment has them, regardless of what is installed in base.1

Use it for the project

  1. Activate the environment in the terminal:
conda activate book-practice
Listing 42 — Select the project environment for terminal commands.
  1. Check the selected Python and NumPy:
python -c "import sys; print(sys.executable)"
python -c "import numpy; print(numpy.__version__)"
Listing 43 — Check the interpreter location and installed NumPy version.

The path should identify book-practice; the second command should display a version number rather than an import error.

  1. In VS Code, use Python: Select Interpreter and choose book-practice for the open project. Terminal activation and editor selection must agree about which Python the project uses.

For a notebook, select the matching Python kernel—the process that executes its cells. Selecting an interpreter for saved files does not change a notebook that is already running with another kernel.

Leave the environment

conda deactivate
Listing 44 — Leave the active conda environment.

This changes the terminal’s active environment; it does not delete the environment or the program files. Activate book-practice again when returning to the project.

How this relates to venv

Python also provides venv, a tool that creates an environment from an existing Python installation. Its files commonly live in a project’s .venv folder. It serves the same isolation goal, but it is created and activated differently from a conda environment. Use one approach for a project; the commands above follow the book’s conda route.2

With the environment chosen, both saved scripts and interactive sessions can use the same intended packages.


  1. conda, Managing environments. Creation, activation and deactivation checked on 22 October 2026.↩︎

  2. Python documentation, venv — Creation of virtual environments.↩︎