### Install Common Tools in New Environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/tips.rst.txt Example of a postmkvirtualenv hook and a requirements file for bootstrapping new virtual environments. ```bash $ cat postmkvirtualenv #!/usr/bin/env bash curl -O http://python-distribute.org/distribute_setup.p... />python distribute_setup.py rm distribute_setup.py easy_install pip==dev pip install Mercurial ``` ```text $ cat developer_requirements.txt ipdb ipython pastescript nose http://douglatornell.ca/software/python/Nosy-1.0.tar.gz coverage sphinx grin pyflakes pep8 ``` -------------------------------- ### Usage examples for workon Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Demonstrates listing environments, creating new ones, and switching between them. ```bash $ workon $ mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env1)$ mkvirtualenv env2 New python executable in env2/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env2)$ workon env1 env2 (env2)$ workon env1 (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1 (env1)$ workon env2 (env2)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env2 (env2)$ ``` -------------------------------- ### Initialize and Create Virtual Environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Install the package, set the environment home directory, and create a new virtual environment. ```bash $ pip install virtualenvwrapper ... $ export WORKON_HOME=~/Envs $ mkdir -p $WORKON_HOME $ source /usr/local/bin/virtualenvwrapper.sh $ mkvirtualenv env1 Installing setuptools.......................................... .................................................... .................................................... ...............................done. virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env1/bin/predeactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env1/bin/postdeactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env1/bin/preactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env1/bin/postactivate New python executable in env1/bin/python (env1)$ ls $WORKON_HOME env1 hook.log ``` -------------------------------- ### Example usage of cpvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Demonstrates creating a source environment and duplicating it to a destination environment. ```bash $ workon $ mkvirtualenv source New python executable in source/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (source)$ cpvirtualenv source dest Making script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/easy_install relative Making script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/easy_install-2.6 relative Making script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/pip relative Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/postactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/postdeactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/preactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/predeactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) (dest)$ workon dest source (dest)$ ``` -------------------------------- ### List Site Packages Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Display installed packages in the current environment. ```bash (env1)$ lssitepackages Django-1.1.1-py2.6.egg-info easy-install.pth setuptools-0.6.10-py2.6.egg pip-0.6.3-py2.6.egg django setuptools.pth ``` -------------------------------- ### Install Packages in Environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Use pip to install software packages within the active virtual environment. ```bash (env1)$ pip install django Downloading/unpacking django Downloading Django-1.1.1.tar.gz (5.6Mb): 5.6Mb downloaded Running setup.py egg_info for package django Installing collected packages: django Running setup.py install for django changing mode of build/scripts-2.6/django-admin.py from 644 to 755 changing mode of /Users/dhellmann/Envs/env1/bin/django-admin.py to 755 Successfully installed django ``` -------------------------------- ### Install virtualenvwrapper via pip Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/install.rst.txt Standard installation commands for virtualenvwrapper using pip. ```bash $ pip install virtualenvwrapper ``` ```bash $ sudo pip install virtualenvwrapper ``` ```bash $ pip install --user virtualenvwrapper ``` -------------------------------- ### Automate package installation with postmkvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Appends a pip install command to the postmkvirtualenv hook to automatically install packages when a new environment is created. ```bash (env2)$ echo 'pip install sphinx' >> $WORKON_HOME/postmkvirtualenv ``` -------------------------------- ### Usage examples for deactivate Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Demonstrates switching from a virtual environment back to the system Python. ```bash $ workon $ echo $VIRTUAL_ENV $ mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1 (env1)$ deactivate $ echo $VIRTUAL_ENV $ ``` -------------------------------- ### Verify installed package location Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Locates the executable path for a package installed within the active virtual environment. ```bash (venv3)$ which sphinx-build ``` -------------------------------- ### Define Developer Requirements File Source: https://virtualenvwrapper.readthedocs.io/en/latest/tips.html Lists common development dependencies for installation via pip. ```text $ cat developer_requirements.txt ipdb ipython pastescript nose http://douglatornell.ca/software/python/Nosy-1.0.tar.gz coverage sphinx grin pyflakes pep8 ``` -------------------------------- ### Configure environment for PyMOTW Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/scripts.rst.txt Example script to modify the working directory and PATH for a specific project environment. ```bash pymotw_root=/Users/dhellmann/Documents/PyMOTW cd $pymotw_root PATH=$pymotw_root/bin:$PATH ``` -------------------------------- ### Customize PS1 in postactivate Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/scripts.rst.txt Example code to modify the shell prompt by adding a space between the virtual environment name and the previous PS1 value. ```bash PS1="(`basename "$VIRTUAL_ENV"`) $_OLD_VIRTUAL_PS1" ``` -------------------------------- ### Create a new project with mkproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Initializes a new virtual environment and project directory. ```bash $ mkproject myproj New python executable in myproj/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. Creating /Users/dhellmann/Devel/myproj (myproj)$ pwd /Users/dhellmann/Devel/myproj (myproj)$ echo $VIRTUAL_ENV /Users/dhellmann/Envs/myproj (myproj)$ ``` -------------------------------- ### Create a project with multiple templates Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/projects.rst.txt Initializes a new project directory using the bitbucket and django templates. ```bash $ mkproject -t bitbucket -t django my_site ``` -------------------------------- ### Configure shell startup file Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/install.rst.txt Required environment variables and sourcing for standard shell initialization. ```bash export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/Devel source /usr/local/bin/virtualenvwrapper.sh ``` -------------------------------- ### wipeenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Removes all third-party packages installed in the current virtualenv. ```APIDOC ## wipeenv ### Description Remove all of the installed third-party packages in the current virtualenv. ### Syntax wipeenv ``` -------------------------------- ### Build Documentation Source: https://virtualenvwrapper.readthedocs.io/en/latest/developers.html Generates the HTML documentation using Sphinx and make. ```bash $ make html rm -rf virtualenvwrapper/docs (cd docs && make html SPHINXOPTS="-c sphinx/pkg") sphinx-build -b html -d build/doctrees -c sphinx/pkg source build/html Running Sphinx v0.6.4 loading pickled environment... done building [html]: targets for 2 source files that are out of date updating environment: 0 added, 2 changed, 0 removed reading sources... [ 50%] command_ref reading sources... [100%] developers looking for now-outdated files... none found pickling environment... done checking consistency... done preparing documents... done writing output... [ 33%] command_ref writing output... [ 66%] developers writing output... [100%] index writing additional files... search copying static files... WARNING: static directory '/Users/dhellmann/Devel/virtualenvwrapper/plugins/docs/sphinx/pkg/static' does not exist done dumping search index... done dumping object inventory... done build succeeded, 1 warning. Build finished. The HTML pages are in build/html. cp -r docs/build/html virtualenvwrapper/docs ``` -------------------------------- ### Create a new project Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Creates a new virtual environment and a corresponding project directory. ```bash mkproject [-f|--force] [-t template] [virtualenv_options] ENVNAME ``` ```bash $ mkproject myproj New python executable in myproj/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. Creating /Users/dhellmann/Devel/myproj (myproj)$ pwd /Users/dhellmann/Devel/myproj (myproj)$ echo $VIRTUAL_ENV /Users/dhellmann/Envs/myproj (myproj)$ ``` -------------------------------- ### Create Project Directories in postmkvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/tips.html Automates directory creation and path configuration upon virtual environment creation. ```bash proj_name=$(basename $VIRTUAL_ENV) mkdir $HOME/projects/$proj_name add2virtualenv $HOME/projects/$proj_name cd $HOME/projects/$proj_name ``` -------------------------------- ### Create a virtual environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Initializes a new virtual environment using the mkvirtualenv command. ```bash (env3)$ mkvirtualenv env3 ``` -------------------------------- ### Create a new virtual environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Initializes a new environment in WORKON_HOME and activates it automatically. ```bash mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] ENVNAME ``` ```bash $ workon $ mkvirtualenv mynewenv New python executable in mynewenv/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (mynewenv)$ workon mynewenv (mynewenv)$ ``` -------------------------------- ### Configure Entry Points in pyproject.toml Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/plugins.rst.txt Map plugin functions to virtualenvwrapper extension points using the project entry-points configuration. ```toml [project.entry-points."virtualenvwrapper.initialize"] user_scripts = "virtualenvwrapper.user_scripts:initialize" project = "virtualenvwrapper.project:initialize" [project.entry-points."virtualenvwrapper.initialize_source"] user_scripts = "virtualenvwrapper.user_scripts:initialize_source" ``` -------------------------------- ### Automate Project Directory Management Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/tips.rst.txt Use these snippets in postmkvirtualenv and postactivate hooks to automatically create and switch to project-specific directories. ```bash proj_name=$(basename $VIRTUAL_ENV) mkdir $HOME/projects/$proj_name add2virtualenv $HOME/projects/$proj_name cd $HOME/projects/$proj_name ``` ```bash proj_name=$(basename $VIRTUAL_ENV) cd ~/projects/$proj_name ``` -------------------------------- ### Show environment details Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Displays configuration details for a specific virtual environment. ```bash showvirtualenv [env] ``` -------------------------------- ### Manage multiple virtual environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/index.html Create additional environments and verify their existence in the WORKON_HOME directory. ```bash (env1)$ ls $WORKON_HOME env1 hook.log (env1)$ mkvirtualenv env2 Installing setuptools............................... .................................................... .................................................... ........... ...............................done. virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/predeactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/postdeactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/preactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/postactivate New python executable in env2/bin/python (env2)$ ls $WORKON_HOME env1 env2 hook.log ``` -------------------------------- ### Syntax for virtualenvwrapper Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Defines the command syntax for displaying help information. ```text virtualenvwrapper ``` -------------------------------- ### Configure PATH and Source virtualenvwrapper Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/install.rst.txt Set the PATH and source the virtualenvwrapper script to initialize the environment. ```bash export PATH=/usr/local/bin:$PATH source /usr/local/bin/virtualenvwrapper.sh ``` -------------------------------- ### mkproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Creates a new virtual environment and a corresponding project directory. ```APIDOC ## mkproject ### Description Creates a new virtualenv in WORKON_HOME and a project directory in PROJECT_HOME. ### Syntax `mkproject [-f|--force] [-t template] [virtualenv_options] ENVNAME` ### Parameters - `-f, --force`: Create the virtualenv even if the project directory already exists. - `-t template`: Template to use for project creation (can be repeated). - `virtualenv_options`: Options passed directly to mkvirtualenv. ``` -------------------------------- ### Manage virtual environments with workon Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Lists available environments or switches to a specific one. ```bash $ workon $ mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env1)$ mkvirtualenv env2 New python executable in env2/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env2)$ workon env1 env2 (env2)$ workon env1 (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1 (env1)$ workon env2 (env2)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env2 (env2)$ ``` -------------------------------- ### mktmpenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Creates a new temporary virtual environment in the WORKON_HOME directory with a unique name. ```APIDOC ## mktmpenv ### Description Creates a new virtualenv in the WORKON_HOME directory with a unique name. This environment is deleted when deactivated. ### Syntax `mktmpenv [(-c|--cd)|(-n|--no-cd)] [VIRTUALENV_OPTIONS]` ### Parameters - **-c / --cd** (flag) - Optional - Change working directory to the virtualenv directory during post-activate phase. - **-n / --no-cd** (flag) - Optional - Do not change working directory to the virtualenv directory during post-activate phase. ``` -------------------------------- ### mkvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Creates a new virtual environment in the WORKON_HOME directory and automatically activates it. ```APIDOC ## mkvirtualenv ### Description Creates a new environment in the WORKON_HOME directory. The new environment is automatically activated after being initialized. ### Syntax `mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] ENVNAME` ### Parameters - **-a** (path) - Optional - Associate an existing project directory with the new environment. - **-i** (package) - Optional - Install one or more packages after the environment is created. - **-r** (file) - Optional - Specify a text file listing packages to be installed via pip. - **ENVNAME** (string) - Required - The name of the new virtual environment. ``` -------------------------------- ### Copy a virtual environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Demonstrates copying an existing virtual environment to a new destination. ```bash $ workon $ mkvirtualenv source New python executable in source/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (source)$ cpvirtualenv source dest Making script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/easy_install relative Making script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/easy_install-2.6 relative Making script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/pip relative Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/postactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/postdeactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/preactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) Script /Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/predeactivate cannot be made relative (it's not a normal script that starts with #!/Users/dhellmann/Devel/virtualenvwrapper/tmp/dest/bin/python) (dest)$ workon dest source (dest)$ ``` -------------------------------- ### mkproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Creates a new virtualenv in WORKON_HOME and a project directory in PROJECT_HOME. ```APIDOC ## mkproject ### Description Create a new virtualenv in the WORKON_HOME and project directory in PROJECT_HOME. ### Syntax mkproject [-f|--force] [-t template] [virtualenv_options] ENVNAME ### Options - **-f, --force** - Create the virtualenv even if the project directory already exists. - **-t template** - Template to use for project creation (can be repeated). ``` -------------------------------- ### allvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Runs a specified command in all virtual environments located under WORKON_HOME. The command is executed within each environment after activation, though activation hooks are bypassed. ```APIDOC ## allvirtualenv ### Description Run a command in all virtualenvs under WORKON_HOME. ### Syntax allvirtualenv command with arguments ### Usage Example $ allvirtualenv pip install -U pip ``` -------------------------------- ### Manage Multiple Environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Create additional environments and switch between them using the workon command. ```bash (env1)$ ls $WORKON_HOME env1 hook.log (env1)$ mkvirtualenv env2 Installing setuptools............................... .................................................... .................................................... ........... ...............................done. virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/predeactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/postdeactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/preactivate virtualenvwrapper.user_scripts Creating /Users/dhellmann/Envs/env2/bin/postactivate New python executable in env2/bin/python (env2)$ ls $WORKON_HOME env1 env2 hook.log ``` ```bash (env2)$ workon env1 (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Envs/env1 (env1)$ ``` -------------------------------- ### Change Directory in postactivate Source: https://virtualenvwrapper.readthedocs.io/en/latest/tips.html Automatically navigates to a project-specific directory when activating an environment. ```bash proj_name=$(basename $VIRTUAL_ENV) cd ~/projects/$proj_name ``` -------------------------------- ### List site-packages contents with lssitepackages Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Displays the contents of the site-packages directory for the currently active virtualenv. ```bash $ mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env1)$ $ workon env1 (env1)$ lssitepackages setuptools-0.6.10-py2.6.egg pip-0.6.3-py2.6.egg easy-install.pth setuptools.pth ``` -------------------------------- ### virtualenvwrapper Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Displays help information including a list of commands, descriptions, version details, and configuration locations. ```APIDOC ## virtualenvwrapper ### Description Print a list of commands, their descriptions, and some details about the version and locations used by virtualenvwrapper as basic help output. ### Syntax virtualenvwrapper ``` -------------------------------- ### Update shell startup configuration Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/install.rst.txt Update the source path in your shell startup file to point to the new virtualenvwrapper.sh script. ```bash source /usr/local/bin/virtualenvwrapper_bashrc ``` ```bash source /usr/local/bin/virtualenvwrapper.sh ``` -------------------------------- ### Define an extension entry point function Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/plugins.rst.txt Every extension point uses a function that accepts a single argument, which is a list of strings passed from the command line. ```python def function_name(args): # args is a list of strings passed to the hook loader ``` -------------------------------- ### Navigate to site-packages with cdsitepackages Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Provides a shortcut to the site-packages directory of the active virtualenv, optionally navigating into a subdirectory. ```bash $ mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1 (env1)$ cdsitepackages PyMOTW/bisect/ (env1)$ pwd /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1/lib/python2.6/site-packages/PyMOTW/bisect ``` -------------------------------- ### Enable lazy loading Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/install.rst.txt Alternative initialization script to load virtualenvwrapper only when needed. ```bash export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/Devel export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh source /usr/local/bin/virtualenvwrapper_lazy.sh ``` -------------------------------- ### Syntax for workon command Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Defines the syntax for listing or switching virtual environments. ```bash workon [(-c|--cd)|(-n|--no-cd)] [environment_name|"."] ``` -------------------------------- ### Navigate to site-packages directory Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Changes the current working directory to the site-packages folder of the active virtualenv. ```bash cdsitepackages [subdir] ``` ```bash $ mkvirtualenv env1 New python executable in env1/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1 (env1)$ cdsitepackages PyMOTW/bisect/ (env1)$ pwd /Users/dhellmann/Devel/virtualenvwrapper/tmp/env1/lib/python2.6/site-packages/PyMOTW/bisect ``` -------------------------------- ### Implement Logging in Extensions Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/plugins.rst.txt Use the standard Python logging module to record extension activity to the hook log file. ```python import logging log = logging.getLogger(__name__) def pre_mkvirtualenv(args): log.debug('pre_mkvirtualenv %s', str(args)) # ... ``` -------------------------------- ### Switch between environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/index.html Use the workon command to change the active virtual environment. ```bash (env2)$ workon env1 (env1)$ echo $VIRTUAL_ENV /Users/dhellmann/Envs/env1 (env1)$ ``` -------------------------------- ### Configure Environment Hooks Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/index.rst.txt Customize environment activation by appending commands to hook scripts. ```bash (env1)$ echo 'cd $VIRTUAL_ENV' >> $WORKON_HOME/postactivate (env1)$ workon env2 (env2)$ pwd /Users/dhellmann/Envs/env2 ``` -------------------------------- ### Configure environment activation hooks Source: https://virtualenvwrapper.readthedocs.io/en/latest/index.html Append commands to the postactivate script to automatically execute actions upon environment activation. ```bash (env1)$ echo 'cd $VIRTUAL_ENV' >> $WORKON_HOME/postactivate (env1)$ workon env2 (env2)$ pwd /Users/dhellmann/Envs/env2 ``` -------------------------------- ### Invoke project post-creation hook Source: https://virtualenvwrapper.readthedocs.io/en/latest/plugins.html Triggers the post_mkproject hook after project creation. ```bash virtualenvwrapper_run_hook project.post_mkproject ``` -------------------------------- ### List virtual environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Displays all environments managed by virtualenvwrapper. ```bash lsvirtualenv [-b] [-l] [-h] ``` -------------------------------- ### Create a temporary environment with mktmpenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Generates a unique virtual environment that is deleted upon deactivation. ```bash mktmpenv [(-c|--cd)|(-n|--no-cd)] [VIRTUALENV_OPTIONS] ``` ```bash $ mktmpenv Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7' New python executable in 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/bin/python Overwriting 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/lib/python2.7/distutils/__init__.py with new content Installing setuptools............................................... .................................................................... .................................................................done. This is a temporary environment. It will be deleted when deactivated. (1e513ac6-616e-4d56-9aa5-9d0a3b305e20) $ ``` -------------------------------- ### Bind a virtualenv to a project with setvirtualenvproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Associates a virtual environment with a project directory so that workon automatically switches to the project. ```bash $ mkproject myproj New python executable in myproj/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. Creating /Users/dhellmann/Devel/myproj (myproj)$ mkvirtualenv myproj_new_libs New python executable in myproj/bin/python Installing setuptools............................................. .................................................................. .................................................................. done. Creating /Users/dhellmann/Devel/myproj (myproj_new_libs)$ setvirtualenvproject $VIRTUAL_ENV $(pwd) ``` -------------------------------- ### Configure Enhanced Zsh Prompt with Color Source: https://virtualenvwrapper.readthedocs.io/en/latest/tips.html Leverage zsh-specific features to add color and right-aligned segments to the virtual environment prompt indicator. ```zsh zsh_virtualenv_prompt() { # If not in a virtualenv, print nothing [[ "$VIRTUAL_ENV" == "" ]] && return # Distinguish between the shell where the virtualenv was activated # and its children local venv_name="${VIRTUAL_ENV##*/}" if typeset -f deactivate >/dev/null; then echo "[%F{green}${venv_name}%f] " else echo "<%F{green}${venv_name}%f> " fi } setopt PROMPT_SUBST PROMPT_PERCENT # Display a "we are in a virtualenv" indicator that works in child shells too VIRTUAL_ENV_DISABLE_PROMPT=1 RPS1='$(zsh_virtualenv_prompt)' ``` -------------------------------- ### lssitepackages Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Lists the contents of the site-packages directory for the active virtual environment. ```APIDOC ## lssitepackages ### Description Displays the contents of the site-packages directory of the currently-active virtualenv. ### Syntax `lssitepackages` ``` -------------------------------- ### workon Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Lists available virtual environments or switches the active environment to the one specified by name. ```APIDOC ## workon ### Description List or change working virtual environments. ### Syntax workon [(-c|--cd)|(-n|--no-cd)] [environment_name|"."] ### Parameters - **environment_name** (string) - Optional - The name of the virtual environment to activate. If omitted, lists all environments. - **-c / --cd** (flag) - Optional - Change working directory to the project directory during post-activate phase. - **-n / --no-cd** (flag) - Optional - Do not change working directory to the project directory during post-activate phase. ``` -------------------------------- ### Syntax for cdproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Defines the command syntax for changing the directory to the active project. ```text cdproject ``` -------------------------------- ### Declare a namespace package in __init__.py Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/plugins.rst.txt Use this code in your __init__.py file to participate in the virtualenvwrapper namespace package. ```python """virtualenvwrapper module """ __import__('pkg_resources').declare_namespace(__name__) ``` -------------------------------- ### Duplicate a virtual environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Creates a copy of an existing virtual environment. Use with caution as path information may be hard-coded. ```bash cpvirtualenv ENVNAME [TARGETENVNAME] ``` -------------------------------- ### showvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Displays details for a specific virtual environment. ```APIDOC ## showvirtualenv ### Description Show the details for a single virtualenv. ### Syntax `showvirtualenv [env]` ### Parameters - **env** (string) - Optional - The name of the virtual environment to inspect. ``` -------------------------------- ### Syntax for setvirtualenvproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Defines the command syntax for binding a virtualenv to a project path. ```text setvirtualenvproject [virtualenv_path project_path] ``` -------------------------------- ### Configure virtualenvwrapper for MSYS Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/install.rst.txt Environment variables and source commands for using virtualenvwrapper within an MSYS environment. ```bash export WORKON_HOME=$HOME/.virtualenvs export MSYS_HOME=/c/msys/1.0 source /usr/local/bin/virtualenvwrapper.sh ``` ```bash export WORKON_HOME=$HOME/.virtualenvs export MSYS_HOME=C:\msys\1.0 source /usr/local/bin/virtualenvwrapper.sh ``` -------------------------------- ### Run command in all virtual environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Executes a specified command within every virtual environment located under WORKON_HOME. ```bash $ allvirtualenv pip install -U pip ``` -------------------------------- ### Run Test Suite Source: https://virtualenvwrapper.readthedocs.io/en/latest/developers.html Executes tests using tox. Various configurations allow for running all tests, specific scripts, or tests under specific Python versions. ```bash $ tox ``` ```bash $ tox -- tests/test_cd.sh ``` ```bash $ tox -e py311 ``` ```bash $ tox -e py311 -- tests/test_cd.sh ``` ```bash $ tox -e fast ``` -------------------------------- ### Invoke project pre-creation hook Source: https://virtualenvwrapper.readthedocs.io/en/latest/plugins.html Triggers the pre_mkproject hook with the project name argument. ```bash virtualenvwrapper_run_hook project.pre_mkproject $project_name ``` -------------------------------- ### Create a temporary virtual environment Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Generates a unique virtual environment that is automatically deleted upon deactivation. ```bash mktmpenv [(-c|--cd)|(-n|--no-cd)] [VIRTUALENV_OPTIONS] ``` ```bash $ mktmpenv Using real prefix '/Library/Frameworks/Python.framework/Versions/2.7' New python executable in 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/bin/python Overwriting 1e513ac6-616e-4d56-9aa5-9d0a3b305e20/lib/python2.7/distutils/__init__.py with new content Installing setuptools.............................................. .................................................................... .................................................................done. This is a temporary environment. It will be deleted when deactivated. (1e513ac6-616e-4d56-9aa5-9d0a3b305e20) $ ``` -------------------------------- ### deactivate Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Switches from the current virtual environment back to the system-installed version of Python. ```APIDOC ## deactivate ### Description Switch from a virtual environment to the system-installed version of Python. ### Syntax deactivate ``` -------------------------------- ### lsvirtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html Lists all virtual environments managed by virtualenvwrapper. ```APIDOC ## lsvirtualenv ### Description Lists all of the environments. ### Syntax `lsvirtualenv [-b] [-l] [-h]` ### Parameters - **-b** (flag) - Optional - Brief mode, disables verbose output. - **-l** (flag) - Optional - Long mode, enables verbose output (default). - **-h** (flag) - Optional - Print the help for lsvirtualenv. ``` -------------------------------- ### Implement Direct Action Hook Source: https://virtualenvwrapper.readthedocs.io/en/latest/plugins.html Direct action hooks perform tasks immediately when triggered by the hook loader. ```python def initialize(args): for filename, comment in GLOBAL_HOOKS: make_hook(os.path.join('$WORKON_HOME', filename), comment) return ``` -------------------------------- ### Syntax for wipeenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Defines the command syntax for removing all third-party packages from the current environment. ```text wipeenv ``` -------------------------------- ### setvirtualenvproject Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Binds an existing virtualenv to an existing project directory. When no arguments are provided, it defaults to the current virtualenv and current working directory. ```APIDOC ## setvirtualenvproject ### Description Binds an existing virtualenv to an existing project directory. This association ensures that when the virtualenv is activated via 'workon', the project directory is also activated. ### Syntax setvirtualenvproject [virtualenv_path project_path] ### Parameters - **virtualenv_path** (string) - Optional - The full path to the virtualenv. - **project_path** (string) - Optional - The full path to the project directory. ``` -------------------------------- ### cdsitepackages Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Changes the current working directory to the site-packages directory of the active virtual environment. ```APIDOC ## cdsitepackages ### Description Changes the current working directory to the site-packages directory for the active virtualenv. An optional subdirectory can be specified to navigate into the site-packages hierarchy. ### Syntax `cdsitepackages [subdir]` ``` -------------------------------- ### Edit premkvirtualenv hook Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/scripts.rst.txt Command to open the global premkvirtualenv hook script for editing. ```bash vim ~/.virtualenvs/premkvirtualenv ``` -------------------------------- ### Customize Bash/Zsh Prompt for Virtual Environments Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/tips.rst.txt Use these functions in .bashrc or .zshrc to display a custom virtualenv indicator that persists in sub-shells. ```bash virtualenv_prompt() { # If not in a virtualenv, print nothing [[ "$VIRTUAL_ENV" == "" ]] && return # Distinguish between the shell where the virtualenv was activated # and its children local venv_name="${VIRTUAL_ENV##*/}" if typeset -f deactivate >/dev/null; then echo "[${venv_name}] " else echo "<${venv_name}> " fi } # Display a "we are in a virtualenv" indicator that works in child shells too VIRTUAL_ENV_DISABLE_PROMPT=1 PS1='$(virtualenv_prompt)'"$PS1" ``` ```zsh zsh_virtualenv_prompt() { # If not in a virtualenv, print nothing [[ "$VIRTUAL_ENV" == "" ]] && return # Distinguish between the shell where the virtualenv was activated # and its children local venv_name="${VIRTUAL_ENV##*/}" if typeset -f deactivate >/dev/null; then echo "[%F{green}${venv_name}%f] " else echo "<%F{green}${venv_name}%f> " fi } setopt PROMPT_SUBST PROMPT_PERCENT # Display a "we are in a virtualenv" indicator that works in child shells too VIRTUAL_ENV_DISABLE_PROMPT=1 RPS1='$(zsh_virtualenv_prompt)' ``` -------------------------------- ### add2virtualenv Source: https://virtualenvwrapper.readthedocs.io/en/latest/_sources/command_ref.rst.txt Adds specified directories to the Python path of the active virtual environment. ```APIDOC ## add2virtualenv ### Description Adds the specified directories to the Python path for the currently-active virtualenv by creating a .pth file. Running without arguments displays current extra paths. ### Syntax `add2virtualenv directory1 directory2 ...` ### Options - `-d`: Remove the added path. ```