本文介绍了如何源一个Bash脚本的virtualenv激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建一个Bash脚本来激活一个Python的virtualenv?
How do you create a Bash script to activate a Python virtualenv?
我有一个像目录结构:
.env
bin
activate
...other virtualenv files...
src
shell.sh
...my code...
我可以通过激活我的virtualenv:
I can activate my virtualenv by:
user@localhost:src$ . ../.env/bin/activate
(.env)user@localhost:src$
不过,从Bash脚本做同样什么都不做:
However, doing the same from a Bash script does nothing:
user@localhost:src$ cat shell.sh
#!/bin/bash
. ../.env/bin/activate
user@localhost:src$ ./shell.sh
user@localhost:src$
我在做什么错了?
What am I doing wrong?
推荐答案
在源代码,你加载脚本激活到Active外壳。
When you source, you're loading the activate script into your active shell.
当您在脚本中做到这一点,你把它加载到shell时你的脚本完成后,你又回到了原来的,未激活的外壳而退出。
When you do it in a script, you load it into that shell which exits when your script finishes and you're back to your original, unactivated shell.
您最好的办法是做一个函数
Your best option would be to do it in a function
activate () {
. ../.env/bin/activate
}
或别名
alias activate=". ../.env/bin/activate"
希望这有助于。
这篇关于如何源一个Bash脚本的virtualenv激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!