Configuring Python Virtual Environments Across Operating Systems
Windows Configuration
Execute the following commands to acquire the necesary packages:
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:
C:\Python39\Scripts\virtualenvwrapper.bat
macOS and Linux Configuration
Install the required tools using the package manager:
python3 -m pip install virtualenv
python3 -m pip install virtualenvwrapper
Locate the shell script:
sudo find / -name virtualenvwrapper.sh
Copy the script to a universally accessible path:
sudo cp /opt/python3.9/bin/virtualenvwrapper.sh /usr/bin/
Append the environment configuration to your shell profile (~/.zshrc or ~/.bashrc):
export VENV_HOME="$HOME/py_venvs"
export WORKON_HOME=$VENV_HOME
source /usr/bin/virtualenvwrapper.sh
Reload the shell configuration:
source ~/.zshrc
Establish a symbollic link for the base executable:
sudo ln -s /opt/python3.9/bin/virtualenv /usr/bin/virtualenv
Common Operations
Provision a new isolated workspace using the default interpreter:
mkvirtualenv project_alpha
Provision a workspace targeting a specific interpreter version:
mkvirtualenv -p python3.8 project_beta
List all provisioned workspaces:
workon
Switch to an active workspace:
workon project_alpha
Install dependencies within the active workspace:
pip install requests
Terminate the active workspace session:
deactivate
Permanently remove a workspace (must be deactivated first):
rmvirtualenv project_alpha