【问题标题】:which mkvirtualenv returning false (1) but still executeswhich mkvirtualenv 返回 false (1) 但仍执行
【发布时间】:2018-09-07 16:27:00
【问题描述】:

有人可以向我解释为什么发出以下命令(在终端中)

which mkvirtualenv
echo "Return Value: $?"
mkvirtualenv test
deactivate

成功输出如下

$Return Value: 1
Using base prefix '/usr'
New python executable in /home/xxxxxx/.virtualenvs/test/bin/python3
Not overwriting existing python script /home/xxxxxx/.virtualenvs/test/bin/python (you must use /home/xxxxxx/.virtualenvs/test/bin/python3)
Installing setuptools, pip, wheel...done.
(test)$
$

根据which用户手册,它返回并退出代码1 if

如果一个或多个指定的命令不存在或不可执行

因此,正在执行一个不存在或不可执行的mkvirtualenv 命令。我认为这个问题的答案可以解释为什么当我在 bash 脚本中执行相同的代码行时,像这样

#!/bin/bash
which mkvirtualenv
echo "Return Value: $?"
mkvirtualenv test
deactivate

我得到以下输出

$ Return Value: 1
$ ./test.sh: line 5: mkvirtualenv: command not found
$ ./test.sh: line 6: deactivate: command not found

谁能告诉我为什么它在脚本中失败,因为它导致我的一个安装文件失败。

编辑:

根据this answer,只需在每个 bash 脚本中获取 virtualenvwrapper.sh 文件是解决此问题的一种破解方法。

【问题讨论】:

  • 你的终端使用什么外壳?
  • @0.sh /bin/bash

标签: bash


【解决方案1】:

当您以/bin/bash 运行脚本时,您没有使用用户上下文,而是使用一般上下文(我缺乏正确的术语,请耐心等待)基本上您将无法访问您可能碰巧在 bashrc 中设置的所有 ENV 变量/profilerc/类似。碰巧 virtualenvwrapper 会要求您设置一些东西。

如何解决?

/bin/bash 更改为/usr/bin/env bash,脚本的行为将与您在shell 中编写此代码时完全一样。

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2021-07-11
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多