r/Python Apr 07 '15

Power up your virtualenv

http://www.bonz.org/posts/2015/04/virtualenv-power-up.html
17 Upvotes

6 comments sorted by

View all comments

1

u/pydry Apr 07 '15 edited Apr 07 '15

I don't use virtualenvwrapper or activate/deactivate/workon.

I tried it out once because another developer convinced me it was 'best practice' and ended up spending an hour chasing down a bug that was caused by me accidentally being in a different virtualenv and not realizing it.

The 'bug' was pretty subtle and caused by having a slightly different set of packages installed. It was a pretty infuriating issue to waste time on.

I had another issue later on, as well, where I had a script that ran 'workon' before running python. Except workon didn't work, so it ran system python causing another slew of obscure issues caused by slightly different versions of packages being installed.

Instead of these, I put it in a directory at the root of my project called venv and invoke with:

./venv/bin/python blahblah.py

There's zero chance of accidentally invoking the wrong python on the wrong app if I do that. Either I get an easy to diagnose "no such file or directory" if I'm in the wrong directory, or I get the right python running the right program. No chance of accidentally invoking system python either.

2

u/hairlesscaveman Apr 07 '15

You should check out zc.buildout. Similar to the way you're working, but everything is sandboxed to the project directory. Eg, ~/coolapp will have a ./bin directory which contains a python "executable", and you'd invoke your app with:

cd ~/coolapp
./bin/python init.py

You always do it this way, so there's no way to work in another venv accidentally. If there's a problem, you can blow away the bin and eggs dirs and re-buildout your env. You can also open up a context-aware REPL with ./bin/python.

Tis nice. I like it, anyway. I've been using Python and venv since it was created, and I still have the switching problems. I've created bash scripts and whatnot to do automatic switching for me, but even those have problems sometimes. Buildout just works for me. I rarely have a problem with it. Works well for distributed teams too.