是否可以运行一个以我的 python virtualenv 为源的 shell 脚本,然后让 shell 处于新环境中?
这是我的shell脚本
#!/usr/bin/env bash
function createProject() {
if [ -e $1 ]
then
rm -r ./$1
fi
if [ -e env-$1 ]
then
rm -r ./env-$1
fi
virtualenv ./env-$1
django-admin startproject $1
}
createProject $1
source ./env-$1/bin/activate
exit 1
然后我运行 ./script.sh hello-world。 基本上,如果我要运行 source ./hello-world/bin/activate 我的 shell,virualenv 将被激活,然后 shell 将在新环境中运行。 我该如何实现?
请您参考如下方法:
您只需要使用 shell 函数就可以实现,因为它们不会产生单独的进程。 您的方法的问题是 virtualenv 在为运行 shell 脚本而创建的子进程中被激活。
不要使用可执行的 shell 脚本,而是这样做:
function createProject() {
if [ -e $1 ]
then
rm -r ./$1
fi
if [ -e env-$1 ]
then
rm -r ./env-$1
fi
virtualenv ./env-$1
django-admin startproject $1
source ./env-$1/bin/activate
}
将其保存为 createProject.sh 并在 .bashrc 或 .bash_profile 中获取该文件
source createProject.sh
这样virtualenv在当前进程中被激活。
