FinnM
FinnM

Reputation: 411

How can I run a cleanup script on virtualenv `deactivate`?

Via autoenv, whenever I cd into a directory with a .env file, I automatically activate my python virtual environment and set some environment variables. This .env file by essence is a setup script.

# /absolute/path/to/project/.env
source /absolute/path/to/project/.venv/bin/activate

export ENV_VAR_1="foo"
export ENV_VAR_2="bar"

But when I'm done working on my project and I type deactivate

Is there a clean way for me to run a teardown script when I call deactivate that does some project clean up when I'm done for the day?

Upvotes: 0

Views: 326

Answers (1)

user2390182
user2390182

Reputation: 73490

Inside the activate script, there is a deactivate block in which you can unset the variables:

deactivate () {
    # ...
    unset ENV_VAR_1
    unset ENV_VAR_2
}

Upvotes: 1

Related Questions