liveBook · Manning

Virtual environment

As explained in Python How-To

A virtual environment is an isolated directory tree containing a Python installation and project dependencies that are separate from the computer's other installations.

Need different package versions for two Python projects? Installing packages systemwide makes the projects share them: upgrading a package for one project may break the other. A virtual environment gives each project an isolated directory tree for its Python tools and dependencies, so each can use the package versions it needs. section 14.1.1

Create and activate an environment

From the project directory, run `$ python3 -m venv taskier-env` (on Windows, use `python` if needed). This creates a folder named `taskier-env`. Activate it with `source taskier-env/bin/activate` on macOS or `taskier-env\Scripts\activate.bat` on Windows. The environment name appearing in the command prompt signals that it is active. Name the environment so its project is easy to recognize. section 14.1.2 section 14.1.3

Install a project dependency

With the environment active, run `pip install streamlit==1.10.0` to install the version used for the task-management web app. Pinning that version helps reproduce the setup; the installation belongs to this environment rather than being shared across projects. section 14.1.3

In Visual Studio Code, open the project folder, run `Python: Select Interpreter`, and choose the environment’s interpreter. Check the status bar to verify the selected environment and Python version. Open the project folder itself if the environment does not appear in the interpreter list. section 14.1.4

Choose the tool according to what must vary. The standard-library `venv` module handles project-specific packages but uses the systemwide Python by default. If projects also need distinct Python versions, conda can create an environment with a specified interpreter and dependencies: `conda create -n taskier-env python=3.10.4 streamlit=1.10.0`. Activate it with `conda activate taskier-env`, then select its interpreter in Visual Studio Code. section 14.1.5

How Python How-To covers it

How deep it goes
Defines the directory-tree boundary and walks through creation, activation, installation, and editor selection; compares venv with conda for Python versions.
Examples
Uses a task-management app with Streamlit 1.10.0 and a practice task installing pandas in a separate environment.
What it assumes
Assumes access to a Python command line while teaching virtual-environment creation and package installation step by step.
What you can do after
Operate/configure a project environment by creating it, installing a dependency, and selecting its interpreter in Visual Studio Code.
How it connects
Connects virtual environments to pip, package version conflicts, Python interpreters, venv, conda, and Visual Studio Code.

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 Python How-To. The section links point to the parts of the book it draws on.