无法通过 bash 脚本激活 virtualenv
Unable to activate virtualenv via bash script
我正在 运行在 python 的 virtualenv 中创建一个项目。这是 virtualenv 的路径。
~/iss/issp/bin
问题是当我尝试 运行 激活脚本时:
source activate
它抛出以下错误。
:~/iss/issp/bin$ source activate
: command not found
bash: activate: line 4: syntax error near unexpected token `$'{\r''
'ash: activate: line 4: `deactivate () {
脚本中的代码如下:
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
unset pydoc
# 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 2>/dev/null
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
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="/home/pablo/issp"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "$PYTHONHOME" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
fi
fi
export PS1
fi
alias pydoc="python -m pydoc"
# 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 2>/dev/null
fi
刚遇到同样的问题,决定hexdump -C bin/activate
弄清楚。原来我的 bin/activate 文件有 CR/LF 行结尾,而不仅仅是 CR,用 (tr -d '\r' < bin/activate) > bin/activate
更改它们解决了我的问题。
也许您的 .bashrc 文件中有一个别名,这就是为什么 deactivate 就像命令,而不是函数
改为
deactivate() {
使用这个
function deactivate() {
问题是 activate
脚本有 Windows 行结尾。我们可以通过 运行 命令行中的以下命令来确认这一点。
$ file activate
哪个returns
activate: ASCII text, with CRLF line terminators
CRLF 行终止符表示 windows 行结尾。
因此我们需要将它们转换为 Unix 行结尾,以便我们可以 source
文件。
我们有几个选择
dos2unix activate
-(就地编辑文件)
sed -i 's/\r$//' activate
(同时编辑文件)
mv activate activate_with_windows_line_endings && (tr -d '\r' < activate_with_windows_line_endings ) > activate
这里我们只是删除任何出现的 \r
并保留原始文件。
- 在您喜欢的文本编辑器中打开文件。大多数都会有一种方法来显示文件的当前行尾(下图右下角)和一种将它们转换为 Unix 行尾的方法(在 Notepad++ 中,我们转到
Edit → EOL Conversion → Unix (LF)
然后保存。这是一个如何在 Notepad++ 中执行此操作的屏幕截图
终于
现在 source activate
应该可以了。
我 运行 使用 VS Code 和 python 标准库中的 venv
包解决了这个问题,因为我使用 bash 作为我的默认终端。
Bash 脚本应始终使用 LF 而不是 CRLF 行结尾。这是 python virtualenv
包的 now fixed,但 venv
.
仍然是一个问题
如上文所述,https://bugs.python.org/issue43437, venv
copies its activate
script template in binary mode when creating new virtual environments. Therefore we only need to convert the original to Unix line endings, using any of 。
激活脚本模板位于此处:<python3_root>/Lib/venv/scripts/common/activate
固定到位:sed -i 's/\r$//' activate
只要该文件保持其 LF 行结尾,使用 venv
创建的新虚拟环境应该具有可行的激活脚本。
我正在 运行在 python 的 virtualenv 中创建一个项目。这是 virtualenv 的路径。
~/iss/issp/bin
问题是当我尝试 运行 激活脚本时:
source activate
它抛出以下错误。
:~/iss/issp/bin$ source activate
: command not found
bash: activate: line 4: syntax error near unexpected token `$'{\r''
'ash: activate: line 4: `deactivate () {
脚本中的代码如下:
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
unset pydoc
# 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 2>/dev/null
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
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="/home/pablo/issp"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "$PYTHONHOME" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
fi
fi
export PS1
fi
alias pydoc="python -m pydoc"
# 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 2>/dev/null
fi
刚遇到同样的问题,决定hexdump -C bin/activate
弄清楚。原来我的 bin/activate 文件有 CR/LF 行结尾,而不仅仅是 CR,用 (tr -d '\r' < bin/activate) > bin/activate
更改它们解决了我的问题。
也许您的 .bashrc 文件中有一个别名,这就是为什么 deactivate 就像命令,而不是函数
改为
deactivate() {
使用这个
function deactivate() {
问题是 activate
脚本有 Windows 行结尾。我们可以通过 运行 命令行中的以下命令来确认这一点。
$ file activate
哪个returns
activate: ASCII text, with CRLF line terminators
CRLF 行终止符表示 windows 行结尾。
因此我们需要将它们转换为 Unix 行结尾,以便我们可以 source
文件。
我们有几个选择
dos2unix activate
-(就地编辑文件)sed -i 's/\r$//' activate
(同时编辑文件)mv activate activate_with_windows_line_endings && (tr -d '\r' < activate_with_windows_line_endings ) > activate
这里我们只是删除任何出现的\r
并保留原始文件。- 在您喜欢的文本编辑器中打开文件。大多数都会有一种方法来显示文件的当前行尾(下图右下角)和一种将它们转换为 Unix 行尾的方法(在 Notepad++ 中,我们转到
Edit → EOL Conversion → Unix (LF)
然后保存。这是一个如何在 Notepad++ 中执行此操作的屏幕截图
终于
现在 source activate
应该可以了。
我 运行 使用 VS Code 和 python 标准库中的 venv
包解决了这个问题,因为我使用 bash 作为我的默认终端。
Bash 脚本应始终使用 LF 而不是 CRLF 行结尾。这是 python virtualenv
包的 now fixed,但 venv
.
如上文所述,https://bugs.python.org/issue43437, venv
copies its activate
script template in binary mode when creating new virtual environments. Therefore we only need to convert the original to Unix line endings, using any of
激活脚本模板位于此处:<python3_root>/Lib/venv/scripts/common/activate
固定到位:sed -i 's/\r$//' activate
只要该文件保持其 LF 行结尾,使用 venv
创建的新虚拟环境应该具有可行的激活脚本。