setV:一个管理 Python 虚拟环境的 Bash 函数 | Linux 中国

Posted Linux中国

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了setV:一个管理 Python 虚拟环境的 Bash 函数 | Linux 中国相关的知识,希望对你有一定的参考价值。

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

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

◈ 默认使用 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. setv --new rango  # setv -n rango
  2. # 或使用定制的 Python 路径
  3. setv --new --python /opt/python/python3 rango  # setv -n -p /opt/python/python3 rango

激活已有的虚拟环境

  
    
    
  
  1. setv VIRTUAL_ENVIRONMENT_NAME
  
    
    
  
  1. # 示例
  2. setv rango

列出所有的虚拟环境

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

删除虚拟环境

  
    
    
  
  1. setv --delete rango

切换到另外一个虚拟环境

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

制表符补完

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

参与其中

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

setV 脚本

  
    
    
  
  1. #!/usr/bin/env bash
  2. # setV - A Lightweight Python virtual environment manager.
  3. # Author: Sachin (psachin) <iclcoolster@gmail.com>
  4. # Author's URL: https://psachin.gitlab.io/about
  5. #
  6. # License: GNU GPL v3, See LICENSE file
  7. #
  8. # Configure(Optional):
  9. # Set `SETV_VIRTUAL_DIR_PATH` value to your virtual environments
  10. # directory-path. By default it is set to '~/virtualenvs/'
  11. #
  12. # Usage:
  13. # Manual install: Added below line to your .bashrc or any local rc script():
  14. # ---
  15. # source /path/to/virtual.sh
  16. # ---
  17. #
  18. # Now you can 'activate' the virtual environment by typing
  19. # $ setv <YOUR VIRTUAL ENVIRONMENT NAME>
  20. #
  21. # For example:
  22. # $ setv rango
  23. #
  24. # or type:
  25. # setv [TAB] [TAB] (to list all virtual envs)
  26. #
  27. # To list all your virtual environments:
  28. # $ setv --list
  29. #
  30. # To create new virtual environment:
  31. # $ setv --new new_virtualenv_name
  32. #
  33. # To delete existing virtual environment:
  34. # $ setv --delete existing_virtualenv_name
  35. #
  36. # To deactivate, type:
  37. # $ deactivate
  38. # Path to virtual environment directory
  39. SETV_VIRTUAL_DIR_PATH="$HOME/virtualenvs/"
  40. # Default python version to use. This decides whether to use `virtualenv` or `python3 -m venv`
  41. SETV_PYTHON_VERSION=3 # Defaults to Python3
  42. SETV_PY_PATH=$(which python${SETV_PYTHON_VERSION})
  43. function _setvcomplete_()
  44. {
  45. # Bash-autocompletion.
  46. # This ensures Tab-auto-completions work for virtual environment names.
  47. local cmd="${1##*/}" # to handle command(s).
  48. # Not necessary as such. 'setv' is the only command
  49. local word=${COMP_WORDS[COMP_CWORD]} # Words thats being completed
  50. local xpat='${word}' # Filter pattern. Include
  51. # only words in variable '$names'
  52. local names=$(ls -l "${SETV_VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}') # Virtual environment names
  53. COMPREPLY=($(compgen -W "$names" -X "$xpat" -- "$word")) # compgen generates the results
  54. }
  55. function _setv_help_() {
  56. # Echo help/usage message
  57. echo "Usage: setv [OPTIONS] [NAME]"
  58. echo Positional argument:
  59. echo -e "NAME Activate virtual env."
  60. echo Optional arguments:
  61. echo -e "-l, --list List all Virtual Envs."
  62. echo -e "-n, --new NAME Create a new Python Virtual Env."
  63. echo -e "-d, --delete NAME Delete existing Python Virtual Env."
  64. echo -e "-p, --python PATH Python binary path."
  65. }
  66. function _setv_custom_python_path()
  67. {
  68. if [ -f "${1}" ];
  69. then
  70. if [ "`expr $1 : '.*python\([2,3]\)'`" = "3" ];
  71. then
  72. SETV_PYTHON_VERSION=3
  73. else
  74. SETV_PYTHON_VERSION=2
  75. fi
  76. SETV_PY_PATH=${1}
  77. _setv_create $2
  78. else
  79. echo "Error: Path ${1} does not exist!"
  80. fi
  81. }
  82. function _setv_create()
  83. {
  84. # Creates new virtual environment if ran with -n|--new flag
  85. if [ -z ${1} ];
  86. then
  87. echo "You need to pass virtual environment name"
  88. _setv_help_
  89. else
  90. echo "Creating new virtual environment with the name: $1"
  91. if [ ${SETV_PYTHON_VERSION} -eq 3 ];
  92. then
  93. ${SETV_PY_PATH} -m venv ${SETV_VIRTUAL_DIR_PATH}${1}
  94. else
  95. virtualenv -p ${SETV_PY_PATH} ${SETV_VIRTUAL_DIR_PATH}${1}
  96. fi
  97. echo "You can now activate the Python virtual environment by typing: setv ${1}"
  98. fi
  99. }
  100. function _setv_delete()
  101. {
  102. # Deletes virtual environment if ran with -d|--delete flag
  103. # TODO: Refactor
  104. if [ -z ${1} ];
  105. then
  106. echo "You need to pass virtual environment name"
  107. _setv_help_
  108. else
  109. if [ -d ${SETV_VIRTUAL_DIR_PATH}${1} ];
  110. then
  111. read -p "Really delete this virtual environment(Y/N)? " yes_no
  112. case $yes_no in
  113. Y|y) rm -rvf ${SETV_VIRTUAL_DIR_PATH}${1};;
  114. N|n) echo "Leaving the virtual environment as it is.";;
  115. *) echo "You need to enter either Y/y or N/n"
  116. esac
  117. else
  118. echo "Error: No virtual environment found by the name: ${1}"
  119. fi
  120. fi
  121. }
  122. function _setv_list()
  123. {
  124. # Lists all virtual environments if ran with -l|--list flag
  125. echo -e "List of virtual environments you have under ${SETV_VIRTUAL_DIR_PATH}:\n"
  126. for virt in $(ls -l "${SETV_VIRTUAL_DIR_PATH}" | egrep '^d' | awk -F " " '{print $NF}')
  127. do
  128. echo ${virt}
  129. done
  130. }
  131. function setv() {
  132. # Main function
  133. if [ $# -eq 0 ];
  134. then
  135. _setv_help_
  136. elif [ $# -le 3 ];
  137. then
  138. case "${1}" in
  139. -n|--new) _setv_create ${2};;
  140. -d|--delete) _setv_delete ${2};;
  141. -l|--list) _setv_list;;
  142. *) if [ -d ${SETV_VIRTUAL_DIR_PATH}${1} ];
  143. then
  144. # Activate the virtual environment
  145. source ${SETV_VIRTUAL_DIR_PATH}${1}/bin/activate
  146. else
  147. # Else throw an error message
  148. echo "Sorry, you don't have any virtual environment with the name: ${1}"
  149. _setv_help_
  150. fi
  151. ;;
  152. esac
  153. elif [ $# -le 5 ];
  154. then
  155. case "${2}" in
  156. -p|--python) _setv_custom_python_path ${3} ${4};;
  157. *) _setv_help_;;
  158. esac
  159. fi
  160. }
  161. # Calls bash-complete. The compgen command accepts most of the same
  162. # options that complete does but it generates results rather than just
  163. # storing the rules for future use.
  164. complete -F _setvcomplete_ setv

via: 

本文由  原创编译, 荣誉推出

以上是关于setV:一个管理 Python 虚拟环境的 Bash 函数 | Linux 中国的主要内容,如果未能解决你的问题,请参考以下文章

1.0Python虚拟环境创建与管理

用pipenv管理python虚拟环境

Linux系统Python虚拟环境管理软件pyenv

[python] virtualenv和virtualenvwrapper管理python虚拟环境

[python] virtualenv和virtualenvwrapper管理python虚拟环境

Python虚拟环境管理 之 virtualenv 和 pipenv