TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Virtualenv's bin/activate is Doing It Wrong

107 点作者 kefeizhou大约 12 年前

13 条评论

bpatrianakos大约 12 年前
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_mitsuhiko大约 12 年前
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 未加载
lifeisstillgood大约 12 年前
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
Comkid大约 12 年前
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 未加载
timtadh大约 12 年前
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.
cabalamat大约 12 年前
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 未加载
tbatterii大约 12 年前
I haven't used activate in years, I just call the interpreter directly.
评论 #5292649 未加载
diminoten大约 12 年前
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.
snprbob86大约 12 年前
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.
kennu大约 12 年前
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 未加载
regularfry大约 12 年前
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 未加载
heykiddos大约 12 年前
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 未加载
amckinlay大约 12 年前
...and Python 3.3 just included virtualenv as a standard library...