如何从 bash 脚本停用 virtualenv
how to deactivate virtualenv from a bash script
我是 shell 脚本的新手,但我想使用 virtualenv 为 activate/deactivate 虚拟环境制作一个 bash 脚本。
然后我想像 Ubuntu 中的服务一样使用此脚本,将其复制到 /etc/init.d 文件夹中。
在我的脚本中,我有一个这样的变量:
VENV=/opt/odoo/odoo_server/venv_oddo/bin
此变量表示我的虚拟环境中的 bin 路径。
在脚本中,我可以使用以下语句激活虚拟环境:
. ${VENV}/activate
这是可能的,因为 activate 是虚拟环境中 bin 目录中的一个文件。
但我不知道在我的脚本中使用什么语句来停用我的虚拟环境。
我不能这样做:. ${VENV}/deactivate
问题是不存在名为 deactivate 的文件,但 deactivated 是虚拟环境中 bin/activate 文件中的一个函数。
就deactivate
。只要您使用 bash.
,它就可以在脚本和命令行中使用
编辑:在大多数情况下,最好在脚本和服务中拼写完整的 python 路径。它是无状态的,更便携,几乎可以在任何地方使用。所以与其做
. $VENV/bin/activate
/path/to/my/script.py --parameters
通常最好做
$VENV/bin/python /path/to/my/script --parameters
相信我,它会为您节省调试时间)
很难提供这样有用的服务。
. ${VENV}/activate # note the dot
或
source ${VENV}/activate
将source the activate
script, i.e. run its contents as if they were part of the shell or script where you source them. virtualenvironment
's activate
is designed for this usage。相反,只需使用
正常执行脚本
${VENV}/activate # note: NO dot and NO 'source' command
将运行它的内容放在子外壳中,不会有任何有用的效果。
但是,您的服务脚本已经 运行 在它自己的子 shell 中。因此,除了您 运行 作为服务启动过程的一部分的任何 python 命令外,它不会有任何影响。
从好的方面来说,您甚至不必关心停用环境,除非您想要 运行 更多 python 服务启动过程中的东西,但在外部你的虚拟环境。
在 ${VENV}/activate 中复制停用代码。
粘贴你的 ~/.bashrc
deactivate() {
# reset old environment variables
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
hash -r
fi
if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
运行命令。
$ $VENV/activate
$ deactivate
python 2.7和python 3.5我都是这样选择性使用的,没问题
我想知道差评的原因
virtualenvwrapper
提供的deactivate
"command"实际上是一个shell函数,workon
也是如此。如果您有一个虚拟环境处于活动状态,则可以使用 typeset -F
.
列出这些函数的名称
为了在脚本中使用它们,需要在那里定义它们,因为 shell 函数不会传播到子 shells。
要定义这些函数,请在您打算调用这些函数的 shell 脚本中获取 virtualenvwrapper.sh
脚本,例如:
source $(which virtualenvwrapper.sh)
这使您可以像在 shell:
中那样在 shell 脚本中调用这些函数
deactivate
更新: 我所描述的适用于 virtualenvwrapper 提供的其他功能(例如 workon
)。我错误地认为它也适用于停用,但这是一个更复杂的情况,因为它是一个仅在 shell 中定义的函数,其中 workon
或 activate
是 运行.
如果您只需要以编程方式禁用/更改 virtualenv,您可以使用 shell 函数而不是 shell 脚本 。例如,放在 ~/.bashrc
或 ~/.bash_aliases
(如果您已设置)或 ~/.zshrc
或 ~/.zsh_aliases
(如果您使用 zsh
)的末尾:
function ch() {
# change this to your directory
cd ~/git-things/my_other_py_project
# this works, as a shell function won't spawn a subshell as the script would
deactivate
# re-source to change the virtualenv (my use case; change to fit yours)
source .venv-myotherpyproject/bin/activate
}
重新启动 shell 或重新获取您使用 source ~/.zsh_aliases
更改的文件并使用命令 ch
执行该功能。
不知何故 deactivate
在我的案例中也找不到(通常我在 bash[=20 中的 far2l 下工作=]).我使用的解决方案:
unset VIRTUAL_ENV & deactivate
之后 pip -V
在 .local.
中显示路径
我是 shell 脚本的新手,但我想使用 virtualenv 为 activate/deactivate 虚拟环境制作一个 bash 脚本。 然后我想像 Ubuntu 中的服务一样使用此脚本,将其复制到 /etc/init.d 文件夹中。
在我的脚本中,我有一个这样的变量:
VENV=/opt/odoo/odoo_server/venv_oddo/bin
此变量表示我的虚拟环境中的 bin 路径。
在脚本中,我可以使用以下语句激活虚拟环境:
. ${VENV}/activate
这是可能的,因为 activate 是虚拟环境中 bin 目录中的一个文件。
但我不知道在我的脚本中使用什么语句来停用我的虚拟环境。
我不能这样做:. ${VENV}/deactivate
问题是不存在名为 deactivate 的文件,但 deactivated 是虚拟环境中 bin/activate 文件中的一个函数。
就deactivate
。只要您使用 bash.
编辑:在大多数情况下,最好在脚本和服务中拼写完整的 python 路径。它是无状态的,更便携,几乎可以在任何地方使用。所以与其做
. $VENV/bin/activate
/path/to/my/script.py --parameters
通常最好做
$VENV/bin/python /path/to/my/script --parameters
相信我,它会为您节省调试时间)
很难提供这样有用的服务。
. ${VENV}/activate # note the dot
或
source ${VENV}/activate
将source the activate
script, i.e. run its contents as if they were part of the shell or script where you source them. virtualenvironment
's activate
is designed for this usage。相反,只需使用
${VENV}/activate # note: NO dot and NO 'source' command
将运行它的内容放在子外壳中,不会有任何有用的效果。
但是,您的服务脚本已经 运行 在它自己的子 shell 中。因此,除了您 运行 作为服务启动过程的一部分的任何 python 命令外,它不会有任何影响。
从好的方面来说,您甚至不必关心停用环境,除非您想要 运行 更多 python 服务启动过程中的东西,但在外部你的虚拟环境。
在 ${VENV}/activate 中复制停用代码。
粘贴你的 ~/.bashrc
deactivate() {
# reset old environment variables
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
hash -r
fi
if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
运行命令。
$ $VENV/activate
$ deactivate
python 2.7和python 3.5我都是这样选择性使用的,没问题
我想知道差评的原因
virtualenvwrapper
提供的deactivate
"command"实际上是一个shell函数,workon
也是如此。如果您有一个虚拟环境处于活动状态,则可以使用 typeset -F
.
为了在脚本中使用它们,需要在那里定义它们,因为 shell 函数不会传播到子 shells。
要定义这些函数,请在您打算调用这些函数的 shell 脚本中获取 virtualenvwrapper.sh
脚本,例如:
source $(which virtualenvwrapper.sh)
这使您可以像在 shell:
中那样在 shell 脚本中调用这些函数deactivate
更新: 我所描述的适用于 virtualenvwrapper 提供的其他功能(例如 workon
)。我错误地认为它也适用于停用,但这是一个更复杂的情况,因为它是一个仅在 shell 中定义的函数,其中 workon
或 activate
是 运行.
如果您只需要以编程方式禁用/更改 virtualenv,您可以使用 shell 函数而不是 shell 脚本 。例如,放在 ~/.bashrc
或 ~/.bash_aliases
(如果您已设置)或 ~/.zshrc
或 ~/.zsh_aliases
(如果您使用 zsh
)的末尾:
function ch() {
# change this to your directory
cd ~/git-things/my_other_py_project
# this works, as a shell function won't spawn a subshell as the script would
deactivate
# re-source to change the virtualenv (my use case; change to fit yours)
source .venv-myotherpyproject/bin/activate
}
重新启动 shell 或重新获取您使用 source ~/.zsh_aliases
更改的文件并使用命令 ch
执行该功能。
不知何故 deactivate
在我的案例中也找不到(通常我在 bash[=20 中的 far2l 下工作=]).我使用的解决方案:
unset VIRTUAL_ENV & deactivate
之后 pip -V
在 .local.