Farewell to Dependency Chaos: Installation and Usage of Python Virtual Environment virtualenv

In Python development, version conflicts of dependencies across different projects (e.g., Project A requires Django 1.11 while Project B requires 2.2) often lead to "dependency chaos." Installing packages globally may overwrite library files, causing runtime errors. Virtual environments solve this by creating isolated Python environments for each project, each with its own interpreter and dependencies that do not interfere with others. Virtualenv is a commonly used lightweight open-source tool. Before installation, ensure Python and pip are already installed. Execute `pip install virtualenv` to install it. To create a virtual environment, navigate to the project directory and run `virtualenv venv` (where `venv` is the environment name and can be customized). This generates a `venv` folder containing the isolated environment. Activation of the virtual environment varies by operating system: - On Windows CMD: Use `venv\Scripts\activate.bat` - For PowerShell, set the execution policy first before running the activation script - On Mac/Linux: Execute `source venv/bin/activate` After activation, the command line prompt will show `(venv)`, indicating the virtual environment is active. Dependencies installed via `pip` in this state are isolated to the environment and can be verified with `pip list`. To export dependencies, use `pip freeze > requirements.txt`, allowing others to quickly install the same environment via `pip install -r requirements.txt`. To exit the environment, use `deactivate`. Deletion of the virtual environment folder directly removes the entire environment.

Read More