Configuring Python Virtual Environments Across Operating Systems
Windows Configuration
Execute the following commands to acquire the necessary packages:
bash python -m pip install virtualenv python -m pip install virtualenvwrapper-win
Define the directory for storing environments via system environment variables: Navigate to System Properties -> Environment Variables -> New System Variable.
- Variable Name:
WORKON_HOME - Variable Value:
D:\PythonVenvs
Apply the changes by executing the batch script located in the Scripts directory of your Python installation:
cmd
C:\Python39\Scripts\virtualenvwrapper.bat
macOS and Linux Configuration
Install the required tools using the package manager: bash python3 -m pip installl virtualenv python3 -m pip install virtualenvwrapper
Locate the shell script: bash sudo find / -name virtualenvwrapper.sh
Copy the script to a universally accessible path: bash sudo cp /opt/python3.9/bin/virtualenvwrapper.sh /usr/bin/
Append the environment configuration to your shell profile (~/.zshrc or ~/.bashrc):
bash
export VENV_HOME="$HOME/py_venvs"
export WORKON_HOME=$VENV_HOME
source /usr/bin/virtualenvwrapper.sh
Reload the shell configuration: bash source ~/.zshrc
Establish a symbolic link for the base executable: bash sudo ln -s /opt/python3.9/bin/virtualenv /usr/bin/virtualenv
Common Operations
Provision a new isolated workspace using the default interpreter: bash mkvirtualenv project_alpha
Provision a workspace targeting a specific interpreter version: bash mkvirtualenv -p python3.8 project_beta
List all provisioned workspaces: bash workon
Switch to an active workspace: bash workon project_alpha
Install dependencies within the active workspace: bash pip install requests
Terminate the active workspace session: bash deactivate
Permanently remove a workspace (must be deactivated first): bash rmvirtualenv project_alpha