TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Virtualenv's bin/activate is Doing It Wrong

107 pointsby kefeizhouabout 12 years ago

13 comments

bpatrianakosabout 12 years ago
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?<p>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 <i>true</i> "best" way to do anything.<p>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 <i>not</i> cocky asshole (not that I think that about the author at all, just generally speaking).
评论 #5293803 未加载
评论 #5293981 未加载
the_mitsuhikoabout 12 years ago
The fact that it does <i>not</i> use a subshell makes it useful. You can trivially use it in bash scripts and cronjobs.
评论 #5293375 未加载
评论 #5291978 未加载
lifeisstillgoodabout 12 years ago
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
Comkidabout 12 years ago
Reading the Google Groups responses, he's officially created a <a href="http://xkcd.com/1172/" rel="nofollow">http://xkcd.com/1172/</a> situation heh
评论 #5291796 未加载
timtadhabout 12 years ago
I handle this issue (and a bunch of other environment variable issues) with a little utility I wrote.<p><a href="https://github.com/timtadh/swork" rel="nofollow">https://github.com/timtadh/swork</a><p>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.<p>When you want to get back to the original configuration it simply restores the original environment vars.
cabalamatabout 12 years ago
The approach i use is simply:<p><pre><code> $ cat run #!/bin/sh . venv/bin/activate python main.py </code></pre> Is this wrong? If so, what would work better?
评论 #5293582 未加载
tbatteriiabout 12 years ago
I haven't used activate in years, I just call the interpreter directly.
评论 #5292649 未加载
diminotenabout 12 years ago
None of these problems are the kinds of problems I've <i>ever</i> run into. They're entirely a series of what if and edge case scenarios in which virtualenv is <i>not</i> the answer, which is fine. I don't need a tool that can do things I'm never going to do.
snprbob86about 12 years ago
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.<p>I used to use `exec env $@` at the end of the script, so that I could `appenv &#62; prod` for diffing, but I actually prefer this articles suggestion: `exec "${@:-$SHELL}"` since you can still `appenv env &#62; prod`, but `appenv` provides an interactive shell by default.
kennuabout 12 years ago
But what will be the added CO2 footprint of launching so many unnecessary shells!<p>Also, I don't like the prospect of having to hit Ctrl-D twice to logout.
评论 #5292814 未加载
评论 #5291922 未加载
regularfryabout 12 years ago
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.
评论 #5293633 未加载
heykiddosabout 12 years ago
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.
评论 #5293164 未加载
amckinlayabout 12 years ago
...and Python 3.3 just included virtualenv as a standard library...