我需要虚拟环境吗?
对于每个项目和每个阶段(开发、生产等),我使用不同的 Linux 用户。我可以使用 pip 和 --user
选项在 $HOME 中安装软件包。
我的隔离环境来自不同的 Linux 用户。
我可以从 virtualenv 获得哪些好处?到目前为止,我认为没有理由使用 virtualenv。但也许我错过了一些东西。
Linux 用户名的构建方式如下:project_name_S,S 是阶段(dev、qual、prod、testing)。每个阶段可以位于不同的主机上。
更新:
问这个问题三年多了:我现在使用 virtualenv。 用户环境有问题。也许现在有更好的支持。但没有什么可以阻止你在 $HOME 中创建 virtualenv :-)
For every project and every stage (dev, prod, ...) I use a different linux user. I can use pip and the --user
option, to install packages in $HOME.
My isolated environment comes form different linux users.
Which benefits could I get from virtualenv? Up to now, I see no reason to use virtualenv. But maybe I am missing something.
Linux user names are build like this: project_name_S and S is the stage (dev, qual, prod, testing). Each stage can be on a different host.
Update:
More than three years after asking this question: I use virtualenv now. The user-environment was buggy. Maybe it has better support now. But nothing stops you from creating a virtualenv in $HOME :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Virtualenv 非常适合管理依赖关系。配置文件(或设置文件)非常适合管理环境之间的变量差异。 (例如数据库位置等)
Python hitchhikers 指南非常好,值得花 20 分钟阅读。
http://docs.python-guide.org/en/latest/index.html
请参阅有关虚拟环境的部分。
http://docs.python-guide.org/en/latest/dev/ virtualenvs/
如果您只想使用不同的 home 或 env 模式变量,您可以在运行 python 代码之前设置它。
example.py 然后会像这样查找 PROD_MODE 变量。
那么你需要一个 virtualenv 吗?
我强烈推荐它。所以你已经有了 Django 并导入了一些其他库(我也强烈推荐 pip)并且一切都在你的机器上运行。您的路径已设置,您的代码可以解析为使用 PATH 和 PYTHON_PATH 的代码。杰出的!
现在您要在另一台计算机(可能是 aws、Linux 服务器或类似服务器)上进行部署,或者其他开发人员想要帮助您在项目上编写代码。他们如何确保该机器上的环境设置与您的环境相同,以及如何确保使用测试所有闪亮新代码的相同环境进行部署? virtualenv 可以为你做到这一点!您只需在新机器上移植或重新创建虚拟环境即可,一切都按照测试/构建的方式运行。
简而言之,虚拟环境可以帮助您确保在发布/部署代码时记住所有导入、安装和路径设置而不会感到头疼。
Virtualenv's are great for managing dependencies. Config files (or settings files) are very good for managing variable differences between environments. (e.g db location e.tc)
The python hitchhikers guide is very good and worth a 20 min read.
http://docs.python-guide.org/en/latest/index.html
See this section on virtual envs.
http://docs.python-guide.org/en/latest/dev/virtualenvs/
If you just want to use different home or env mode variables you could just set it before you run the python code.
example.py would then look for the PROD_MODE variable like so.
So do you need a virtualenv?
I would strongly recommend it. So you have Django working and you've imported some other libraries (i also strongly recommend pip) and everything is working on your machine. Your path is setup and your code can resolve to the code using PATH and PYTHON_PATH. Brilliant!
Now you come to deploy on another machine (maybe aws, an linux server or similar) or a fellow developer wants to help code on your project. How do they make sure the env on there machine is setup just the same as yours and how do you ensure you deploy with the same env you tested all your shinny new code with? A virtualenv does this for you! You just port or recreate the virtual env on the new machine any everything works just as tested/built.
In short a virtual env helps you ensure you don't have a headache in remembering all your imports, installs and path settings when you release/deploy code.
创建 virtualenv 应该比创建新用户更快更容易。我不建议切换现有项目,但考虑将其用于新项目。
Creating a virtualenv sould be quicker and easier than creating a new user. I would not recomend switching for existing projects, but consider it for new projects.
使用 virtualenv 可以得到一些用户或 home 方案无法得到的东西:
能够使用不同版本的包 - 例如,针对不同站点的 django stable 和 django dev,而不会污染系统范围的 Python安装(或您用户的 python 安装)。
能够冻结软件包要求并轻松复制运行环境。您可以使用备用安装方案来执行此操作,但是您可以执行的操作非常有限(在要安装的软件包方面),您必须手动跟踪您的需求文件。
一般来说,我建议您在下一个项目中重新访问 virtualenv。
Some things that you get with virtualenv that you don't get with the user or home schemes:
Ability to use different versions of packages - for example, django stable and django dev for different sites, without polluting the system-wide Python install (or your user's python install).
Ability to freeze package requirements and easily replicate running environment. You could possibly do this with the alternate install scheme, but you would be very limited to what you can do (in terms of packages to install), you'd have to manually keep track of your requirements file.
In general, I would recommend you re-visit virtualenv for your next project.