Live data from Hacker News

Virtualenv's bin/activate is Doing It Wrong

gist.github.com

21–30 of 31 posts

Re: Virtualenv's bin/activate is Doing It Wrong

#21

The fact that it does not use a subshell makes it useful. You can trivially use it in bash scripts and cronjobs.

Because "eval `myscript`" is somehow non-trivial?

No, this is just bad. The whole paradigm of having build setup depend on local variables in an interactive shell (which is pervasive, virtualenv is hardly the only bad actor here) is just broken inherently. It's lazy programming. It creates all sorts of failure modes -- builds can magically stop working when people update their .bashrc files (e.g. set PATH explicitly, but OOPS the build system expects to source a script and then spawn you an interactive shell), etc...

Implicit configuration is bad, OK? Just don't do it.

Re: Virtualenv's bin/activate is Doing It Wrong

#23

The approach i use is simply: $ cat run #!/bin/sh . venv/bin/activate python main.py Is this wrong? If so, what would work better?

Not wrong, but if you're just running a program under a virtualenv (as opposed to doing shell work), just:

/path/to/env/bin/python main.py

will suffice. No need to create a separate shell script, start a new shell, and activate. Great for cronjobs.

Re: Virtualenv's bin/activate is Doing It Wrong

#25

Ah, I like this. I'm going to cavort off with these ideas and put them to good use in my rubygems environment switcher gemenv.

...aaaaand done. At least, the subshell launcher part. https://github.com/regularfry/gemenv, if anyone 'round these parts is interested.

Re: Virtualenv's bin/activate is Doing It Wrong

#26
This is also the same technique I like to use for providing configuration to applications. Basically, write YOUR_APP_ENV=prod or whatever into /etc/environment and then add appenv to PATH which tests against the machine's environment, sets all the appropriate environment variables accordingly and ends with an `exec`. Super nice way to provide shell-friendly language-agnostic configuration.

I used to use `exec env $@` at the end of the script, so that I could `appenv > prod` for diffing, but I actually prefer this articles suggestion: `exec "${@:-$SHELL}"` since you can still `appenv env > prod`, but `appenv` provides an interactive shell by default.

Re: Virtualenv's bin/activate is Doing It Wrong

#27
Virtualenv bin/activate isn't doing anything "wrong". It's just not doing it how the author believes is best. Can the whole "You're doing X wrong" meme die already please?

I like the idea though! From what I see so far the author makes a good argument for using a subshell rather than setting and resetting environment variables. At the same time though, I haven't enough experience with his suggestion yet to see the downsides... and there most definitely will be. Why? Because that's programming, man. There's no panacea, no perfect way to do things, no true "best" way to do anything.

So is this the "right" way? Probably not. Better is probably more like it and using that word instead makes you seem, you know, like not cocky asshole (not that I think that about the author at all, just generally speaking).

Re: Virtualenv's bin/activate is Doing It Wrong

#28

Virtualenv bin/activate isn't doing anything "wrong". It's just not doing it how the author believes is best. Can the whole "You're doing X wrong" meme die already please? I like the idea though! From what I see so far the author makes a good argument for using a subshell rather than setting and resetting environment variables. At the same time though, I haven't enough experience with his suggestion yet to see the do…

Can the whole "You're doing X wrong" meme die already please?

You clicked through to an article you wouldn't otherwise read. The meme persists because it works, and complaining about it just makes it more effective.

(I didn't click through because I don't really care if X is doing Y wrong.)

Re: Virtualenv's bin/activate is Doing It Wrong

#30

Virtualenv bin/activate isn't doing anything "wrong". It's just not doing it how the author believes is best. Can the whole "You're doing X wrong" meme die already please? I like the idea though! From what I see so far the author makes a good argument for using a subshell rather than setting and resetting environment variables. At the same time though, I haven't enough experience with his suggestion yet to see the do…

"Virtualenv bin/activate isn't doing anything "wrong". It's just not doing it how the author believes is best."

So is it possible to do something wrong, or is it only possible to do something that another person (or you) believes is not best?

"So is this the "right" way? Probably not. Better is probably more like it ..."

So this way is better, but the other way wasn't wrong? How could this way be better?

Post reply on HN