News for hackers. Stuff that matters.
Re:Virtualenv's bin/activate is Doing It Wrong by bpatrianakos on Wednesday February 27, 2013 @12:45PM (#5293680)
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 by jrockway on Wednesday February 27, 2013 @01:03PM (#5293803)
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 by bpatrianakos on Wednesday February 27, 2013 @02:59PM (#5294510)
It does work and it makes me hate it even more! Because by the time something really is wrong I'll be immune to the meme and I'll never know what's wrong.
Re:Virtualenv's bin/activate is Doing It Wrong by kenko on Wednesday February 27, 2013 @01:35PM (#5293981)
"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?

Re:Virtualenv's bin/activate is Doing It Wrong by the_mitsuhiko on Wednesday February 27, 2013 @07:15AM (#5291948)
The fact that it does not use a subshell makes it useful. You can trivially use it in bash scripts and cronjobs.
Re:Virtualenv's bin/activate is Doing It Wrong by ajross on Wednesday February 27, 2013 @12:05PM (#5293375)
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 by IgorPartola on Wednesday February 27, 2013 @07:25AM (#5291978)
Why can you not do?:

  #!/usr/bin/virtual-bash my-env
  
  echo hello
Edit: in fact something like /usr/bin/virt-python my-env is much nicer than /var/lib/my-app/env/bin/python. Basically like virtualenvwrapper but for Python scripts that are not run interactively.
Re:Virtualenv's bin/activate is Doing It Wrong by the_mitsuhiko on Wednesday February 27, 2013 @07:33AM (#5292015)
I regularly write scripts that involve more than one virtualenv. At the moment this is trivial by just activating and deactivating envs from one script. Starting subshells is complicated because communication into the shell barely exists. Worse than that is that the shebang only supports two arguments and is heavily length limited.
Re:Virtualenv's bin/activate is Doing It Wrong by IgorPartola on Wednesday February 27, 2013 @08:02AM (#5292094)
Unless you are rapidly switching envs, you could launch the parts of your scripts that operate on different envs as subscripts. Then you can pipe data in and out. Can you provide an example where this does not work?

Point taken on the shebang limitations.

Re:Virtualenv's bin/activate is Doing It Wrong by lifeisstillgood on Wednesday February 27, 2013 @04:36AM (#5291485)
Venv is one of the pieces of sanity in the python packaging deployment world and any improvements welcome - this goes on my List if things to think hard about
Re:Virtualenv's bin/activate is Doing It Wrong by Comkid on Wednesday February 27, 2013 @05:29AM (#5291633)
Reading the Google Groups responses, he's officially created a http://xkcd.com/1172/ situation heh
Re:Virtualenv's bin/activate is Doing It Wrong by avdd_ on Wednesday February 27, 2013 @06:30AM (#5291796)
Re:Virtualenv's bin/activate is Doing It Wrong by timtadh on Wednesday February 27, 2013 @09:06AM (#5292370)
I handle this issue (and a bunch of other environment variable issues) with a little utility I wrote.

https://github.com/timtadh/swork

It basically figures out the pid of your shell and dumps the environment variables into a file in /tmp/swork. Then you make custom activate scripts for all of your projects. It has really helped me deal with a lot of projects. It also has convenience functions for quickly cd'ing to any project.

When you want to get back to the original configuration it simply restores the original environment vars.

Re:Virtualenv's bin/activate is Doing It Wrong by cabalamat on Wednesday February 27, 2013 @12:24PM (#5293526)
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?
Re:Virtualenv's bin/activate is Doing It Wrong by ibejoeb on Wednesday February 27, 2013 @12:33PM (#5293582)
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 by tbatterii on Wednesday February 27, 2013 @08:55AM (#5292332)
I haven't used activate in years, I just call the interpreter directly.
Re:Virtualenv's bin/activate is Doing It Wrong by thraxil on Wednesday February 27, 2013 @10:02AM (#5292649)
Yep. I do mostly Django development and my project template just automatically sets up a virtualenv in a 've' subdirectory and sets the shebang line in manage.py to "#!ve/bin/python". From there, I just run "./manage.py whatever" and I never even have to think about it.
Re:Virtualenv's bin/activate is Doing It Wrong by untothebreach on Wednesday February 27, 2013 @10:53AM (#5292949)
that's a good way...since I already use Makefiles to do little tasks in my projects, I end up just setting `PYTHON=.virtualenv/bin/python` and in my targets, using `$(PYTHON)` instead of `python`. Ditto for `pip`, `nosetests`, etc.
Re:Virtualenv's bin/activate is Doing It Wrong by diminoten on Wednesday February 27, 2013 @12:38PM (#5293623)
None of these problems are the kinds of problems I've ever run into. They're entirely a series of what if and edge case scenarios in which virtualenv is not the answer, which is fine. I don't need a tool that can do things I'm never going to do.
Re:Virtualenv's bin/activate is Doing It Wrong by snprbob86 on Wednesday February 27, 2013 @12:44PM (#5293674)
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 by kennu on Wednesday February 27, 2013 @05:57AM (#5291713)
But what will be the added CO2 footprint of launching so many unnecessary shells!

Also, I don't like the prospect of having to hit Ctrl-D twice to logout.

Re:Virtualenv's bin/activate is Doing It Wrong by simonh on Wednesday February 27, 2013 @10:32AM (#5292814)
> But what will be the added CO2 footprint of launching so many unnecessary shells!

The shells are actually fairly easy to recycle responsibly, it's fork()s I'm worried about.

Re:Virtualenv's bin/activate is Doing It Wrong by __alexs on Wednesday February 27, 2013 @07:07AM (#5291922)
Then type 'exec inve' instead of just 'inve.'
Re:Virtualenv's bin/activate is Doing It Wrong by kennu on Wednesday February 27, 2013 @07:11AM (#5291931)
Perhaps that makes sense. Actually I usually type 'workon myproject' (using virtualenvwrapper, because it provides a nice standard location and activation command for virtualenvs), so maybe this could all be transparent.
Re:Virtualenv's bin/activate is Doing It Wrong by regularfry on Wednesday February 27, 2013 @09:24AM (#5292457)
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.
Re:Virtualenv's bin/activate is Doing It Wrong by regularfry on Wednesday February 27, 2013 @12:40PM (#5293633)
...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 by heykiddos on Wednesday February 27, 2013 @08:16AM (#5292142)
This is also how all HPC computing environments (such as SLURM, SGE) do it. They simply spawn a subshell for you to work with and do whatever you want to run.
Re:Virtualenv's bin/activate is Doing It Wrong by michaelhoffman on Wednesday February 27, 2013 @11:28AM (#5293164)
As a long-time SGE user you are not describing something familiar to me. Can you describe what you mean more specifically?
Re:Virtualenv's bin/activate is Doing It Wrong by amckinlay on Wednesday February 27, 2013 @01:34PM (#5293975)
...and Python 3.3 just included virtualenv as a standard library...