Live data from Hacker News

Virtualenv's bin/activate is Doing It Wrong

gist.github.com

11–20 of 31 posts

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

#11

Earlier quoted context omitted.

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.

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.

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

#14
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

#17

I haven't used activate in years, I just call the interpreter directly.

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

#18
post #4

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.

> 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

#19
post #17

I haven't used activate in years, I just call the interpreter directly.

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.

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

#20

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.

As a long-time SGE user you are not describing something familiar to me. Can you describe what you mean more specifically?
Post reply on HN