{"id":310,"library":"virtualenv","title":"virtualenv","description":"virtualenv is a tool for creating isolated Python environments, allowing developers to manage dependencies for different projects separately. The current version is 21.2.0, with regular updates to enhance functionality and maintain compatibility with Python versions.","status":"active","version":"21.2.0","language":"python","source_language":"en","source_url":"https://github.com/pypa/virtualenv","tags":["python","virtualenv","environment","dependency management"],"install":[{"cmd":"pip install virtualenv","lang":"bash","label":"Install virtualenv"}],"dependencies":[],"imports":[],"quickstart":{"code":"import os\n\n# Create a virtual environment\nos.system('virtualenv myenv')\n\n# Activate the virtual environment\nif os.name == 'nt':\n    os.system('.\\\\myenv\\\\Scripts\\\\activate')\nelse:\n    os.system('source myenv/bin/activate')\n\n# Install a package within the virtual environment\nos.system('pip install requests')\n\n# Deactivate the virtual environment\nos.system('deactivate')","lang":"python","description":"This script demonstrates how to create, activate, use, and deactivate a virtual environment using virtualenv."},"warnings":[{"fix":"Review and update custom scripts to align with the new discovery module structure.","message":"virtualenv 21.0.0 refactored the discovery module, which may affect custom scripts relying on internal APIs.","severity":"breaking","affected_versions":"21.0.0 and later"},{"fix":"Avoid using the --relocatable option; consider other methods for creating portable environments.","message":"The --relocatable option is experimental and may not work in all circumstances.","severity":"deprecated","affected_versions":"All versions"},{"fix":"Use the -p option to specify the desired Python interpreter when creating a virtual environment.","message":"Creating a virtual environment without specifying the Python version may result in using an unintended interpreter.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure PYTHONPATH is unset or correctly configured when working within a virtual environment.","message":"System-wide PYTHONPATH settings can interfere with virtualenv's isolation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Recreate the virtual environment in the new location instead of moving it.","message":"Moving a virtual environment directory can break its functionality due to absolute paths.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that the shell executing the activation command supports `source` (e.g., Bash, Zsh). If a minimal shell is used, consider explicitly calling the Python interpreter from the virtual environment's `bin` directory (e.g., `/path/to/venv/bin/python`) or ensure the `PATH` is correctly updated manually, or switch to a compatible shell for activation.","message":"Activation or deactivation of the virtual environment fails with 'sh: 1: source: not found' or 'sh: 1: deactivate: not found' errors, indicating that the shell's `source` command is not available or the activation script cannot be found in the current shell environment (e.g., when using a minimal shell like dash without sourcing capabilities).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the virtual environment is correctly activated using 'source /path/to/venv/bin/activate'. If the issue persists, verify the integrity of the 'deactivate' script within the virtual environment's 'bin' directory or recreate the virtual environment.","message":"The 'deactivate' command was not found or could not be executed by the shell. This can occur if the virtual environment's activation script was not properly sourced, or if the 'deactivate' script is missing or corrupted.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T21:01:07.951Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install virtualenv globally using pip: `pip install virtualenv`.","cause":"The `virtualenv` command is not installed, or its executable location is not included in your system's PATH environment variable.","error":"virtualenv: command not found"},{"fix":"Ensure the parent directory exists and you have write permissions, or specify a different path: `mkdir my_project && cd my_project && virtualenv venv`.","cause":"The virtual environment could not be created because the target directory does not exist or the user lacks write permissions for that location.","error":"failed to create virtual environment"},{"fix":"Specify the full path to your desired Python interpreter using the `-p` flag: `virtualenv -p /usr/bin/python3.9 my_env`.","cause":"`virtualenv` could not automatically locate a Python executable at common system paths.","error":"Cannot find python interpreter"},{"fix":"In Command Prompt, use: `.\\my_env\\Scripts\\activate.bat`. In PowerShell, use: `.\\my_env\\Scripts\\Activate.ps1`.","cause":"On Windows, the virtual environment activation script needs to be run by specifying its full path and correct file extension, typically from the `Scripts` directory.","error":"'activate' is not recognized as an internal or external command, operable program or batch file."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}