How to source virtualenv activate in a Bash script
When you source, you're loading the activate script into your active 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}
or an alias
alias activate=". ../.env/bin/activate"
Hope this helps.
You should call the bash script using source.
Here is an example:
#!/bin/bash# Let's call this script venv.shsource "<absolute_path_recommended_here>/.env/bin/activate"
On your shell just call it like that:
> source venv.sh
Or as @outmind suggested: (Note that this does not work with zsh)
> . venv.sh
There you go, the shell indication will be placed on your prompt.
Although it doesn't add the "(.env)" prefix to the shell prompt, I found this script works as expected.
#!/bin/bashscript_dir=`dirname $0`cd $script_dir/bin/bash -c ". ../.env/bin/activate; exec /bin/bash -i"
e.g.
user@localhost:~/src$ which pip/usr/local/bin/pipuser@localhost:~/src$ which python/usr/bin/pythonuser@localhost:~/src$ ./shelluser@localhost:~/src$ which pip~/.env/bin/pipuser@localhost:~/src$ which python~/.env/bin/pythonuser@localhost:~/src$ exitexit