无法通过 bash 脚本激活 virtualenv

pau*_*fon 4 virtualenv python-3.x

我在 python 的 virtualenv 中运行一个项目。这是 virtualenv 的路径。

~/iss/issp/bin
Run Code Online (Sandbox Code Playgroud)

问题是当我尝试使用以下命令运行激活脚本时:

source activate
Run Code Online (Sandbox Code Playgroud)

它引发以下错误。

:~/iss/issp/bin$ source activate
: command not found
bash: activate: line 4: syntax error near unexpected token `$'{\r''
'ash: activate: line 4: `deactivate () {
Run Code Online (Sandbox Code Playgroud)

这是脚本中的代码:

# 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 [ ! "$1" = "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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 7

The issue is that the activate script has Windows line endings. We can confirm this by running the below command on the command line.

$ file activate
Run Code Online (Sandbox Code Playgroud)

which returns

activate: ASCII text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)

CRLF line terminators means windows line endings.

Therefore we need to convert them to Unix line endings so that we can source the file.

We have a few of options

  1. dos2unix activate - (this edits the file in place)
  2. sed -i 's/\r$//' activate (also edits the file in place)
  3. mv activate activate_with_windows_line_endings && (tr -d '\r' < activate_with_windows_line_endings ) > activate here we we just remove any occurrence of \r and the original file is preserved.
  4. Open the file in your favourite text editor. Most will have a way to both show the current line endings of the file (bottom right of image below) and a way to convert them to Unix line endings (In Notepad++ we go to Edit ? EOL Conversion ? Unix (LF) and then save. Here is a screenshot of how to do it in Notepad++ 在此处输入图片说明

Finally

Now source activate should work.


Арс*_*гин 5

刚刚遇到了同样的问题,并决定弄清楚hexdump -C bin/activate。结果我的 bin/activate 文件有 CR/LF 行结尾,而不仅仅是 CR,更改它们并(tr -d '\r' < bin/activate) > bin/activate解决了我的问题。

  • 它删除文件的全部内容 (4认同)

pro*_*ome 5

venv我使用 VS Code 和python 标准库中的包遇到了这个问题,因为我使用 bash 作为默认终端。

Bash 脚本应始终使用 LF 而不是 CRLF 行结尾。现在,这个问题已针对 python包修复virtualenv,但对于venv.

如此处所述,https://bugs.python.org/issue43437在创建新的虚拟环境时以二进制模式venv复制其activate脚本模板。因此,我们只需使用上述任何Mark 的方法将原始行结尾转换为 Unix 行结尾即可。

激活脚本模板位于此处:<python3_root>/Lib/venv/scripts/common/activate

要将其固定到位:sed -i 's/\r$//' activate

只要该文件保留其 LF 行结尾,创建的新虚拟环境venv就应该具有可行的激活脚本。