Python virtual environment (3)—virtualenvwrapper use

Original link: https://chegva.com/5687.html

◎Knowledge points

  1. virtualenvwrapper overview

  2. virtualenvwrapper install

  3. virtualenvwrapper uses

1. virtualenvwrapper overview

Although virtualenv solves the problems of Python third-party library version conflicts and management confusion, it is also very inconvenient during use. for example:

  1. The virtual environment management is inconvenient: the virtual environment is scattered under various paths, and the management will inevitably be chaotic as the project lasts for a long time. I don’t know where it is and what version it is

  2. Virtual environment activation is inconvenient: When you want to use a virtual environment, you need to manually switch to the Scripts directory or bin directory of the virtual environment, and then execute activate or source activate

  3. Virtual environment switching is also inconvenient: when switching to another virtual environment, you need to deactivate the current virtual environment first, then enter the corresponding directory, and execute the activation command

The third-party library virtualenvwrapper was created to solve the inconvenience of using virtualenv. You can also see the detailed introduction from the official description of Pypi virtualenvwrapper :

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducings inducing the conflict.

Features

  1. Organizes all of your virtual environments in one place. Manage all virtual environments under the same path

  2. Wrappers for creating, copying and deleting environments, including user-configurable hooks.

  3. Use a single command to switch between environments. Use only one command to switch between multiple virtual environments

  4. Tab completion for commands that take a virtual environment as argument. Tab completion is provided for commands that use a virtual environment as an argument.

  5. User-configurable hooks for all operations. User-configurable hooks are provided for all operations

  6. Plugin system for more creating sharable extensions. Provides a plug-in system for more creating sharable extensions.

1679936819972720.png

2. virtualenvwrapper installation

1679940937773349.png

 # install virtualenvwrapper 
  
➜ ~ pip3 install virtualenvwrapper 
  
 
  
# Add two lines export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 in ~/.bash_profile 
  
# Specify the Python interpreter in the Python installation directory corresponding to virtualenvwrapper source ~/Library/Python/3.8/bin/virtualenvwrapper.sh 
  
# Execute virtualenvwrapper.sh in the Python installation directory corresponding to virtualenvwrapper 
  
 
  
# Execute virtualenvwrapper 
  
➜ ~ virtualenvwrapper 
  
 
  
virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv 
  
tool. The extensions include wrappers for creating and deleting 
  
virtual environments and otherwise managing your development workflow, 
  
making it easier to work on more than one project at a time without 
  
introducing conflicts in their dependencies. 
  
 
  
For more information please refer to the documentation: 
  
 
  
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html 
  
 
  
Commands available: 
  
 
  
add2virtualenv: add directory to the import path 
  
 
  
allvirtualenv: run a command in all virtualenvs 
  
 
  
cdproject: change directory to the active project 
  
 
  
cdsitepackages: change to the site-packages directory 
  
 
  
cdvirtualenv: change to the $VIRTUAL_ENV directory 
  
 
  
cpvirtualenv: duplicate the named virtualenv to make a new one 
  
 
  
lssitepackages: list contents of the site-packages directory 
  
 
  
lsvirtualenv: list virtualenvs 
  
 
  
mkproject: create a new project directory and its associated virtualenv 
  
 
  
mktmpenv: create a temporary virtualenv 
  
 
  
mkvirtualenv: Create a new virtualenv in $WORKON_HOME 
  
 
  
rmvirtualenv: Remove a virtualenv 
  
 
  
setvirtualenvproject: associate a project directory with a virtualenv 
  
 
  
showvirtualenv: show details of a single virtualenv 
  
 
  
toggle global site-packages: turn access to global site-packages on/off 
  
 
  
virtualenvwrapper: show this help message 
  
 
  
wipeenv: remove all packages installed in the current virtualenv 
  
 
  
workon: list or change working virtualenvs

3. virtualenvwrapper use

3.1 Create a virtual environment

1679943879122162.png

 ➜ ~ mkvirtualenv MyVENV1 
  
created virtual environment CPython3.9.10.final.0-64 in 391ms 
  
creator CPython3Posix(dest=/Users/anzhihe/.virtualenvs/MyVENV1, clear=False, no_vcs_ignore=False, global=False) 
  
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) 
  
added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4 
  
activators Bash Activator, CShell Activator, Fish Activator, Nushell Activator, PowerShell Activator, Python Activator 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/predeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/postdeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/preactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/postactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/get_env_details 
  
(MyVENV1) ➜ ~ mkvirtualenv MyVENV2 
  
created virtual environment CPython3.9.10.final.0-64 in 373ms 
  
creator CPython3Posix(dest=/Users/anzhihe/.virtualenvs/MyVENV2, clear=False, no_vcs_ignore=False, global=False) 
  
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) 
  
added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4 
  
activators Bash Activator, CShell Activator, Fish Activator, Nushell Activator, PowerShell Activator, Python Activator 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/predeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/postdeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/preactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/postactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/get_env_details 
  
(MyVENV2) ➜ ~ deactivate 
  
 
  
# modify WORKON_HOME default path ➜ ~ mkdir -p ~/MyVirEnvs 
  
➜ ~ export WORKON_HOME=~/MyVirEnvs 
  
➜ ~ mkvirtualenv MyVENV3 
  
created virtual environment CPython3.9.10.final.0-64 in 365ms 
  
creator CPython3Posix(dest=/Users/anzhihe/MyVirEnvs/MyVENV3, clear=False, no_vcs_ignore=False, global=False) 
  
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) 
  
added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4 
  
activators Bash Activator, CShell Activator, Fish Activator, Nushell Activator, PowerShell Activator, Python Activator 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/predeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/postdeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/preactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/postactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/get_env_details 
  
 
  
 
  
(MyVENV3) ➜ ~ where python2 
  
python2: aliased to /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 
  
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2 
  
/usr/local/bin/python2 
  
(MyVENV3) ➜ ~ mkvirtualenv -p "/Library/Frameworks/Python.framework/Versions/2.7/bin/python2" MyVENV4 
  
created virtual environment CPython2.7.18.final.0-64 in 850ms 
  
creator CPython2macOsFramework(dest=/Users/anzhihe/MyVirEnvs/MyVENV4, clear=False, no_vcs_ignore=False, global=False) 
  
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) 
  
added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.37.1 
  
activators Bash Activator, CShell Activator, Fish Activator, Nushell Activator, PowerShell Activator, Python Activator 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/predeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/postdeactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/preactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/postactivate 
  
virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/get_env_details 
  
(MyVENV4) ➜ ~

3.2 Activate or switch

1679945928229955.png

 ➜ ~lsvirtualenv 
  
MyVENV3 
  
======= 
  
MyVENV4 
  
======= 
  
 
  
➜ ~ workon 
  
MyVENV3 
  
MyVENV4 
  
➜ ~ work on MyVENV3 
  
(MyVENV3) ➜ ~ work on MyVENV4 
  
(MyVENV4) ➜ ~

3.3 Virtual Environment Operation

1679946886108617.png

 (MyVENV4) ➜ ~ work on MyVENV3 
  
(MyVENV3) ➜ ~ where python 
  
/Users/anzhihe/MyVirEnvs/MyVENV3/bin/python 
  
/Library/Frameworks/Python.framework/Versions/2.7/bin/python 
  
/usr/local/bin/python 
  
(MyVENV3) ➜ ~ where pip 
  
/Users/anzhihe/MyVirEnvs/MyVENV3/bin/pip 
  
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip 
  
/usr/local/bin/pip 
  
(MyVENV3) ➜ ~ pip --version 
  
pip 23.0.1 from /Users/anzhihe/MyVirEnvs/MyVENV3/lib/python3.9/site-packages/pip (python 3.9) 
  
 
  
(MyVENV3) ➜ ~ pip install requests 
  
(MyVENV3) ➜ ~ cat test.py 
  
import requests 
  
 
  
print(requests) 
  
 
  
(MyVENV3) ➜ ~ python test.py 
  
<module 'requests' from '/Users/anzhihe/MyVirEnvs/MyVENV3/lib/python3.9/site-packages/requests/__init__.py'> 
  
 
  
(MyVENV3) ➜ ~ cdvirtualenv 
  
(MyVENV3) ➜ MyVENV3 cdsitepackages 
  
(MyVENV3) ➜ site-packages lssitepackages 
  
__pycache__ idna setuptools-67.4.0.dist-info 
  
_distutils_hack idna-3.4.dist-info setuptools-67.4.0.virtualenv 
  
_virtualenv.pth pip urllib3 
  
_virtualenv.py pip-23.0.1.dist-info urllib3-1.26.15.dist-info 
  
certifi pip-23.0.1.virtualenv wheel 
  
certifi-2022.12.7.dist-info pkg_resources wheel-0.38.4.dist-info 
  
charset_normalizer requests wheel-0.38.4.virtualenv 
  
charset_normalizer-3.1.0.dist-info requests-2.28.2.dist-info 
  
distutils-precedence.pth setuptools

3.4 Deactivate the virtual environment

1679969057213249.png

 (MyVENV3) ➜ site-packages 
  
(MyVENV3) ➜ site-packages deactivate 
  
➜ site-packages rmvirtualenv MyVENV3 
  
Removing MyVENV3... 
  
➜ MyVirEnvs

This article is transferred from: https://chegva.com/5687.html
This site is only for collection, and the copyright belongs to the original author.