david
david

Reputation: 6805

What does "source" the bin/activate script mean?

This is in reference to a response I received that said I need to source this script in order to active the virtualenv.

No idea what that means, beginner here trying to figure out virtualenv.

Upvotes: 9

Views: 4903

Answers (1)

paxdiablo
paxdiablo

Reputation: 882656

To source a script is to run it in the context of the current shell rather than running it in a new shell.

For example:

. myscript.sh

or:

source myscript.sh

(depending on which shell you're running).

If you run the script in its own shell, any changes it makes to the environment are in that shell rather than the one you call it from. By sourcing it, you can affect the environment of the current shell.

For example, examine the following transcript:

pax> cat script.sh 
export xyzzy=plugh
echo $xyzzy

pax> export xyzzy=twisty

pax> echo $xyzzy ; script.sh ; echo $xyzzy
twisty
plugh
twisty

pax> echo $xyzzy ; . script.sh ; echo $xyzzy
twisty
plugh
plugh

When you run the script (different shell), it sets xyzzy to plugh but that gets lost when the shell exits back to your original shell. You'll find the original value has been "restored" (in quotes because the original value was never actually changed, only a copy of it was).

When you source it, it's just as if you typed the commands within the current shell so the effect on the variables is persistent.

Upvotes: 20

Related Questions