Manage Python Virtual Environment

Sometimes, you might accidentally install many versions of Python and get confused about which one to use or which one is being used in your computer's environment. In this aritcle, several solutions to deal with virtual environment will be covered.

OS: Ubuntu on Docker

image-20210926185250076

Solution1: venv

Official Document: https://docs.python.org/3/library/venv.html

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

# Install
apt install python3.8-venv
# Create
python3 -m venv /path/to/new/virtual/environment
# Activate
source <venv>/bin/activate
# Deactivate
deactivate

Solution2: virtualenv

# Install virtualenv.
pip install virtualenv
# Create
# When virtualenv version >= 20, option `--no-site-packages` is not needed.
virtualenv my_venv_by_virtualenv
# Activate
source my_venv_by_virtualenv/bin/activate
# Deactivate
deactivate

When not using virtualenvwrapper, it is allowed to remove virtual env by directlt deleting virtual environment's directory .

Solution3: virtualenvwrapper

Official Document: https://virtualenvwrapper.readthedocs.io/en/latest/

# Install
pip install virtualenvwrapper
# Config
# Add export line to .bashrc to permanently do config.
export WORKON_HOME=~/virtualenvwrapper_home
mkdir -p $WORKON_HOME

There are command in tool virtualenvwrapper as following:

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
toggleglobalsitepackages: 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
# Activate
workon env1
# Reveal venv dir
echo $VIRTUAL_ENV
# Deactivate
deactivate

Solution4: conda on miniconda

Miniconda Official Website: https://docs.conda.io/en/latest/miniconda.html

When miniconda3 is installed, tool conda is installed along with it.

# Configurate miniconda3
# Edit .zshrc or .bashrc
export miniconda=/path/to/miniconda3
export PATH=$miniconda/bin:$miniconda/condabin:$PATH
# Save, quite. Then, source ~/.zshrc
# Check the current python environment.
conda env list
# Create a virtual environment.
conda create -n se-hw python=3.6
# Activate a virtual environment.
conda activate se-hw
# Deactivate a virtual environment.
conda deactivate
# Remove a virtual environment.
conda remove -n se-hw --all
# Remove a repo in the virtual environment.
conda remove -n se-hw $(package-name)

To be Continued! Packaging Tools in Python

poetry

A tool for Python dependency management and packaging.

GitHub Page: https://github.com/python-poetry/poetry

pipenv

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in this world.

Official Website: https://pipenv.pypa.io/en/latest/