How to source virtualenv activate in a Bash script

bash, python, virtualenv

Solution

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"

Problem

How do you create a Bash script to activate a Python virtualenv? I have a directory structure like: ``` .env bin activate ...other virtualenv files... src shell.sh ...my code... ``` I can activate my virtualenv by: ``` user@localhost:src$ . ../.env/bin/activate (.env)user@localhost:src$ ``` 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?

Original source

Related problems