+91 81900 98289

Python VirtualEnv

This Python VirtualEnv lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A virtual environment is a private folder of Python and packages for one project. python -m venv .venv creates it.

Activate it before pip install. Each project then keeps its own versions, and an upgrade in one folder does not break another.

Example 1: Start here

Commit requirements.txt, not the .venv folder.

print("python -m venv .venv") # print writes this; the quotes are not shown print("Then activate the environment and run pip.")

Example 2: Read a fake environment name

A virtual environment is a folder that holds one project’s packages.

import sys # load a tool that comes with Python print(sys.prefix.endswith("venv") or True) # and needs both tests; or needs one print("Create one with: python -m venv .venv")

Example 3: Pick an interpreter

After you activate the environment, python means the interpreter inside that folder.

folder = ".venv" # text print(folder + "/bin/python")

Example 4: Keep the project list

Install project packages only after the environment is active, so they do not mix with other projects.

packages = ["requests"] # a list; the values stay in this order print("Install inside the environment:", ", ".join(packages)) # join the pieces into one text
Commit requirements.txt, not the .venv folder.
Call Now 81900 98289