Virtualenv with sudo

Virtualenv is a great way to manage Python environments. Today I ran into an issue when I had to run the script parts of which required sudo privileges and the rest of it needed all my virtual environment packages. When we run sudo all virtualenv settings (environment variables, aliases, functions, etc) become unavailable.
Virtualenv user guide offers following solution:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

Adding this lines to my script saved me a lot of trouble – now I have my environment available when I run sudo!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s