The virtualenvwrapper package supports managing many virtual environments. See their documentation page for more information.
Since Python 3.3 the standard library contains the venv package supporting the creation of virtual environments. We recommend to create the virtual environment in the root directory of your project and naming it either .venv or venv (see The Hichhiker's Guide to Python).
Creating a virtual environment:
python -m venv .venvActivate the virtual environment:
source .venv/bin/activateDeactivate the virtual environment:
deactivateThe following helper automatically activates a virtual environment when cd-ing into a directory with a accompanying virtual environment. For this to work, the virtual environment must be located in the root directory of your project and be named .venv or venv. Activation also works whe cd-ing into a subdirectory. Changing into a directory outside of your project's directory structure will automatically deactivate the virtual environment.
Add the following lines to your ~/.bashrc:
_update_path() {
# Activate python virtualenv if '.venv' or 'venv' directory exists
P=$(pwd)
while [[ "$P" != / ]]; do
if [[ -d "$P/.venv" && -f "$P/.venv/bin/activate" ]]; then
if [[ "$P/.venv" != "$VIRTUAL_ENV" ]]; then
source $P/.venv/bin/activate
fi
FOUND_VENV=yes
break
fi
if [[ -d "$P/venv" && -f "$P/venv/bin/activate" ]]; then
if [[ "$P/venv" != "$VIRTUAL_ENV" ]]; then
source $P/venv/bin/activate
fi
FOUND_VENV=yes
break
fi
P=$(dirname "$P")
done
if [[ "$FOUND_VENV" != yes && -v VIRTUAL_ENV ]]; then
deactivate
fi
unset FOUND_VENV
unset P
true
}
cd() {
builtin cd "$@"
_update_path
}
_update_path
After registering hooks with install pre-commit will abort commits if there are black, isort or flake8 changes to be made. If machine fixable (ie. black and isort) pre-commit usually applies those changes leaving you to stage them using git add before retrying your commit.
pip install pre-commit
pre-commit installStore the following configuration in .pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: black
name: black
language: system
entry: black
types: [python]
- id: isort
name: isort
language: system
entry: isort -y
types: [python]
- id: flake8
name: flake8
language: system
entry: flake8
types: [python]You can also run black, isort and flake8 on all content without comitting:
pre-commit run -a