setV:一个管理 Python 虚拟环境的 Bash 函数

了解一下 setV,它是一个轻量级的 Python 虚拟环境管理器,是 virtualenvwrapper 的替代产品。

这一年多来,我的 bash_scripts 项目中悄悄隐藏这 setV,但现在是时候该公开了。setV 是一个 Bash 函数,我可以用它代替 virtualenvwrapper。它提供了使你能够执行以下操作的基本功能:

  • 默认使用 Python 3
  • 创建一个新的虚拟环境
  • 使用带有 -p(或 --python)的自定义 Python 路径来创建新的虚拟环境
  • 删除现有的虚拟环境
  • 列出所有现有的虚拟环境
  • 使用制表符补全(以防你忘记虚拟环境名称)

安装

要安装 setV,请下载该脚本:

1
curl https://gitlab.com/psachin/setV/raw/master/install.sh

审核一下脚本,然后运行它:

1
sh ./install.sh

当安装 setV 时,安装脚本会要求你引入(source)一下 ~/.bashrc~/.bash_profile 的配置,根据你的喜好选择一个。

用法

基本的命令格式是 setv

创建虚拟环境

1
2
3
4
setv --new rango  # setv -n rango

# 或使用定制的 Python 路径
setv --new --python /opt/python/python3 rango # setv -n -p /opt/python/python3 rango

激活已有的虚拟环境

1
setv VIRTUAL_ENVIRONMENT_NAME
1
2
# 示例
setv rango

列出所有的虚拟环境

1
2
3
setv --list
# 或
setv [TAB] [TAB]

删除虚拟环境

1
setv --delete rango

切换到另外一个虚拟环境

1
2
# 假设你现在在 'rango',切换到 'tango'
setv tango

制表符补完

如果你不完全记得虚拟环境的名称,则 Bash 式的制表符补全也可以适用于虚拟环境名称。

参与其中

setV 在 GNU GPLv3下开源,欢迎贡献。要了解更多信息,请访问它的 GitLab 存储库中的 setV 的 README 的贡献部分。

setV 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
# setV - A Lightweight Python virtual environment manager.
# Author: Sachin (psachin) <iclcoolster@gmail.com>
# Author's URL: https://psachin.gitlab.io/about
#
# License: GNU GPL v3, See LICENSE file
#
# Configure(Optional):
# Set `SETV_VIRTUAL_DIR_PATH` value to your virtual environments
# directory-path. By default it is set to '~/virtualenvs/'
#
# Usage:
# Manual install: Added below line to your .bashrc or any local rc script():
# ---
# source /path/to/virtual.sh
# ---
#
# Now you can 'activate' the virtual environment by typing
# $ setv <YOUR VIRTUAL ENVIRONMENT NAME>
#
# For example:
# $ setv rango
#
# or type:
# setv [TAB] [TAB] (to list all virtual envs)
#
# To list all your virtual environments:
# $ setv --list
#
# To create new virtual environment:
# $ setv --new new_virtualenv_name
#
# To delete existing virtual environment:
# $ setv --delete existing_virtualenv_name
#
# To deactivate, type:
# $ deactivate

# Path to virtual environment directory
SETV_VIRTUAL_DIR_PATH="$HOME/virtualenvs/"
# Default python version to use. This decides whether to use `virtualenv` or `python3 -m venv`
SETV_PYTHON_VERSION=3 # Defaults to Python3
SETV_PY_PATH=$(which python${SETV_PYTHON_VERSION})

function _setvcomplete_()
{
# Bash-autocompletion.
# This ensures Tab-auto-completions work for virtual environment names.
local cmd="${1##*/}" # to handle command(s).
# Not necessary as such. 'setv' is the only command

local word=${COMP_WORDS[COMP_CWORD]} # Words thats being completed
local xpat='${word}' # Filter pattern. Include
# only words in variable '$names'
local names=$(ls -l "${SETV_VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}') # Virtual environment names

COMPREPLY=($(compgen -W "$names" -X "$xpat" -- "$word")) # compgen generates the results
}

function _setv_help_() {
# Echo help/usage message
echo "Usage: setv [OPTIONS] [NAME]"
echo Positional argument:
echo -e "NAME Activate virtual env."
echo Optional arguments:
echo -e "-l, --list List all Virtual Envs."
echo -e "-n, --new NAME Create a new Python Virtual Env."
echo -e "-d, --delete NAME Delete existing Python Virtual Env."
echo -e "-p, --python PATH Python binary path."
}

function _setv_custom_python_path()
{
if [ -f "${1}" ];
then
if [ "`expr $1 : '.*python\([2,3]\)'`" = "3" ];
then
SETV_PYTHON_VERSION=3
else
SETV_PYTHON_VERSION=2
fi
SETV_PY_PATH=${1}
_setv_create $2
else
echo "Error: Path ${1} does not exist!"
fi
}

function _setv_create()
{
# Creates new virtual environment if ran with -n|--new flag
if [ -z ${1} ];
then
echo "You need to pass virtual environment name"
_setv_help_
else
echo "Creating new virtual environment with the name: $1"

if [ ${SETV_PYTHON_VERSION} -eq 3 ];
then
${SETV_PY_PATH} -m venv ${SETV_VIRTUAL_DIR_PATH}${1}
else
virtualenv -p ${SETV_PY_PATH} ${SETV_VIRTUAL_DIR_PATH}${1}
fi

echo "You can now activate the Python virtual environment by typing: setv ${1}"
fi
}

function _setv_delete()
{
# Deletes virtual environment if ran with -d|--delete flag
# TODO: Refactor
if [ -z ${1} ];
then
echo "You need to pass virtual environment name"
_setv_help_
else
if [ -d ${SETV_VIRTUAL_DIR_PATH}${1} ];
then
read -p "Really delete this virtual environment(Y/N)? " yes_no
case $yes_no in
Y|y) rm -rvf ${SETV_VIRTUAL_DIR_PATH}${1};;
N|n) echo "Leaving the virtual environment as it is.";;
*) echo "You need to enter either Y/y or N/n"
esac
else
echo "Error: No virtual environment found by the name: ${1}"
fi
fi
}

function _setv_list()
{
# Lists all virtual environments if ran with -l|--list flag
echo -e "List of virtual environments you have under ${SETV_VIRTUAL_DIR_PATH}:\n"
for virt in $(ls -l "${SETV_VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}')
do
echo ${virt}
done
}

function setv() {
# Main function
if [ $# -eq 0 ];
then
_setv_help_
elif [ $# -le 3 ];
then
case "${1}" in
-n|--new) _setv_create ${2};;
-d|--delete) _setv_delete ${2};;
-l|--list) _setv_list;;
*) if [ -d ${SETV_VIRTUAL_DIR_PATH}${1} ];
then
# Activate the virtual environment
source ${SETV_VIRTUAL_DIR_PATH}${1}/bin/activate
else
# Else throw an error message
echo "Sorry, you don't have any virtual environment with the name: ${1}"
_setv_help_
fi
;;
esac
elif [ $# -le 5 ];
then
case "${2}" in
-p|--python) _setv_custom_python_path ${3} ${4};;
*) _setv_help_;;
esac
fi
}

# Calls bash-complete. The compgen command accepts most of the same
# options that complete does but it generates results rather than just
# storing the rules for future use.
complete -F _setvcomplete_ setv

via: https://opensource.com/article/20/1/setv-bash-function

作者:Sachin Patil 选题:lujun9972 译者:wxy 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


setV:一个管理 Python 虚拟环境的 Bash 函数
https://linuxcat.top/article-11800-1.html
作者
Sachin Patil
发布于
2020年1月19日
许可协议
CC-BY-NC