edkalel edkalel - 2 years ago 413
Python Question

how to deactivate virtualenv from a bash script

I'm a novice in shell scripting, but I want to make a bash script for activate/deactivate a virtual enviroment using virtualenv.
Then I want to use this script like a service in Ubuntu copying it inside /etc/init.d folder.

In my script, I have a variable like this:


This variable represents the bin path in my virtual enviroment.

Inside the script, I can activate the virtual enviroment with this statement:
. ${VENV}/activate

This is possible because activate is a file inside bin directory in the virtual enviroment.

But I don't know the statement to use in my script to deactivate my virtual enviroment.
I can't do this:
. ${VENV}/deactivate

The problem is that doesn't exist a file named deactivate, but deactivated is a function inside the bin/activate file in the virtual enviroment.

Answer Source

It'll be hard to make a service like that useful.

. ${VENV}/activate # note the dot


source ${VENV}/activate

will source the activate script, i.e. run its contents as if they were part of the shell or script where you source them. virtualenvironment's activate is designed for this usage. In contrast, just executing the script normally with

${VENV}/activate # note: NO dot and NO 'source' command

will run its content in a subshell and won't have any useful effect.

However, your service script will already run in a subshell of its own. So except for any python commands you run as part of the service start process, it won't have any effect.

On the plus side, you won't even have to care about de-activating the environment, unless you want to run even more python stuff in the service start process, but outside of your virtualenv.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download