我在考虑把virtualenv的Django web应用程序,我正在我的应用程序的git存储库。这似乎是一个简单的方法来保持部署的简单和容易。有什么理由不让我这么做吗?
当前回答
Storing the virtualenv directory inside git will, as you noted, allow you to deploy the whole app by just doing a git clone (plus installing and configuring Apache/mod_wsgi). One potentially significant issue with this approach is that on Linux the full path gets hard-coded in the venv's activate, django-admin.py, easy_install, and pip scripts. This means your virtualenv won't entirely work if you want to use a different path, perhaps to run multiple virtual hosts on the same server. I think the website may actually work with the paths wrong in those files, but you would have problems the next time you tried to run pip.
已经给出的解决方案是在git中存储足够的信息,以便在部署期间创建virtualenv并进行必要的pip安装。通常情况下,人们运行pip freeze来获取列表,然后将其存储在一个名为requirements.txt的文件中。可以用pip install -r requirements.txt加载。RyanBrady已经展示了如何将deploy语句串成一行:
# before 15.1.0
virtualenv --no-site-packages --distribute .env &&\
source .env/bin/activate &&\
pip install -r requirements.txt
# after deprecation of some arguments in 15.1.0
virtualenv .env && source .env/bin/activate && pip install -r requirements.txt
就我个人而言,我只是把这些放在我在做git克隆或git拉之后运行的shell脚本中。
存储virtualenv目录还使处理pip升级变得有点棘手,因为您必须手动添加/删除和提交升级产生的文件。对于requirements.txt文件,只需更改requirements.txt中的适当行,然后重新运行pip install -r requirements.txt。如前所述,这也减少了“提交垃圾邮件”。
其他回答
我使用pip freeze将我需要的包添加到requirements.txt文件中,并将其添加到我的存储库中。我试图想出一种方法来解释为什么要存储整个virtualenv,但我找不到。
我认为出现的主要问题之一是virtualenv可能无法被其他人使用。原因是它总是使用绝对路径。因此,如果您的virtualenv是在/home/lyle/myenv/,它将假设所有其他使用此存储库的人都是相同的(它必须是完全相同的绝对路径)。您不能假定人们使用与您相同的目录结构。
更好的做法是每个人都建立自己的环境(无论是否使用virtualenv)并在那里安装库。这也使您的代码在不同的平台(Linux/Windows/Mac)上更可用,还因为virtualenv安装在每个平台上都不同。
我用的基本上是David Sickmiller的答案,但更自动化一点。我在我的项目的顶层创建了一个名为activate的(不可执行的)文件,包含以下内容:
[ -n "$BASH_SOURCE" ] \
|| { echo 1>&2 "source (.) this with Bash."; exit 2; }
(
cd "$(dirname "$BASH_SOURCE")"
[ -d .build/virtualenv ] || {
virtualenv .build/virtualenv
. .build/virtualenv/bin/activate
pip install -r requirements.txt
}
)
. "$(dirname "$BASH_SOURCE")/.build/virtualenv/bin/activate"
(根据David的回答,这假设您正在执行pip freeze > requirements.txt,以保持您的需求列表是最新的。)
以上给出了大致的思路;我通常使用的实际激活脚本(文档)更复杂一些,提供了-q (quiet)选项,在python3不可用时使用python,等等。
然后,它可以从任何当前工作目录中获取,并将正确激活,如果需要,首先设置虚拟环境。我的顶级测试脚本通常有这样的代码,所以它可以在不需要开发人员激活的情况下运行:
cd "$(dirname "$0")"
[[ $VIRTUAL_ENV = $(pwd -P) ]] || . ./activate
源。/activate,而不是activate,在这里很重要,因为后者会在找到当前目录中的activate之前找到您路径中的任何其他activate。
Storing the virtualenv directory inside git will, as you noted, allow you to deploy the whole app by just doing a git clone (plus installing and configuring Apache/mod_wsgi). One potentially significant issue with this approach is that on Linux the full path gets hard-coded in the venv's activate, django-admin.py, easy_install, and pip scripts. This means your virtualenv won't entirely work if you want to use a different path, perhaps to run multiple virtual hosts on the same server. I think the website may actually work with the paths wrong in those files, but you would have problems the next time you tried to run pip.
已经给出的解决方案是在git中存储足够的信息,以便在部署期间创建virtualenv并进行必要的pip安装。通常情况下,人们运行pip freeze来获取列表,然后将其存储在一个名为requirements.txt的文件中。可以用pip install -r requirements.txt加载。RyanBrady已经展示了如何将deploy语句串成一行:
# before 15.1.0
virtualenv --no-site-packages --distribute .env &&\
source .env/bin/activate &&\
pip install -r requirements.txt
# after deprecation of some arguments in 15.1.0
virtualenv .env && source .env/bin/activate && pip install -r requirements.txt
就我个人而言,我只是把这些放在我在做git克隆或git拉之后运行的shell脚本中。
存储virtualenv目录还使处理pip升级变得有点棘手,因为您必须手动添加/删除和提交升级产生的文件。对于requirements.txt文件,只需更改requirements.txt中的适当行,然后重新运行pip install -r requirements.txt。如前所述,这也减少了“提交垃圾邮件”。
如果你只是设置开发环境,然后使用pip冻结文件,caz,使git回购干净。
然后如果进行生产部署,则签入整个venv文件夹。这将使您的部署更具可重复性,不需要那些libxxx-dev包,并避免互联网问题。
所以有两个回购。一个用于您的主要源代码,其中包括一个requirements.txt。和一个env repo,其中包含整个venv文件夹。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何在django应用程序中显示favicon ?
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if