在Bash脚本激活的virtualenv不工作 [英] Activating virtualenv in Bash script not working

查看:194
本文介绍了在Bash脚本激活的virtualenv不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写脚本来自动执行我的烧瓶环境设置。

Writing a script to automate my flask environment setup.

if [[ -z $1 ]];
    then
        echo "usage: flaskup <dirname> <template dir>";
        exit
    else
        virtualenv $1 &&
        cd ./$1 &&
        source bin/activate &&
        bin/pip install flask &&
        mkdir ./app &&
        mkdir ./app/static &&
        mkdir ./app/templates && 
        exit;
fi

我期待这个离开我在它创建的​​目录中,并激活了虚拟环境,但它让我在我从运行脚本的同一目录下。我能做些什么,使与激活虚拟环境中的壳呢?

I'm expecting this to leave me in the directory it created, with the virtual environment activated, however it leaves me in the same directory I ran the script from. What can I do to make the script exit with the shell in the activated virtual environment?

推荐答案

如果您在自己的壳运行脚本(运行它 /路径/到/脚本剧本如果它生活在你的 $ PATH ),那么你不能得到你想要的。运行该脚本的shell是不同的外壳,然后你运行它之一,它不能改变父shell的状态。你能做的最接近的将是有回声路径作为输出脚本并运行它作为光盘$(/路径/到/脚本)或相似的。

If you run the script in its own shell (run it as /path/to/script or script if it lives in your $PATH) then you can't get what you want. The shell that runs the script is a different shell then the one you ran it from and it cannot change the status of the parent shell. The closest you could do would be to have the script echo the path as output and run it as cd "$(/path/to/script)" or similar.

另外,如果您运行脚本。 /路径/到/脚本(或类似),那么你与你当前的shell中运行它,任何目录更改它使会在你当前的shell,而不是一个子shell发生。

Alternatively, if you run the script as . /path/to/script (or similar) then you are running it with your current shell and any directory changes it makes will be happening in your current shell and not a sub-shell.

这篇关于在Bash脚本激活的virtualenv不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆