Macos 为什么在.sh文件中使用virtualenv命令会给出;未找到命令";?

Macos 为什么在.sh文件中使用virtualenv命令会给出;未找到命令";?,macos,bash,sh,Macos,Bash,Sh,我正在尝试在Mac(Mavericks)上自动删除和重新创建virtualenv 我有一个文件clean\u venv.sh: #!/bin/bash echo "Start" deactivate rmvirtualenv test mkvirtualenv test 这使得: Start ./clean_venv.sh: line 3: deactivate: command not found ./clean_venv.sh: line 4: rmvirtualenv: command

我正在尝试在Mac(Mavericks)上自动删除和重新创建virtualenv

我有一个文件
clean\u venv.sh

#!/bin/bash
echo "Start"
deactivate
rmvirtualenv test
mkvirtualenv test
这使得:

Start
./clean_venv.sh: line 3: deactivate: command not found
./clean_venv.sh: line 4: rmvirtualenv: command not found
./clean_venv.sh: line 5: mkvirtualenv: command not found

但是,在同一位置运行命令可以很好地工作。为什么会这样

虚拟环境是当前shell进程的一项功能。启动新的shell进程(如运行shell脚本)将创建一个不继承虚拟环境的新进程

鉴于此,您实际上不需要停用
。确保其他命令位于
路径
中,或者如果它们是函数,则从虚拟环境的启动文件导入后,即可调用这些命令

或者,在当前shell中定义一个函数,并使用它

clean_venv () {
    echo "Useless noise here."
    deactivate
    rmvirtualenv test
    mkvirtualenv test
}
受此启发,我发现以下代码很有效:

#!/bin/bash
source `which virtualenvwrapper.sh`
mkvirtualenv temp    # This makes sure I'm not on the test virtualenv,      
workon temp          # otherwise I can't delete it. deactivate doesn't
                     # work for some reason
rmvirtualenv test
mkvirtualenv test
workon test
rmvirtualenv temp
pip install -r requirements.txt; 

这感觉有点不舒服,但它达到了预期的效果。更新
requirements.txt
后,一个命令可以从头开始重新创建virtualenv。最后我有一个虚拟环境(
test
)和
temp
不再存在。

你的标题是
sh
,但是标签和问题是
bash
。这是两个不同的贝壳。Bash与股票Bourne shell向上兼容,但它们是不同的;即使
/bin/sh
是Bash的符号链接,它也会导致Bash在POSIX兼容模式下运行,这会以多种方式改变其行为。也许是更好的表达方式。此外,我还找到了一个解决办法,我已经发布了一个答案