【问题标题】:Unable to activate virtualenv via bash script无法通过 bash 脚本激活 virtualenv
【发布时间】:2015-09-07 18:36:35
【问题描述】:

我正在 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 [ ! "$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

【问题讨论】:

    标签: python-3.x virtualenv


    【解决方案1】:

    问题在于activate 脚本有Windows 行尾。我们可以通过在命令行上运行以下命令来确认这一点。

    $ file activate
    

    返回

    activate: ASCII text, with CRLF line terminators
    

    CRLF 行终止符表示 windows 行结束符。

    因此我们需要将它们转换为 Unix 行尾,以便我们可以source 文件。

    我们有几个选择

    1. dos2unix activate - (这会就地编辑文件)
    2. sed -i 's/\r$//' activate(也可以就地编辑文件)
    3. mv activate activate_with_windows_line_endings && (tr -d '\r' < activate_with_windows_line_endings ) > activate 在这里我们只是删除所有出现的\r 并保留原始文件。
    4. 在您喜欢的文本编辑器中打开文件。大多数将有一种方法来显示文件的当前行尾(下图右下角)和一种将它们转换为 Unix 行尾的方法(在 Notepad++ 中,我们转到 Edit → EOL Conversion → Unix (LF) 然后保存。这是截图如何在记事本++中做到这一点

    终于

    现在source activate 应该可以工作了。

    【讨论】:

    • 虽然我个人建议使用 WSL 并避免所有这些 windows 问题。
    【解决方案2】:

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

    Bash 脚本应始终使用 LF 而不是 CRLF 行尾。这是python virtualenv 包的now fixed,但它仍然是venv 的问题。

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

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

    修复它:sed -i 's/\r$//' activate

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

    【讨论】:

      【解决方案3】:

      也许你的 .bashrc 文件中有一个别名,这就是为什么 deactivate 需要像命令,而不是函数

      改为

      deactivate() {
      

      使用这个

      function deactivate() {
      

      【讨论】:

        【解决方案4】:

        刚遇到同样的问题,决定做hexdump -C bin/activate来解决。原来我的 bin/activate 文件有 CR/LF 行结尾而不仅仅是 CR,用(tr -d '\r' < bin/activate) > bin/activate修复了我的问题。

        【讨论】:

        • 关于如何做到这一点的更多解释(tr -d '\r' < bin/activate) > bin/activate也许?
        • @Arturo 这是一个 shell 命令,将它复制粘贴到你的 shell 中,它可能会起作用。
        • 删除文件的全部内容
        • as nish cmets 命令为我删除了文件的内容。
        • 我这样做了,它似乎有效: $ cd .venv/Scripts/ $ cp -p activate activate.abo $ (tr -d '\r' 激活
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 2010-12-14
        • 2020-12-20
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多